This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

help: how to setup serial port baud rate using T2

hi, all. I am a new comer to 8051.
It seems that I have problem in generating the baud rate of 19200 for serial port using timer 2. The uC is AT89C52. here is my code:

#include <at89x52.h>
#include <stdio.h>

void SPortISR (void) interrupt 4
{
unsigned char i = 0;
putchar (i);              /* output character i*/
TI = 0;                   /* clear flag */
}

void main (void){
T2CON = 0x34;             /* Set T2 Mode 1  */
RCAP2H = 0xFF;
RCAP2L = 0xEC;            /* RCAP2 = 65516  */
TH2 = RCAP2H;
TL2 = RCAP2L;

SCON = 0x50;              /*set serial port mode 1 */
ES = 1;                   /*Enable Serial Port interrupt*/
EA = 1;                   /*Enable Global Interrupt */
}
Can anyone figure out the problem? How to check the serial port output?
I would also like to know if there is any similar example.
thanks a lot.

Parents
  • The default implementation of putchar() isn't intended to be used with interrupt driven serial comms. It waits for TI to be set before transmitting a character, but your ISR clears TI, so putchar() hangs.

    If you want to use interrupt driven serial comms with the standard library I/O functions you need to write your own versions of _getkey() and putchar(). The source for Keil's implementations is in the C51\LIB directory. By the way, putchar() translates linefeed characters (0x0a) into CRLF sequences (0x0d, 0x0a).

    Yes, you can use SBUF=i in C code, this is exactly what putchar() does.

    There is an 'interrupt driven serial comms' example somewhere on this site, I suggest you track it down and have a look.

    The basic difference between interrupt driven and non-interrupt driven comms is that with interrupt driven comms the RI and TI flags cause the serial ISR to be called, this will 'preempt' any code outwith the ISR that is looking at these flags. In either case you must reset the flags in software.

    Stefan

Reply
  • The default implementation of putchar() isn't intended to be used with interrupt driven serial comms. It waits for TI to be set before transmitting a character, but your ISR clears TI, so putchar() hangs.

    If you want to use interrupt driven serial comms with the standard library I/O functions you need to write your own versions of _getkey() and putchar(). The source for Keil's implementations is in the C51\LIB directory. By the way, putchar() translates linefeed characters (0x0a) into CRLF sequences (0x0d, 0x0a).

    Yes, you can use SBUF=i in C code, this is exactly what putchar() does.

    There is an 'interrupt driven serial comms' example somewhere on this site, I suggest you track it down and have a look.

    The basic difference between interrupt driven and non-interrupt driven comms is that with interrupt driven comms the RI and TI flags cause the serial ISR to be called, this will 'preempt' any code outwith the ISR that is looking at these flags. In either case you must reset the flags in software.

    Stefan

Children