Keil Logo

C51: Calculating Timer Settings for Serial I/O


Information in this article applies to:

  • C51 Version 5.50 and later

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, February 25, 2021


Did this article provide the answer you needed?
 
Yes
No
Not Sure
 
  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.