Hi, i am using AT89C51ED2 microcontroller to perform serial communication using interrupt 4. The main purpose of code program is send received data to hyper-terminal using interrupt 4 (serial interrupt, echo received data) while the led connected at P1^0 will continuously blink with a delay of 1 sec.
PROBLEM:
when i produce a delay using timer, serial communication doesn't work(timer-1 for serial communication and timer-0 for delay). On the other hand if i produce delay using for loop then it works. :thinker:
#include <reg51.h>char uart_data;int i;
sbit led1 = P1^0;void serial_int_initialization();void SendKey (unsignedchar Key);void delay (unsignedint sec);void main(){
serial_int_initialization();while(1){
led1 =0;
delay(1);for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);
led1 =1;for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);for(i =0; i <20000; i++);
delay(1);}/* endless */}/** * FUNCTION_PURPOSE: serial interrupt, echo received data. * FUNCTION_INPUTS: P3.0(RXD) serial input * FUNCTION_OUTPUTS: P3.1(TXD) serial output */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 */
SendKey (uart_data);}else TI =0;/* if emission occur */}/* clear emission flag for next emission*/void serial_int_initialization()// Serial port initialization with timer-1 interrupt{
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 */
TH1 =0xE8;/* 2400 Bds at 22.1184MHz */// baud rate 19200bps,FD //baud rate 9600,FA
TL1 =0xE8;/* 2400 Bds at 22.1184MHz */
ES =1;/* Enable serial interrupt*/
EA =1;/* Enable global interrupt */
TR1 =1;/* Timer 1 run */}void SendKey (unsignedchar Key){
TI =0;
SBUF = Key;while(TI ==0);}//--------------------------- Delay Function -----------------------------void delay (unsignedint sec)// Timer function to produce a delay of 1 sec{unsignedint i;for(i=0; i<=50*sec; i++)//when multiplied by 50 it produces a delay of 1 sec{
TMOD=0x01;//This timer produces a delay of 20ms
TL0=0xFE;
TH0=0x6F;
TR0=1;while(TF0==0);
TR0=0;
TF0=0;}}
Thank you for your reply milan.
yes i am already using TMR0 for delay. I after initializing UART interrupt, i tried to run TMR0 but still it is not working.
Can you please elaborate it?
- - - Updated - - -
Thank you for your reply.
Yes i am already using Timer-0 for delay.
I tried to start TMR0 after UART interrupt but it is not working. Can you please elaborate it further?