ARM Technical Support Knowledge Articles

CALCULATING TIMER SETTINGS FOR SERIAL I/O

Applies to: C51 C Compiler

Answer


Information in this article applies to:


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

Article last edited on: 2005-09-22 12:30:27

Rate this article

[Bad]
|
|
[Good]
Disagree? Move your mouse over the bar and click

Did you find this article helpful? Yes No

How can we improve this article?

Link to this article
Copyright © 2011 ARM Limited. All rights reserved. External (Open), Non-Confidential