Of course, the biggest problem in generating the 115200 baud rate discussed below is that its clock cycle is equal to 12 machine cycles (12T mode)
If timer T1 is to be used as a baud rate generator, it must work in 8-bit auto-reload mode and the interrupt of T1 must be disabled!
Another important register is PCON. The highest bit SMOD can double the baud rate! Of course, due to the limitation of the 8-bit auto-reload mode, it is not as we imagined. Since it can generate a baud rate of 9600, then the doubled baud rate is 115200, right? !
Too young, Too simple!
Let’s look at two formulas first.
When the baud rate is not doubled using timer T1, the baud rate is calculated as follows:
TH1 = TL1 = 256 - Crystal value/12/2/16/Baud rate
The calculation formula after doubling the baud rate is:
TH1 = TL1 = 256 - Crystal value/12/16/Baud rate
When we do not double the baud rate (let TH1 = 0xFF), the maximum baud rate generated is 28800
The maximum baud rate after doubling is 57600 (exactly twice that of 28800)
So for the enhanced 51 MCU 89C52, T0 cannot be used as a baud rate generator, and T1 does not seem to meet the requirements. Is there no way? Don't forget, it also has T2!!!
Let’s first take a brief look at T2 and its related registers!
Let’s look at the main ones first, what we are going to use!
① As a baud rate generator
When the TCLK and RCLK bits of T2CON are 0 (default), the baud rate of the serial port send and receive is provided by Timer 1; when set to 1, it is provided by Timer 2. One can pass through Timer 1 and the other through Timer 2, so that different baud rates can be obtained when sending and receiving.
Note: When Timer 2 is used as a timer, the increment frequency is 12 times the crystal frequency, and when Timer 2 is used as a baud rate generator, its increment frequency is 2 times the crystal frequency.
When Timer 2 is used as a baud rate generator, TH2 overflow will not set TF2, so there is no need to disable Timer 2 interrupt. If the EXEN2 bit is set, T2EX can be used as an additional external interrupt.
When Timer 2 is used as a baud rate generator, do not read or write TH2 and TL2. You can read the trap register, but do not write it. When accessing the trap register of Timer 2, the timer should be turned off (TR2 is cleared to 0).
Baud rate = Mode 1 and Mode 3 baud rate = (oscillator frequency/32) * [65535-(RCAP2H, RCAP2L)]
The initial value corresponding to 115200
#include
#include
void init_com( void )
{
SCON=0x50;
TH2=0xFF;
TL2=0xFD;
RCAP2H=0xFF;
RCAP2L=0xFD;
/*****************/
TCLK=1;
RCLK=1;
C_T2=0;
EXEN2=0;
/*****************/
TR2=1 ;
IF = 1;
}
void main()
{
init_com();
while(1)
{
printf("Hello World\n");
}
}
Then select the baud rate of 115200 again to test the phenomenon
Emmmmm, originally this article would be finished here, but the sudden ending seems a bit unsatisfactory. Yes, I haven’t finished what I wanted to say, let’s continue to build it, and together we can comprehend the magical T2 and explore what other magical operations there are.
Let’s continue to look at T2.
By the way, it has its own timing function.
In 16-bit auto-reload mode, Timer 2 can be configured as a timer/counter through the C/T2 bit. Depending on the setting or clearing of the external enable flag EXEN2, there are two cases:
Timer 2 is a 16-bit normal timer with automatic reload. The reload value is provided by the trap register and only needs to be preset. It can be used in situations where high timing accuracy is required and the timing time is long (16 bits).
A1, T2MOD = 0x00 (DCEN = 0; default);
Compared with the previous case, the 16-bit automatic reload can be triggered by either a negative transition of the external T2EX or an overflow, and an interrupt can be generated.
A2、T2MOD=0x01(DCEN=1);
At this time, T2EX is allowed to control the direction of counting; when T2EX=0, the reload value is 0FF and 0FF, and when the count is equal to the value stored in the trap register, TF2 is set to generate an interrupt. When T2EX=1; the automatic reload value is the value in the trap register, and TF2 is set to generate an interrupt when overflow occurs.
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode
/* define SFR */
sbit ET2 = IE^5;
sfr T2CON = 0xc8; //timer2 control register
sbit TF2 = T2CON^7;
sbit TR2 = T2CON^2;
sfr T2MOD = 0xc9; //timer2 mode register
sfr RCAP2L = 0xca;
sfr RCAP2H = 0xcb;
sfr TL2 = 0xcc;
sfr TH2 = 0xcd;
sbit TEST_LED = P1^0; //work LED, flash once per second
/* define variables */
WORD count; //1000 times counter
//-----------------------------------------------
/* Timer2 interrupt routine */
void tm2_isr() interrupt 5 using 1
{
TF2 = 0;
if (count-- == 0) //1ms * 1000 -> 1s
{
count = 1000; //reset counter
TEST_LED = ! TEST_LED; //work LED flash
}
}
//-----------------------------------------------
/* main program */
void main()
{
RCAP2L = TL2 = T1MS; //initial timer2 low byte
RCAP2H = TH2 = T1MS >> 8; //initial timer2 high byte
TR2 = 1; //timer2 start running
ET2 = 1; //enable timer2 interrupt
EA = 1; //open global interrupt switch
count = 0; //initial counter
while (1); //loop
}
②Of course, there is also a clock output function
You can refer to here to see other clock outputs
52 series MCU, timer/counter 2 can be set to output clock through T2 (p1^0) pin.
In addition to being a normal I/O port, P1^0 can also be used as the external count input and clock signal output of timer 2.
When C/T2=0 and the T2OE bit of T2MOD is 1, timer 2 can be selected as the clock signal generator and automatically loaded with the initial value. Setting formula:
Clock signal output frequency = (oscillator frequency/4) * (65535-N)
1
2
3
In clock output mode, the counter overflow will not generate an interrupt request. This function is equivalent to Timer 2 being able to be used as a baud rate generator and a clock generator at the same time.
Because the external interrupt is not temporarily used at this time, if there is no conflict in the settings, it may also be able to respond to the external signal introduced by T2EX at the same time. This is just a guess and has not been proven by experiments, haha*
Note: The microcontroller has the function of counting external pulse signals, but there are requirements: The maximum frequency of the counting pulse = the frequency of the oscillator / 24
And to ensure that a given level can be sampled once before the level changes, this level must be maintained for at least one machine cycle.
#include "reg51.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
//-----------------------------------------------
/* define constants */
#define FOSC 11059200L
#define F38_4KHz (65536-18432000/4/38400)
/* define SFR */
sfr T2CON = 0xc8; //timer2 control register
sbit TF2 = T2CON^7;
sbit TR2 = T2CON^2;
sfr T2MOD = 0xc9; //timer2 mode register
sfr RCAP2L = 0xca;
sfr RCAP2H = 0xcb;
sfr TL2 = 0xcc;
sfr TH2 = 0xcd;
sbit T2 = P1^0; //Clock Output pin
//-----------------------------------------------
/* main program */
void main()
{
T2MOD = 0x02; //enable timer2 output clock
RCAP2L = TL2 = F38_4KHz; //initial timer2 low byte
RCAP2H = TH2 = F38_4KHz >> 8; //initial timer2 high byte
TR2 = 1; //timer2 start running
EA = 1; //open global interrupt switch
while (1); //loop
}
Actually, um, although 51 is very low, but the limited resources are used well. With more and bigger resources, it is more reasonable to use them. That is the real skill, right? . .
Yes, it is like that..
By the way, what other methods are there besides T2?
1. Replace 11.0592M with 22.1184M crystal. Then when TH1=0xff, it can generate 115200 baud rate.
2. Change to an IT microcontroller... Haha...
Previous article:8051 Assembly ASM
Next article:No timer or assembly language is needed, only C language is used to achieve accurate delay
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Analysis of the application of several common contact parts in high-voltage connectors of new energy vehicles
- Wiring harness durability test and contact voltage drop test method
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- From probes to power supplies, Tektronix is leading the way in comprehensive innovation in power electronics testing
- Sn-doped CuO nanostructure-based ethanol gas sensor for real-time drunk driving detection in vehicles
- Design considerations for automotive battery wiring harness
- Do you know all the various motors commonly used in automotive electronics?
- What are the functions of the Internet of Vehicles? What are the uses and benefits of the Internet of Vehicles?
- Power Inverter - A critical safety system for electric vehicles
- Analysis of the information security mechanism of AUTOSAR, the automotive embedded software framework
- Introduction to flow principle in automatic monitoring system of oil and gas recovery in oil stations
- Op amp
- R8C uses RD timer and needs to use mov instruction to write trdstr register
- Capacitors and inductors
- If you don’t pay attention to the wiring, you will have to rework it sooner or later
- Purgatory Legend-The Battle between Pre-Simulation and Post-Simulation
- EEWORLD University Hall----labview2016
- Simulation and Calculation in PCB Circuit Design
- [Qinheng RISC-V core CH582] Incomplete evaluation summary
- Read the good book "Operational Amplifier Parameter Analysis and LTspice Application Simulation" 04 Amplifier Noise Evaluation Case