Serial communication between PC to a Microcontroller

Discussion in 'Embedded Systems and Microcontrollers' started by @vajra, May 27, 2018.

  1. @vajra

    Thread Starter Member

    May 2, 2018
    125
    1
    I have AT89C51 controller and i m trying to establish communication between controller and my Laptop via serial cable. I am using At89c51. I have hyperterminal tool. I don't think there is anything wrong in hyperterminal because its showing connected status.

    I am not looking any text message on hyperterminal. What could be wrong
    Code (C):
    1. #include <reg51.h>                
    2.  
    3. //Function Prototype
    4. void Serial_Init(void);
    5. void Send_Byte(unsigned char);  
    6. char Receive_Byte(void);
    7. void Send_String(char String[]);
    8.  
    9. void main(void)
    10. {
    11.   Serial_Init();    //Initialze Serial communication by calling Serial_Init() function
    12.   EA = 1;//Enable Globle interrupt
    13.   ES = 1;//Enable Serial interrupt
    14.   Send_String("Hello!!");
    15.     while(1)            
    16.   {
    17.    
    18.   }
    19. }
    20.  
    21. void Serial_Init(void)        
    22. {
    23.     TMOD = 0x20;// Timer 1 in mode 2 auto reload mode for baudrate generation
    24.     SCON = 0x50;// Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    25.     TH1 = 0xFD;// Load value for 9600 baudrate
    26.     TR1 = 1;// Start Timer
    27. }
    28.  
    29. void Send_Byte(unsigned char DATA)
    30. {
    31.     SBUF = DATA;      //Load data into SBUF
    32.     while(TI == 0); //Wait untill transmission to complete
    33.     TI = 0;                  //Clear transmitte interrupt(TI) flag
    34. }
    35.  
    36. char Receive_Byte(void)
    37. {
    38.     char Receive;
    39.     while(RI == 0); //wait for character
    40.     Receive = SBUF; //Read received data from SBUF
    41.     RI=0;           //Clear Receive interrupt flag
    42.     return Receive;
    43. }
    44.  
    45. void Send_String(char String[])
    46. {
    47.   unsigned char count;
    48.   for(count=0;String[count]!='\0';count++)    //Send single byte one by one untill null character in string
    49.   Send_Byte(String[count]);
    50. }
    51.  
    52. void serial_ISR (void) interrupt 4 //Serial interrupt
    53. {
    54.     //receive character
    55.     char Receive;
    56.     if(RI==1)
    57.     {
    58.         Receive = SBUF;          //Read received data from SBUF
    59.         RI = 0;                  //Clear Receive interrupt flag
    60.     Send_Byte(Receive);      //echo received character
    61.     }
    62.  
    63.   P1 = Receive;              //Display ASCII value of receive character at P1
    64.     RI = 0;
    65. }        
    66.  
     
  2. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,900
    394
    This works
    Code (Text):
    1. #include "reg51.h"
    2. #include "stdio.h"
    3.  
    4. void main (void)
    5. {
    6. SCON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
    7. TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
    8. TH1   = 0xfd;                   /* TH1:  reload value for 9600 baud         */
    9. TR1   = 1;                      /* TR1:  timer 1 run                        */
    10. TI    = 1;                      /* TI:   set TI to send first char of UART  */
    11.  
    12. while(1)
    13.   {
    14.   unsigned char aaa;
    15.   aaa = _getkey();
    16.   putchar(aaa);
    17. printf ("You pressed '%c'.\r\n\r\n", c);
    18.   }
    19. }
    20.  
     
    Last edited: May 27, 2018
    @vajra and cmartinez like this.
  3. be80be

    AAC Fanatic!

    Jul 5, 2008
    1,900
    394
  4. @vajra

    Thread Starter Member

    May 2, 2018
    125
    1
    Sorry for taking so long time to come back on this threads.

    Thanks I appreciate your help. your program is working but you forgot to declared variable c in your code. Program give error without declaration
    Code (C):
    1. #include "reg51.h"
    2. #include "stdio.h"
    3. void main (void)
    4. {
    5. SCON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
    6. TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
    7. TH1   = 0xfd;                   /* TH1:  reload value for 9600 baud         */
    8. TR1   = 1;                      /* TR1:  timer 1 run                        */
    9. TI    = 1;                      /* TI:   set TI to send first char of UART  */
    10. while(1)
    11.   {
    12.   unsigned char aaa;
    13.    unsigned char c;
    14.   aaa = _getkey();
    15.   putchar(aaa);
    16. printf ("You pressed '%c'.\r\n\r\n", c);
    17.   }
    18. }
    I am trying to figure out what's the wrong happening in my first code. I tried to write code as per my knowledge but I don't understand why it's not working.

    Can you point out what's the wrong in code ?
     
    Last edited: May 30, 2018
  5. @vajra

    Thread Starter Member

    May 2, 2018
    125
    1
    I did modification in my code and now I can send data to microcontroller and I can received data from microcontroller

    PC com.jpg

    code

    Code (C):
    1. #include <reg51.h>                
    2. //Function Prototype
    3. void Serial_Init(void);
    4. void Send_Byte(unsigned char);  
    5. char Receive_Byte(void);
    6. void Send_String(char String[]);
    7. void main(void)
    8. {
    9.   Serial_Init();    //Initialze Serial communication by calling Serial_Init() function
    10.   EA = 1;//Enable Globle interrupt
    11.   ES = 1;//Enable Serial interrupt
    12.   Send_String("Hello!!");
    13.     while(1)            
    14.   {
    15.  
    16.   }
    17. }
    18. void Serial_Init(void)        
    19. {
    20.       TR1 = 0;// Stop timer
    21.     TMOD = 0x20;// Timer 1 in mode 2 auto reload mode for baudrate generation
    22.     SCON = 0x50;// Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    23.     TH1 = 0xFD;// Load value for 9600 baudrate
    24.     TR1 = 1;// Start Timer
    25. }
    26. void Send_Byte(unsigned char DATA)
    27. {
    28.     SBUF = DATA;      //Load data into SBUF
    29.     while(TI == 0); //Wait untill transmission to complete
    30.     TI = 0;                  //Clear transmitte interrupt(TI) flag
    31. }
    32. char Receive_Byte(void)
    33. {
    34.     char Receive;
    35.     while(RI == 0); //wait for character
    36.     Receive = SBUF; //Read received data from SBUF
    37.     RI=0;           //Clear Receive interrupt flag
    38.     return Receive;
    39. }
    40.  
    41. /* Function to send a characters untill a null*/
    42. void Send_String(char String[])
    43. {
    44.   unsigned char i = 0;
    45.     while(String[i] != '\0')
    46.     {
    47.         Send_Byte(String[i]);
    48.         i++;
    49.     }
    50. }
    51. void serial_ISR (void) interrupt 4 //Serial interrupt
    52. {
    53.     //receive character
    54.     char Receive;
    55.     if(RI==1)
    56.     {
    57.         Receive = SBUF;          //Read received data from SBUF
    58.         RI = 0;                  //Clear Receive interrupt flag
    59.     Send_Byte(Receive);      //echo received character
    60.     }
    61.   P1 = Receive;              //Display ASCII value of receive character at P1
    62.     RI = 0;
    63. }  
     
  6. @vajra

    Thread Starter Member

    May 2, 2018
    125
    1
    If you look at my both code. I have modified function to send string
    code 1
    Code (C):
    1. void Send_String(char String[])
    2. {
    3.   unsigned char count;
    4.  
    5.     for(count=0;String[count]!='\0';count++)    //Send single byte one by one untill null character in string
    6.  
    7. Send_Byte(String[count]);
    8. }
    code 2
    Code (C):
    1. /* Function to send a characters untill a null*/
    2. void Send_String(char String[])
    3. {
    4.   unsigned char i = 0;
    5.     while(String[i] != '\0')
    6.     {
    7.         Send_Byte(String[i]);
    8.         i++;
    9.     }
    10. }
    I don't think there is something wrong in first function.

    Somebody can help me to understand , why it was not working ?
     
    Last edited: May 31, 2018
Loading...