can anyone tell me what the code tell us...

Thread Starter

sarkarnov

Joined Dec 4, 2018
1
#include<at89x52.h>
#include<intrins.h>

sbit LAMP1=P2^0;
sbit LAMP2=P2^1;
sbit LAMP3=P2^2;
sbit LAMP4=P2^3;
unsigned char ch;
void init_Serial()
{
TMOD=0x10;
TH1=0xFA;
SCON=0x50;
TR0=1;
}

void Rx_Char()
{
while(RI==1);
ch=SBUF;
RI=0;
}

void Tx_String(unsigned char*str)
{
while(*str!='\0')
{
SBUF=*str;
while(TI==1);
TI=0;
str++;
}
}

void main()
{
P2=0xFF;
init_Serial();
Tx_String("\rENTER:\r1:LAMP1 ON\r3:LAMP2 ON\r5:LAMP3 ON\r7:LAMP4 ON\r");
Tx_String("\rENTER:\r2:LAMP1 OFF\r4:LAMP2 OFF\r6:LAMP3 OFF\r8:LAMP4 OFF\r");
Tx_String("\rENTER:\r9:ALL LAMPS ON\r");
Tx_String("\rENTER:\r0:ALL LAMPS OFF\r");

while(1)
{
Rx_Char();
switch(ch)
{
case'1':Tx_String("\rLAMP1 ON\r");
LAMP1=0;
break;

case'2':Tx_String("\rLAMP1 OFF\r");
LAMP1=1;
break;

case'3':Tx_String("\rLAMP2 ON\r");
LAMP2=0;
break;

case'4':Tx_String("\rLAMP2 OFF\r");
LAMP2=1;
break;

case'5':Tx_String("\rLAMP3 ON\r");
LAMP3=0;
break;

case'6':Tx_String("\rLAMP3 OFF\r");
LAMP3=1;
break;

case'7':Tx_String("\rLAMP4 ON\r");
LAMP4=0;
break;

case'8':Tx_String("\rLAMP4 OFF\r");
LAMP4=1;
break;

case'9':Tx_String("\rALL LAMPS ON\r");
LAMP1=0;
LAMP2=0;
LAMP3=0;
LAMP4=0;
break;

case'0':Tx_String("\rALL LAMPS OFF\r");
LAMP1=1;
LAMP2=1;
LAMP3=1;
LAMP4=1;
break;

default:LAMP1=0;
LAMP2=0;
LAMP3=0;
LAMP4=0;
break;
}
}
}
 

spinnaker

Joined Oct 29, 2009
7,830
No way to know for sure without knowing what Tx_String does. I am perplexed at why you would use it that way in a switch statement.


But the code looks like it writes a menu to the serial port, gets input from the user via the serial port then turns on the selected light.
 

dl324

Joined Mar 30, 2015
16,918
Your code would be easier to read using code tags:
Code:
#include<at89x52.h>
#include<intrins.h>

sbit LAMP1 = P2 ^ 0;
sbit LAMP2 = P2 ^ 1;
sbit LAMP3 = P2 ^ 2;
sbit LAMP4 = P2 ^ 3;
unsigned char ch;
void init_Serial() {
  TMOD = 0x10;
  TH1 = 0xFA;
  SCON = 0x50;
  TR0 = 1;
}

void Rx_Char() {
  while (RI == 1);
  ch = SBUF;
  RI = 0;
}

void Tx_String(unsigned char *str) {
  while (*str != '\0') {
    SBUF = *str;
    while (TI == 1);
    TI = 0;
    str++;
  }
}

void main() {
  P2 = 0xFF;
  init_Serial();
  Tx_String("\rENTER:\r1:LAMP1 ON\r3:LAMP2 ON\r5:LAMP3 ON\r7:LAMP4 ON\r");
  Tx_String("\rENTER:\r2:LAMP1 OFF\r4:LAMP2 OFF\r6:LAMP3 OFF\r8:LAMP4 OFF\r");
  Tx_String("\rENTER:\r9:ALL LAMPS ON\r");
  Tx_String("\rENTER:\r0:ALL LAMPS OFF\r");

  while (1) {
    Rx_Char();
    switch (ch) {
      case '1':
        Tx_String("\rLAMP1 ON\r");
        LAMP1 = 0;
        break;

      case '2':
        Tx_String("\rLAMP1 OFF\r");
        LAMP1 = 1;
        break;

      case '3':
        Tx_String("\rLAMP2 ON\r");
        LAMP2 = 0;
        break;

      case '4':
        Tx_String("\rLAMP2 OFF\r");
        LAMP2 = 1;
        break;

      case '5':
        Tx_String("\rLAMP3 ON\r");
        LAMP3 = 0;
        break;

      case '6':
        Tx_String("\rLAMP3 OFF\r");
        LAMP3 = 1;
        break;

      case '7':
        Tx_String("\rLAMP4 ON\r");
        LAMP4 = 0;
        break;

      case '8':
        Tx_String("\rLAMP4 OFF\r");
        LAMP4 = 1;
        break;

      case '9':
        Tx_String("\rALL LAMPS ON\r");
        LAMP1 = 0;
        LAMP2 = 0;
        LAMP3 = 0;
        LAMP4 = 0;
        break;

      case '0':
        Tx_String("\rALL LAMPS OFF\r");
        LAMP1 = 1;
        LAMP2 = 1;
        LAMP3 = 1;
        LAMP4 = 1;
        break;

      default:
        LAMP1 = 0;
        LAMP2 = 0;
        LAMP3 = 0;
        LAMP4 = 0;
        break;
    }
  }
}
EDIT: fixed problem with tabs.
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,197
Generally it looks like it prints a menu, the user enters a number 1-9 and pins (lamps) are turned on or off according to the number entered by the user and the switch() statement. The variables of type sbit should tie that variable directly to a pin, so setting the variable to 1 or 0 sets the pin high or low. I'm not familiar with the platform so I can't help much with the details about setting up serial, etc..
 

Raymond Genovese

Joined Mar 5, 2016
1,653
What I noticed is what goes on in the default section of the switch-case. If the input is anything other than 0-9, all lamps are turned on and with no message.

Since that outcome is the same (minus the message) as case '9', I would have thought that something like this would be more appropriate:

C:
    default:
        Tx_String("\rUnknown Command\r");
        break;
 

MrSoftware

Joined Oct 29, 2013
2,197
What I noticed is what goes on in the default section of the switch-case. If the input is anything other than 0-9, all lamps are turned on and with no message.

Since that outcome is the same (minus the message) as case '9', I would have thought that something like this would be more appropriate:

C:
    default:
        Tx_String("\rUnknown Command\r");
        break;
Indeed, it looks like it's a home project or learning project program, not for a real product, is my guess. Probably thrown together just to see if something would work.
 

spinnaker

Joined Oct 29, 2009
7,830
TS has not been back and my guess is likely not to b back judging from only one post. I see no point in continuing to post to this thread until TS returns.
 
Top