Technical Support

C51: CALCULATING TIMER SETTINGS FOR SERIAL I/O


Information in this article applies to:

  • C51 Version 5.50

QUESTION

Where can I find examples of source code for compilation-time calculation of 8052 timer setting for timeout and serial communication?

ANSWER

The following code calculates and sets the baudrate for a standard 8051. Note that TCLK is defined as 12000000UL in this example.

#pragma disable
void com_baudrate (unsigned baudrate)
{
/*------------------------------------------------
Clear transmit interrupt and buffer.
------------------------------------------------*/
TI = 0;                 /* clear transmit interrupt */
t_in = 0;               /* empty transmit buffer */
t_out = 0;
t_disabled = 1;         /* disable transmitter */

/*------------------------------------------------
Set timer 1 up as a baud rate generator.
------------------------------------------------*/
TR1 = 0;                /* stop timer 1 */
ET1 = 0;                /* disable timer 1 interrupt */
PCON |= 0x80;           /* 0x80=SMOD: set serial baudrate doubler */
TMOD &= ~0xF0;          /* clear timer 1 mode bits */
TMOD |= 0x20;           /* put timer 1 into MODE 2 */
TH1 = (unsigned char) (256 - (TCLK / (16L * 12L * baudrate)));
TR1 = 1;                /* start timer 1 */
}

MORE INFORMATION

Similar code may be found in the Interrupt-Driven Serial I/O example for printf (INTSIO2.ZIP).

SEE ALSO

Last Reviewed: Thursday, September 22, 2005


Did this article provide the answer you needed?
 
Yes
No
Not Sure