当前位置:网站首页>MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
MCU serial port interrupt and message receiving and sending processing -- judge and control the received information
2022-06-13 00:44:00 【Hao Yuehua】
Catalog
The problem of making trouble by yourself this time :( An experiment made by myself )
The problem of making trouble by yourself this time :( An experiment made by myself )
With 51 For single-chip computer , Timer 2 As a serial port 1 To conduct serial port experiments , Check the following data ( Any data )
"hello"--1
"yzh666"--2
"aqawaer"--3
Realization effect :
After receiving these message fields, the single-chip microcomputer judges and displays the number of the message in the first position of the nixie tube , Take a look at the real-time performance of the serial port .
Preliminary foundation :
There are many baud rates for serial ports , The unit is bits per second , With 9600bps For example , In my submission 9600 Bitwise s, If there is no verification 8 Digit bit , A start bit and a stop bit form a data frame ( common 10 position ),
Start bit :1
Data bits :8
Stop bit :1
Check bit :0
Then every s Can accept 960 A packet , Every eight bits is a byte , That's no s Can accept 960 Bytes ( That's one char Or is it unsigned char Data of type ), Then it is equivalent to every 1.041ms Accept a character .
Serial interrupt : Every time you send or receive data TI or RI Set as 1 An interrupt is triggered ( Hardware placement ), Therefore, software reset is required after triggering the interrupt ( Set as 0), Priority is lower than timer interrupt . Serial port interrupt is generally used to receive data .
Implementation steps
Use an array cache to receive data , Set a flag to indicate whether there is data rf , The time interval before two data transmissions rt , Set up a 1ms A timer that generates an interrupt 1, Each timer 1 Enter interrupt judgment RI:RI by 1 be rt by 0( It means that there are still data being accepted ),RI by 0( be rt Start timing , During the timing, only RI by 1 Again from 0 Start counting , Because each time the serial port accepts a data frame, it will be set by the software 0), When rt>40 when , explain (RI by 0 And 40ms If no data is received within this period, it is judged that the data acceptance is completed , The data sent and accepted has been cleared rf ,rt) Here is 40ms I just set it up myself .
After receiving the data , use C Library function strcmp(char *src, char *dst), Add the header file of the library "string.h"
strcmp(Rbuf,"awaqaer\r\n")==0 To determine whether the corresponding data has been received
experimental result :
I set it as 1000ms Automatically send at intervals of "hello","yzh666","awaqaer" A satisfactory result has been achieved :
First, the serial port :

The effect on the board :
Judge the serial number corresponding to the three pieces of data :

Main code
Timer 1 And serial port code
void TIME1() interrupt 3{
smg_play(DT[s],s++);
if(s>7)s=0;
smgtime++;
time++;
if(rf){
if(!RI){
if(rt++==40){
// It is considered that the primary data transmission is completed
Rbuf[i]='\r';
Rbuf[i+1]='\n';
Rbuf[i+2]='\0';
if(strcmp(Rbuf,"yzh666\r\n")==0)DT[0]=1;
else if(strcmp(Rbuf,"hello\r\n")==0)DT[0]=2;
else if(strcmp(Rbuf,"awaqaer\r\n")==0)DT[0]=3;
else DT[0]=0;
SendString(Rbuf);
i=0;
rf=0;
}
}
else {
rt=0;
}
}
}
void Uart1() interrupt 4{
if(RI){
RI=0;
if(!rf){
rf=1;
Rbuf[i++]=SBUF;
}
else {
Rbuf[i++]=SBUF;
}
}
}There are still defects
When the interval between sending data is 300ms There is no problem in judging the above data , But less than 300ms, I measured 200ms There are two messages that are accepted as the same message , I set RI==0 The interval is 10( The code says 40) The same is true for the interval between messages . In my understanding ,9600 Of bps, The transmission rate is 1 Data frames /1.041ms, wait for 40 A byte of time is about the same 40ms, The running time of the program can be ignored , Serial port and timer preemption cpu And the resulting error is not big , because 1ms Yes cpu It's been a long time .
边栏推荐
- Win10 home vs pro vs enterprise vs enterprise LTSC
- 1. Google grpc framework source code analysis Hello World
- OceanBase 雄踞墨天轮2021年度中国数据库魔力象限领导者
- 深度学习每周期的步数多少合适?
- [GYCTF2020]Ezsqli --BUUCTF
- MySQL lpad() and rpad() concatenate string functions with specified length
- Target recognition gadget
- 牌好不好无法预料
- 硬(磁)盘(二)
- Kali system -- dnsrecon for DNS collection and analysis
猜你喜欢

Maya modeling VI

Stm32f4 development of DMA transmission to GPIO port

The origin of MySQL in bedtime stories

Target recognition gadget

Another year 1024, happy programmer's Day!
![[imx6ull] video monitoring project (USB camera +ffmepeg)](/img/f9/1a7b68083b52c973336db9044b52c2.jpg)
[imx6ull] video monitoring project (USB camera +ffmepeg)

Arduino controls tb6600 driver +42 stepper motor

Mysql批量插入数据时如何解决重复问题?

Some basic design knowledge

JPA execution failed in scheduled task -executing an update/delete query transactionrequiredexception
随机推荐
Card constructions -- two points
[gxyctf2019] no dolls -- detailed explanation
Opencv face recognition of ros2 foxy~galactic~humble
.net core 抛异常对性能影响的求证之路
Set sail
天津银行周传凯:从 0 到 1,我的分布式数据库落地经验谈
硬(磁)盘(一)
pytorch是什么?解释pytorch的基本概念
[MRCTF2020]Ez_ bypass --BUUCTF
Arduino interrupt
为什么磁盘分区的时候,第一个分区前面总有一段空间(63或者2048个扇区)
Build your own PE manually from winpe of ADK
Five mock technologies of go
Arduino control tm1637 common positive four digit nixie tube
A simple deadlock example
通过抓包下载钉钉直播回放
[LeetCode]26. Removes duplicates from a sorted array thirty-three
What are the conditions of index invalidation?
Programming training 1
三角波与三角波卷积