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.

atmel 89c51 uart problem

Status
Not open for further replies.

chethankp

Member level 4
Joined
Jan 4, 2010
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
bangalore
Activity points
1,860
Hi,

I have written a uart program to send some data to PC from atmel 89c51
I get some dummy char back when ever I send data .

if I remove sendUartdata(); from my receive interrupt , I get the proper data back .

What would be the problem ?

below is the code I am using ...
#include <REGX51.H>
#include "reg_c51.h"

char uart_data;

char *SendData ="Testing All Data";

void main (void)
{
SCON = 0x50; /* uart in mode 1 (8 bit), REN=1 */
TMOD = TMOD | 0x20 ; /* Timer 1 in mode 2 */
TH1 = 0xFD; /* 9600 Bds at 11.059MHz */
TL1 = 0xFD; /* 9600 Bds at 11.059MHz */
ES = 1; /* Enable serial interrupt*/
EA = 1; /* Enable global interrupt */
TR1 = 1; /* Timer 1 run */
while(1); /* endless */
}

void sendUartdata(void)
{
int i =0;
for(i = 0; i<16 ; i++)
SBUF = SendData;
}


void serial_IT(void) interrupt 4
{
if (RI == 1)
{ /* if reception occur */
RI = 0; /* clear reception flag for next reception */
uart_data = SBUF; /* Read receive data */
SBUF = 'A'; /* Send back same data on uart*/
sendUartdata();

}
else TI = 0; /* if emission occur */
}
 

The problem is that when you write a byte at the uart, it needs a certain time to be transmited. This time deppends on the baudrate. It's easy to calculate. For example, if you are transmiting with no parity and one stop bit, the total ammount of bits is 10 (8 from the byte, 1 start bit and 1 stop bit). If your baudrate if 9600 bps, the time per bit is 1 / 9600 = 104 micro seconds. So, the time to transmit one byte will be 10 bits * 104 us = 1.04 ms. You need to wait more than 1ms to put another char at the usart.

I never used this microcontroller that you are working with, but in the PIC family microcontroller, there's a bit on a uart SFR that indicates if the uart transmission is busy. If is the case for your microcontroller, your function sendUartdata() will look like:

void sendUartdata(void)
{
int i =0;
for(i = 0; i<16 ; i++)
{
while(BUSY_FLAG); // wait here until the tx buf be empty
SBUF = SendData;
}
}



Hope I've helped.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top