Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing 89C51 Microcontroller with PC using UART

Status
Not open for further replies.

sathiieesh

Newbie level 5
Newbie level 5
Joined
Feb 5, 2010
Messages
8
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,283
Location
Chennai
Activity points
1,379
Hi..
I am interfacing 89c51 microcontroller with PC using UART... when i used the code below.. i can used to send and receive the messages in hyperterminal but its not the actual message.. for example if i send Z from PC, microcontroller returns it back by [ ... if i send w from PC , microcontroller returns back by sending u ... what might be the problem.. i also checked it for different baud rates.... i have used max232 ic in between microcontroller and rs232...
Compiler:MikroC for 8051
please check it...


Code:
char uart_rd;

void main() {
  
  UART1_Init(4800);               // Initialize UART module at 4800 bps
  Delay_ms(100);                  // Wait for UART module to stabilize
  
  UART1_Write_Text("Start");
  while (1) {                     // Endless loop
    if (UART1_Data_Ready()) {     // If data is received,
      uart_rd = UART1_Read();     //   read the received data,
      UART1_Write(uart_rd);       //   and send data via UART
    }
  }
}

Sathiesh Kumar.V
 

if u use keil example than test ur hardware first.then u go for ur compiler.Hello World example
 

First, try to add a delay between the read and the write commands.

Second, try a single operation at a time.
For instance, try just sending a character from you controller to the hyperterminal. If you received it correctly, then try the opposite operation; that is sending a character from the hyperterminal to the controller.

Hope this helps.
 

Hii Stahiiresh,

1. First off all what compiler you are using?
If you are using KEIL or SDCC you can use this code

2. For receiving data in PC there is 2 methode :
1. Polling Methode ( it means that you are waiting until data is recived and micro can't do anything else)
2. Interrupt Mode ( it means micro can do anything, but if the PC sending data the program stop and give the serive to the Intterupt Serial Vector for handling communication with pc)

From your source code : I think you are using polling methode.

Step for Programming UART :
1. Register Use for UART (SBUF 0x99 , PCON 0x87, SCON 0x98)
2. Decide Timer for Clock
3. Decide Mode :
Mode 0 ==> Syncrohnous
Mode 1 ==> Uart 8 Bit Baud Rate Flexible
Mode 2 ==> Uart 9 Bit Baud Rate Fixed
Mode 3 ==> Uart 9 Bit Baud Rate Flexible ( Multi Processor Communication Master Slave)


Step For Counting Baudrate for Register :
Baudrate = (2^ smod/32) * (Frequency Osilator/(12*(256-TH1)

example :
AT89C51 using Timer 1 using SMOD 1 Baudrate 9600 Frequency Osilator = 11.0592 MHz

9600 = (2^1/32) *(11059200 /(12*(256-TH1))
[256-TH1] = (2^1/32*11059200/9600)
TH1 = 256 - 6 = 250 (Decimal) / 0xFA (Heksa)

The Final Sample Source Code : ( I Use Mide Compiler. It's Free Compiler Using SDCC check this site http://www.opcube.com/home.html) :

Code:
//==============
// For Polling Methode
//==============
#include <8051.h>

//==============================================
// Serial Inisialisation using 9600 baud rate 8 data bits no parity 1 stop bit
//==============================================
void init_serial(unsigned char baud)
{
  TMOD = 0x20; //using timer 1 mode auto reload
  PCON = 0;
  SM0 = 0;
  SM1 = 1; // mode 1 (sm0 = 0 sm1 = 0 ==> mode 0 , sm0 = 1 sm1 = 0 ==>   / mode 2)
  REN = 1;
  TH1 = baud;
  TL1 = baud;
  TR1 = 1;
}

//====================
// For Send one character to PC
//====================
void send_char(unsigned char datasent)
{
  SBUF = datasent;
  while(!TI)
  {;}
  TI = 0;
}
//=====================
// For send some characters to PC
//=====================
void send_text(unsigned char *text)
{
   char i = 0;
  while (text[i]!=0)
  {
   send_char(text[i]);
   i++;
  }
}

//=====================
// For Receive data from PC 
//=====================
char get_data_from_pc()
{
 while(!RI)
 {;}
  RI=0;
 return SBUF;
}
//===================
// Main Function
//===================
void main(void)
{
  unsigned char datapc;
  init_serial(0xfa);
   while(1)
 {
  datapc=get_data_from_pc();
  send_char(datapc);  
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top