shoaib88
Newbie level 3
- Joined
- Oct 9, 2014
- Messages
- 3
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 32
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:
can you please help me,
here is my code.
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:
can you please help me,
here is my code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 #include <reg51.h> char uart_data; int i; sbit led1 = P1^0; void serial_int_initialization(); void SendKey (unsigned char Key); void delay (unsigned int 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 (unsigned char Key) { TI = 0; SBUF = Key; while (TI == 0); } //--------------------------- Delay Function ----------------------------- void delay (unsigned int sec) // Timer function to produce a delay of 1 sec { unsigned int 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; } }
Last edited by a moderator: