Hi, I'm trying to simulate working of a UART. When I write into SBUF, the TI flag doesn't go high even though I have enabled interrupts. Is there any way I can simulate it using SOUT VTREG. can't find example how to use SOUT.
I looked into the examples given by Keil where it uses some tbuf and rbuf. I want something very simple. Infact to my surpise the same code that I have pasted works if I introduce a delay using a for loop. (again the delay has to be changed for different baud rate). I am not sure what I am missing but it would be really very nice if you could send me a very simple serial example ie.in the same manner I have posted which I could use as a reference. Thanks.
The same code which I posted above, the same when modified as shown below works. The only difference between the two which brings the change is the for loop inside the main(). I am not sure why this delay is needed but then with this delay I am able to see 'Y' getting repeatedly displayed on the com port. Any idea as to why this delay is needed or what am I doing wrong? If I could get the same code modified with corrections then it would help in understanding. ------------------------------------------- #include <AT89S52.H> void serial_int (void) interrupt 4 { if (RI == 1) /* it was a receive interrupt */ { RI = 0; /* clear the received interrupt flag */ } else if (TI == 1) /* otherwise, assume it was a transmit interrupt */ { TI = 0; /* clear the transmit interrupt flag */ } } void main(void) { SCON = 0x50; /* mode 1, 8-bit uart, enable receiver */ TMOD = 0x20; /* timer 1, mode 2, 8-bit reload */ TH1 = 0xFD; /* reload value for 2400 baud */ ET0 = 0; /* we don't want this timer to make interrupts */ TR1 = 1; /* start the timer */ TI = 1; /* clear the buffer */ ES = 1; /* allow serial interrupts */ EA = 1; /* enable interrupts */ while (1) { unsigned int i; for (i = 0; i < 120; i++) {;} /* delay */ SBUF = 'Y'; } } ---------------------------------------
You need to choose whether to use interrupts or a polled approach. You are trying to mix the two - this will only make your life difficult.