| ||||||||
Technical Support Support Resources Product Information | C51: CALCULATING TIMER SETTINGS FOR SERIAL I/OInformation in this article applies to:
QUESTIONWhere can I find examples of source code for compilation-time calculation of 8052 timer setting for timeout and serial communication? ANSWERThe 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 INFORMATIONSimilar code may be found in the Interrupt-Driven Serial I/O example for printf (INTSIO2.ZIP). SEE ALSOLast Reviewed: Thursday, September 22, 2005 | |||||||
| ||||||||