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.
Best Answer
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 inPCON
which you are not setting (which MikeJ-UK mentioned).Emphasis mine.
Next, your
SCON
does not match the comment:You have:
SCON = 0x50
Which equates to:
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 setTH1
andTL1
.