8051 MCU baud rate calculation formula

Publisher:RadiantBlossomLatest update time:2024-04-10 Source: elecfans Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The 8051 microcontroller uses timer 1 working in mode 2 as the serial port baud rate generator. Its baud rate = (2smod /32) × (timer T1 overflow rate), where smod is PCON<7>, indicating whether the baud rate is doubled, and Fsoc is the system crystal size.

In the baud rate formula: T1 overflow rate = the inverse of the overflow period; overflow period = (256-TH1) × 12/Fosc;

Final formula:

Baud rate: Baud = (2smod × Fsoc) / (32 × 12 × (256-TH1))

We generally do not pay much attention to the calculation of the baud rate, but are concerned about the selected transmission speed (baud rate) to reversely calculate the initial value (TH1) of timer 1 (auto-reload mode), so we can derive the above formula to get the formula for TH1:

TH1=256-(Fsoc×2smod)/(12×32×Baud)

Below is a sample program for the serial port sending program, Shui Han wrote it down for your reference. (I used STC12C5A40S2 for debugging, in principle it can be successful on 8051 core microcontrollers such as STC89C5x and AT89C5x. Since the program is relatively simple, I did not try it, but it should be no problem)


#include "Reg52.H"

/****************************************************** ******************

Please calculate the maximum speed that the selected crystal can reach in advance. The baud rate cannot exceed the maximum speed.

(1) Double the baud rate (SMOD=1): Max_Baud = FOSC/12/16

(2) Baud rate is not doubled (SMOD=0): Max_Baud = FOSC/12/32

For example: 22.1184MHz crystal oscillator, when the baud rate is doubled, the maximum baud rate = 22118400/12/16 = 115200

*************************************************** *******************/

#define FOSC 22118400 //Oscillator frequency

#define BAUD 9600 //Baud rate

#define SMOD 1 //Whether to double the baud rate

#if SMOD

#define TC_VAL (256-FOSC/16/12/BAUD)

#else

#define TC_VAL (256-FOSC/32/12/BAUD)

#endif


typedef unsigned char uint8;

typedef unsigned int uint16;


code const char str1[] = "Ther string is transmitted from 80C51!rn";

code const char str2[] ​​= "Author: xqlu(at)ysu.edu.cnrn";


/***************Function declaration********************/

void InitUART(void);

void SendOneByte(uint8);

void SendrStr(const uint8 *ptr);


/********************Main function************************/

void main(void)

{

uint8 i=0;

InitUART();


while(str2[i]!='')

{

SendOneByte(str2[i++]);

}


SendrStr(str1);


while(1);

}


/****************Interrupt service function****************/

void UART_ISR(void) interrupt 4

{

uint8 RX_Data;

//Only respond to the "receive" interrupt, and erase it directly when the "send" interrupt comes

if(RI)

{

RI = 0; //The serial port interrupt flag cannot be cleared by itself and needs to be cleared manually

RX_Data=SBUF;

SendOneByte(RX_Data);

}

else

TI = 0; //Serial port interrupt is generated after the buffer data is sent

}


/****************Serial port initialization function*************/

void InitUART(void)

{

TMOD = 0x20;

SCON = 0x50;

TH1 = TC_VAL;

TL1 = TH1;

PCON = 0x80; // double the transmission rate

ES = 1;

EA = 1;

TR1 = 1;

}

/**************Serial port sending character function*************/

void SendOneByte(uint8 c)

{

ES = 0; //Disable sending interrupt

SBUF = c;

while(!TI);

TI = 0;

ES = 1;

}

/**************Serial port sending string function*************/

void SendrStr(const uint8 *ptr)

{

do

{

SendOneByte(*ptr);

}while(*ptr++!='');

}


Reference address:8051 MCU baud rate calculation formula

Previous article:51 single chip microcomputer principle and design scheme
Next article:Coexistence of Keil C51 and Keil MDK

Recommended ReadingLatest update time:2024-11-16 09:36

DHT11 temperature and humidity sensor driver programming based on C8051F410
This is also a little thing I made during the summer training. I bought some sensors online and spent a lot of money on them, so I used them to practice. The output pin of DHT11 needs to be pulled up with a resistor, which is really troublesome. The program is also not very good. If there is a problem with the conne
[Microcontroller]
Useful information! 8051 microcontroller's inversion method to scan the keyboard
The keyboard scan uses the inversion method to read the keys: first output zero level from the high 4 bits of port P2, and read the keyboard status from the low 4 bits of port P2; then output zero level from the low 4 bits of port P2, and read the keyboard status from the high 4 bits of port P2. Combining the results
[Microcontroller]
Useful information! 8051 microcontroller's inversion method to scan the keyboard
8051 Assembly Language LED Flowing Light
When writing programs in assembly language, there is no need to add header files, and the program files need to have ASM as the suffix. ORG 0000H //Assembly pseudo-instruction, which specifies where the next statement will start. This assembly pseudo-instruction is only used by the compiler when
[Microcontroller]
Design of C8051 microcontroller to realize multi-target ultrasonic ranging
Ultrasonic ranging sensors are widely used in industrial field ranging, mobile robot navigation and positioning due to their high measurement accuracy, fast response and low price. The commonly used method for ultrasonic ranging sensors is that one transmitter corresponds to one receiving head, and there are also mult
[Microcontroller]
Design of C8051 microcontroller to realize multi-target ultrasonic ranging
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号