# find TH1 in AT89s52 micro controller in serial communication

I want to establish serial communication between an 8-bit microcontroller, AT89S52, and a PC. I am using a crystal oscillator of 8 MHz frequency. For baud rate calculation I have used the following formula:

 Fbaud = (2^SMOD * Fosc)/(12*32*(256-TH1))


In my case

 SMOD = 1;
Fbaud  =9600; // baud rate
Fosc = 8MHz   // oscillator frequency


So using formula TH1 = 251.659722222

I have used TH1 = 251 i.e. 0xFB and also 252 i.e. 0xFC. In both cases I am getting garbage values on Hyper terminal.

I have used following code::

#include <REGX52.h>
#include <stdio.h>
void serial_ini();
void serial_ini()
{
SCON = 0x50; // mode 1 , 8 bit UART, enanle receiver
TMOD = 0x20; // timer 1 mode 2(auto reload mode)
TH1 = 0xFB ; // baud rate 9600 // crystal = 8.000 mhz..
TR1 = 1; // Timer1 run
TI = 1;
}

void main()
{
serial_ini();
while(1)
{
SBUF = 0x41;
while(TI==0);
TI=0;
}
}


Please suggest to me how to calculate TH1 for AT89S52 microcontroller.

• I'm not familiar with the 8952 but looking at some of my old 8752 code, I see that I have set the MSB of PCON with the comment /* Use timer 1 for baud rate. */ (which may or may not be relevant to your device). – MikeJ-UK May 22 '13 at 9:58
• @MikeJ-UK I typically find that the number after the "8" indicates memory architecture with the core (i.e. 51/52) being common. 8052 = ROM-less, 8752 = OTP/EPROM, 8952 = Flash. The 89 series also tend to be exclusively Atmel though I've seen one or two that were not. So your comment does apply to the chip in question. But it may or may not apply depending on the OP's desired mode of operation. – embedded.kyle May 22 '13 at 15:25
• Check out book by Mazidi on 8051! It has everything! – Swanand May 28 '13 at 19:17

All of the following information is available in Atmel 8051 Microcontrollers Hardware Manual which is referenced in Section 9 of the AT89S52 datasheet as the place to look for information on Timers 0 and 1.

Note: I'm assuming what you have in the comments is what you are trying to do.

First off, your formula is wrong. If you're trying to use Mode 1, SMOD doesn't enter into it. Even so, SMOD is in PCON which you are not setting (which MikeJ-UK mentioned).

The baud rate in Mode 0 is fixed:

The baud rate in Mode 2 depends on the value of bit SMOD in Special Function Register PCON.

If SMOD = 0 (which is its value on reset), the baud rate is 1/64 the oscillator frequency. If SMOD = 1, the baud rate is 1/32 the oscillator frequency.

In the 80C51, the baud rates in Modes 1 and 3 are determined by the Timer 1 overflow rate. In case of Timer2, these baud rates can be determined by Timer 1, or by Timer 2, or by both (one for transmit and the other for receive).

Emphasis mine.

Next, your SCON does not match the comment:

You have:

SCON = 0x50

Which equates to:

 FE SM0 SM1 SM2 REN TB8 RB8 TI  RI
0   1   0   1   0   0   0   0   0


So you're in Mode 2, 9-bit UART with the baud rate given as:

FCPU PERIPH /32 or /16

And serial reception disabled.

And then you've got:

TI = 1

Which you shouldn't do. You clear the interrupt flag. It's set by hardware.

Figure out exactly what you want to achieve and have a good look through those manuals. You'll probably need to rework your formula, change SCON and then set TH1 and TL1.