Serial communication between PC to a Microcontroller

Thread Starter

@vajra

Joined May 2, 2018
154
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
C:
#include <reg51.h>                 

//Function Prototype
void Serial_Init(void);
void Send_Byte(unsigned char);   
char Receive_Byte(void);
void Send_String(char String[]);

void main(void)
{
  Serial_Init();    //Initialze Serial communication by calling Serial_Init() function
  EA = 1;//Enable Globle interrupt
  ES = 1;//Enable Serial interrupt
  Send_String("Hello!!");
    while(1)             
  {
   
  }
}

void Serial_Init(void)         
{
    TMOD = 0x20;// Timer 1 in mode 2 auto reload mode for baudrate generation
    SCON = 0x50;// Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    TH1 = 0xFD;// Load value for 9600 baudrate
    TR1 = 1;// Start Timer
}

void Send_Byte(unsigned char DATA)
{
    SBUF = DATA;      //Load data into SBUF
    while(TI == 0); //Wait untill transmission to complete
    TI = 0;                  //Clear transmitte interrupt(TI) flag
}

char Receive_Byte(void)
{
    char Receive;
    while(RI == 0); //wait for character
    Receive = SBUF; //Read received data from SBUF
    RI=0;           //Clear Receive interrupt flag
    return Receive;
}

void Send_String(char String[])
{
  unsigned char count;
  for(count=0;String[count]!='\0';count++)    //Send single byte one by one untill null character in string
  Send_Byte(String[count]);
}

void serial_ISR (void) interrupt 4 //Serial interrupt
{
    //receive character
    char Receive;
    if(RI==1)
    {
        Receive = SBUF;          //Read received data from SBUF
        RI = 0;                  //Clear Receive interrupt flag
    Send_Byte(Receive);      //echo received character
    }
 
  P1 = Receive;              //Display ASCII value of receive character at P1
    RI = 0;
}
 

be80be

Joined Jul 5, 2008
2,072
This works
Code:
#include "reg51.h"
#include "stdio.h"

void main (void)
{
SCON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
TH1   = 0xfd;                   /* TH1:  reload value for 9600 baud         */
TR1   = 1;                      /* TR1:  timer 1 run                        */
TI    = 1;                      /* TI:   set TI to send first char of UART  */

while(1)
  {
  unsigned char aaa;
  aaa = _getkey();
  putchar(aaa);
printf ("You pressed '%c'.\r\n\r\n", c);
  }
}
 
Last edited:

Thread Starter

@vajra

Joined May 2, 2018
154
Read this it's lot easier then what your doing http://www.keil.com/download/docs/71.asp
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
C:
#include "reg51.h"
#include "stdio.h"
void main (void)
{
SCON  = 0x50;                   /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD |= 0x20;                   /* TMOD: timer 1, mode 2, 8-bit reload      */
TH1   = 0xfd;                   /* TH1:  reload value for 9600 baud         */
TR1   = 1;                      /* TR1:  timer 1 run                        */
TI    = 1;                      /* TI:   set TI to send first char of UART  */
while(1)
  {
  unsigned char aaa;
   unsigned char c;
  aaa = _getkey();
  putchar(aaa);
printf ("You pressed '%c'.\r\n\r\n", c);
  }
}
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:

Thread Starter

@vajra

Joined May 2, 2018
154
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

C:
#include <reg51.h>                
//Function Prototype
void Serial_Init(void);
void Send_Byte(unsigned char);  
char Receive_Byte(void);
void Send_String(char String[]);
void main(void)
{
  Serial_Init();    //Initialze Serial communication by calling Serial_Init() function
  EA = 1;//Enable Globle interrupt
  ES = 1;//Enable Serial interrupt
  Send_String("Hello!!");
    while(1)            
  {
  
  }
}
void Serial_Init(void)        
{
      TR1 = 0;// Stop timer
    TMOD = 0x20;// Timer 1 in mode 2 auto reload mode for baudrate generation
    SCON = 0x50;// Serial Mode 1, 8-DATA bit 1-Start Bit, 1-Stop Bit, Receive Enable
    TH1 = 0xFD;// Load value for 9600 baudrate
    TR1 = 1;// Start Timer
}
void Send_Byte(unsigned char DATA)
{
    SBUF = DATA;      //Load data into SBUF
    while(TI == 0); //Wait untill transmission to complete
    TI = 0;                  //Clear transmitte interrupt(TI) flag
}
char Receive_Byte(void)
{
    char Receive;
    while(RI == 0); //wait for character
    Receive = SBUF; //Read received data from SBUF
    RI=0;           //Clear Receive interrupt flag
    return Receive;
}

/* Function to send a characters untill a null*/
void Send_String(char String[])
{
  unsigned char i = 0;
    while(String[i] != '\0')
    {
        Send_Byte(String[i]);
        i++;
    }
}
void serial_ISR (void) interrupt 4 //Serial interrupt
{
    //receive character
    char Receive;
    if(RI==1)
    {
        Receive = SBUF;          //Read received data from SBUF
        RI = 0;                  //Clear Receive interrupt flag
    Send_Byte(Receive);      //echo received character
    }
  P1 = Receive;              //Display ASCII value of receive character at P1
    RI = 0;
}
 

Thread Starter

@vajra

Joined May 2, 2018
154
If you look at my both code. I have modified function to send string
code 1
C:
void Send_String(char String[])
{
  unsigned char count;

    for(count=0;String[count]!='\0';count++)    //Send single byte one by one untill null character in string

Send_Byte(String[count]);
}
code 2
C:
/* Function to send a characters untill a null*/
void Send_String(char String[])
{
  unsigned char i = 0;
    while(String[i] != '\0')
    {
        Send_Byte(String[i]);
        i++;
    }
}
I don't think there is something wrong in first function.

Somebody can help me to understand , why it was not working ?
 
Last edited:
Top