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

Discussion in 'Programmer's Corner' started by sarkarnov, Dec 4, 2018.

  1. sarkarnov

    Thread Starter New Member

    Dec 4, 2018
    1
    0
    #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;
    }
    }
    }
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,041
    3,134
    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.
     
  3. dl324

    AAC Fanatic!

    Mar 30, 2015
    7,435
    1,759
    Your code would be easier to read using code tags:
    Code (Text):
    1. #include<at89x52.h>
    2. #include<intrins.h>
    3.  
    4. sbit LAMP1 = P2 ^ 0;
    5. sbit LAMP2 = P2 ^ 1;
    6. sbit LAMP3 = P2 ^ 2;
    7. sbit LAMP4 = P2 ^ 3;
    8. unsigned char ch;
    9. void init_Serial() {
    10.   TMOD = 0x10;
    11.   TH1 = 0xFA;
    12.   SCON = 0x50;
    13.   TR0 = 1;
    14. }
    15.  
    16. void Rx_Char() {
    17.   while (RI == 1);
    18.   ch = SBUF;
    19.   RI = 0;
    20. }
    21.  
    22. void Tx_String(unsigned char *str) {
    23.   while (*str != '\0') {
    24.     SBUF = *str;
    25.     while (TI == 1);
    26.     TI = 0;
    27.     str++;
    28.   }
    29. }
    30.  
    31. void main() {
    32.   P2 = 0xFF;
    33.   init_Serial();
    34.   Tx_String("\rENTER:\r1:LAMP1 ON\r3:LAMP2 ON\r5:LAMP3 ON\r7:LAMP4 ON\r");
    35.   Tx_String("\rENTER:\r2:LAMP1 OFF\r4:LAMP2 OFF\r6:LAMP3 OFF\r8:LAMP4 OFF\r");
    36.   Tx_String("\rENTER:\r9:ALL LAMPS ON\r");
    37.   Tx_String("\rENTER:\r0:ALL LAMPS OFF\r");
    38.  
    39.   while (1) {
    40.     Rx_Char();
    41.     switch (ch) {
    42.       case '1':
    43.         Tx_String("\rLAMP1 ON\r");
    44.         LAMP1 = 0;
    45.         break;
    46.  
    47.       case '2':
    48.         Tx_String("\rLAMP1 OFF\r");
    49.         LAMP1 = 1;
    50.         break;
    51.  
    52.       case '3':
    53.         Tx_String("\rLAMP2 ON\r");
    54.         LAMP2 = 0;
    55.         break;
    56.  
    57.       case '4':
    58.         Tx_String("\rLAMP2 OFF\r");
    59.         LAMP2 = 1;
    60.         break;
    61.  
    62.       case '5':
    63.         Tx_String("\rLAMP3 ON\r");
    64.         LAMP3 = 0;
    65.         break;
    66.  
    67.       case '6':
    68.         Tx_String("\rLAMP3 OFF\r");
    69.         LAMP3 = 1;
    70.         break;
    71.  
    72.       case '7':
    73.         Tx_String("\rLAMP4 ON\r");
    74.         LAMP4 = 0;
    75.         break;
    76.  
    77.       case '8':
    78.         Tx_String("\rLAMP4 OFF\r");
    79.         LAMP4 = 1;
    80.         break;
    81.  
    82.       case '9':
    83.         Tx_String("\rALL LAMPS ON\r");
    84.         LAMP1 = 0;
    85.         LAMP2 = 0;
    86.         LAMP3 = 0;
    87.         LAMP4 = 0;
    88.         break;
    89.  
    90.       case '0':
    91.         Tx_String("\rALL LAMPS OFF\r");
    92.         LAMP1 = 1;
    93.         LAMP2 = 1;
    94.         LAMP3 = 1;
    95.         LAMP4 = 1;
    96.         break;
    97.  
    98.       default:
    99.         LAMP1 = 0;
    100.         LAMP2 = 0;
    101.         LAMP3 = 0;
    102.         LAMP4 = 0;
    103.         break;
    104.     }
    105.   }
    106. }
    EDIT: fixed problem with tabs.
     
    Last edited: Dec 4, 2018
    MrSoftware and JohnInTX like this.
  4. MrSoftware

    Senior Member

    Oct 29, 2013
    1,130
    332
    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..
     
  5. Raymond Genovese

    Well-Known Member

    Mar 5, 2016
    1,024
    571
    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:

    Code (C):
    1.  
    2.     default:
    3.         Tx_String("\rUnknown Command\r");
    4.         break;
    5.  
     
  6. MrSoftware

    Senior Member

    Oct 29, 2013
    1,130
    332
    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.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    7,041
    3,134
    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.
     
Loading...