当前位置:网站首页>Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
Preparing for the Blue Bridge Cup Day11__ Basic operation of serial port communication
2022-06-12 23:22:00 【The God of C language】

One , Serial port communication
There are many kinds of serial communication. Here we choose UART,UART It's a full duplex , Asynchronous serial communication , What are full duplex and asynchronous ?
What we use here is asynchrony. We need to agree on the same communication rate , This communication rate is called baud rate , Refers to the number of bits per second of data transmission , As we often use it 9600 Baud rate refers to the transmission per second 9600 position . So we need to specify a baud rate , The baud rate is determined by the timer 1 To produce , So we turn on the timer 1, By giving TH1,TL1 The initial value determines the baud rate . How many values need to be assigned can be used STC-isp To calculate . The transmitted or received data is placed in SUBF.
Two , Code section
#include "reg52.h"
void Send(unsigned char t);
sfr AUXR=0X8E;
void Uart()
{
TMOD=0X20;
TL1=0XFD;
TH1=0XFD;
TR1=1;
AUXR=0X00;
SCON=0X50;
EA=1;
ES=1;
}
unsigned char urdat;
void ServiceUart() interrupt 4
{
if(RI==1)
{
urdat=SBUF;
RI=0;
Send(urdat+1);
}
}
void Send(unsigned char dat)
{
SBUF=dat;
while(TI==0);
TI=0;
}
void main()
{
Uart();
Send(0x5a);
Send(0xa5);
while(1);
}At the beginning, we need to configure the additional auxiliary registers of the Blue Bridge Cup SCM ,AUXR The address is 0x8e. In the interrupt function, we enable the timer 1 Perform baud rate conventions , Need to be right TOMD Register each port by a certain knowledge

Here we use auto reload 8 Bit timer , therefore TMOD=0x20,TR1=1, Start timer . Then on AUXR Carry out a cleaning 0. Another register bit is required to enable serial communication SCON.

Then there is the writing of service functions ,interrput It should be followed by 4, The following rules .

The topic requires that the receiving adopts the interrupt mode , So we need to use yo... In the service function if Function to check whether the completion data is received ( After receiving the complete data RI I'll set 1, And it needs to be initialized manually ,TI Empathy ) So after receiving, the data is stored in SBUF We use it inside urdat Go ahead SBUF Take the data out of it , Yes RI Set up 0, Add... To the received data 1 after , adopt Send The function is sent to the upper computer . that Send It is necessary to realize the function of sending data to the upper computer , Put the data to be sent into SBUF Inside , use while Function to check whether the sending is completed , After sending, it is necessary to TI Set up 0.
边栏推荐
- PyTorch常用参数初始化方法:【均匀分布、正态(高斯)分布、Xavier、kaiming、正交矩阵、稀疏矩阵、常数、单位矩阵、零填充】
- 2022 heavyweight: growth law - skillfully use digital marketing to break through enterprise difficulties
- 应用最广泛的动态路由协议:OSPF
- Theory + practice will help you master the dynamic programming method
- [web technology] 1348- talk about several ways to implement watermarking
- Hostvars in ansible
- For product managers, which of the two certificates, PMP and NPDP, is more authoritative?
- iShot
- Dix points de défense clés dans les exercices d'attaque et de défense détaillés
- About three-tier architecture and MVC
猜你喜欢
![[opencv learning] use the Tesseract OCR movement to recognize numbers](/img/ed/92e5261c9abda4e79d58e9404d3aee.jpg)
[opencv learning] use the Tesseract OCR movement to recognize numbers

Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation

MySQL基础篇视图的总结

The annual salary of 500000 is one line, and the annual salary of 1million is another line

Record 5 - the serial port of stm32f411ceu6 realizes the sending and receiving of fixed length data and variable length data

Shardingsphere-proxy-5.0.0 deployment table implementation (I)

The shutter library recommends sizer to help you easily create a responsive UI

Dry goods sharing | BitSet application details

80 lines of code to realize simple rxjs

Deep feature synthesis and genetic feature generation, comparison of two automatic feature generation strategies
随机推荐
Design of traceid in the project
Lua loop statement
Leetcode 890 finding and replacing patterns [map] the leetcode path of heroding
Model over fitting - solution (II): dropout
The programmer has worked for 7 years. At the age of 31, he has no choice but to deliver takeout. I really don't want you to go through his journey again
启牛帮开通的股票账户是安全可信的吗?
ImageView grayed, reflected, rounded, watermarked
【LeetCode】102. Sequence traversal of binary tree
The development trend of digital collections!
China's new generation information technology industry "14th five year plan" special planning and innovation strategic direction report 2022 ~ 2028
PyTorch常用参数初始化方法:【均匀分布、正态(高斯)分布、Xavier、kaiming、正交矩阵、稀疏矩阵、常数、单位矩阵、零填充】
Summary of MySQL foundation view
Lua conditional statement
Redis realizes SMS verification code login
OpenCV源代码编译
Colab tutorial (super detailed version) and colab pro/colab pro+ usage evaluation
Is the stock account opened by qiniu Gang safe and reliable?
Mysql concat_ WS, concat function use
Abstract methods and abstract classes
【LeetCode】209. Minimum length subarray