This program is written to simulate the serial port hardware mechanism. When used, a timed interrupt can be set with a time interval of 1/4 baud rate. The receiving function is called once for each interrupt, and the sending function is called once for each 4 interrupts. However, the clock does not need to be fast for the microcontroller. You should know that the time interval of each BIT of the 9600 baud rate is 104us. The time for the microcontroller to push and pop the stack once is about 20us (standard 51-core 12M crystal). So the processing time must be considered clearly. Haha. The following program is placed in the timer interrupt program function
//Receiving part
sbit JieShou_D= ;//Define receiving port
uint8 DingShiJiShu, JieShou_h;//Timing count, receiving buffer
uint16 JieShou_T;//Receiving temporary register
bit KaiShi, JieShou_b;//Start receiving standard, receiving completion flag
void JieShou(void) //Receive function, receive one bit every 4 calls
{
if((KaiShi==0) && (JieShou_D==0)) //Serial start bit arrives
{
DingShiJiShu=0; //Start timing
countKaiShi=1;
JieShou_T=0xffff; //Receive temporary register set to all 1s
}
else if((KaiShi==1) && (DingShiJiShu==1)) //2nd call, serial data sampling time arrives
{
JieShou_T >>= 1;
if(JieShou_D) JieShou_T |=0x8000;
}
else if(JieShou_T & 0x807f ==0x803f) //Receive completedJieShou_T=1xxx_xxxx_x011_1111
{
KaiShi=0;
DingShiJiShu=0;
JieShou_h = JieShou_T >> 7; //Shift right 7 bits to get serial data
JieShou_b=1;
}
if(KaiShi)
{
DingShiJiShu++; //Count +1 only when receiving
DingShiJiShu &=0x03; //Receive one bit every 4 calls
}
}
//Sending part
sbit FaSong_D= ;//define sending port
uint16 FaSong_h; //sending buffer
bit FaSong_b; //sending completion flag
void KaiShiFaSong(uint8 fs) //Set the data to be sent and start sending
{
FaSong_h = fs;
FaSong_h <<= 1;
FaSong_h |= 0x0200; //FaSong_h=0000_001x_xxxx_xxx_0
FaSong_b=0; //Send flag = 0 means sending is in progress
}
void FaSong(void) //Sending function, one bit is sent each time it is called
{
if(FaSong_h) //Sending is not completed
{
if(FaSong_h & 0x0001)
FaSong_D=1;
else FaSong_D=0;
FaSong_h >>= 1;
}
else
FaSong_b =1; //Sending completed flag = 1 means sending completed
}
Previous article:Simulation and production of music player based on single chip microcomputer
Next article:Detailed explanation of the role of volatile definition in single chip microcomputer
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- The power plane is clearly more than adequate, so why is the DC voltage drop so critical?
- Asynchronous Timing Design in ASIC.pdf
- [Repost] "Very Silly" Circuit Problem
- E840-DTU device connection to Alibaba Cloud test
- Interface corresponding to SNVS_TAMPER3 signal
- Looking for a good and available domestic accelerometer
- nRF52832-QFAA Hardware Reference Design
- How to design the MCU+8266 program structure
- 【RPi PICO】Snapshot of the RP2040 chip
- Why does TIM1_CH3N not output PWM after configuring PWM in STM32F103C8T6?