当前位置:网站首页>Serial port data frame
Serial port data frame
2022-07-04 22:39:00 【Let everything burn】
List of articles
Mission requirements
Ideas
1 a5 01 b4 It works 01
2 a5 a5/b4 b4 It works a5/b4
3 01 b4 a5 01 Report errors
4 a5 01 02 b4 Report errors
5 Random invalid a5 01 b4 Random invalid It works 01
Valid data is the useful data you need
Frame head The end of the frame is fixed
Invalid data does not conform to the frame header + Valid data + Three byte format at the end of the frame
A byte is decimal 0~255 That's hexadecimal 0x00 To 0xff
A string of data of any length , As long as there are three bytes in it Frame head + One byte of data + Frame tail That is valid data
Data length mismatch , The header or footer of the frame does not match , Are invalid data
Give you some directions , The interrupt service function of the serial port will be called every time the serial port receives a byte , Get one byte per interrupt , But the task requires judging a multi byte data frame , Therefore, it is necessary to save the received data first
Use static variables static Or global variables save data temporarily , Interrupts must be used , If you have spare power to learn, you can use dma
Inquiry serial port reception is meaningless
The main serial port used is usart1 28-33
It uses 30-31
TX Sending port RX Accept port
Query interrupted disable
enable
To accept multiple groups of data, you need to define an array , Then put the data into the array at one time
Now we can only realize the first four situations :
Completion code :
main.c
int main (void){
// The main program
delay_ms(500); // Wait for other devices to be ready when powered on
RCC_Configuration(); // System clock initialization
// RELAY_Init();// Relay initialization
I2C_Configuration();//I2C initialization
OLED0561_Init(); //OLED initialization
OLED_DISPLAY_8x16_BUFFER(0," YoungTalk "); // display string
// OLED_DISPLAY_8x16_BUFFER(3," True "); // display string
LED_Init();//LED initialization
USART1_Init(115200); // Serial initialization , Write baud rate in the parameter
USART1_RX_STA=0xC000; // The initial value is set to enter , That is, a welcome message is displayed
while(1){
if(USART1_RX_STA&0xC000){
// If the flag bit is 0xC000 Indicates that the received data string is complete , Can handle .
if((USART1_RX_STA&0x3FFF)==0){
// A separate enter key displays the welcome message again
printf("\033[1;47;33m\r\n"); // Set the color ( Refer to HyperTerminal use )
// printf(" 1y-- open LED1 The lamp 1n-- Turn off LED1 The lamp \r\n");
// printf(" 2y-- open LED2 The lamp 2n-- Turn off LED2 The lamp \r\n");
printf(" Please enter the control command , Press enter to execute ! \033[0m\r\n");
}else if((USART1_RX_STA&0x3FFF)==6 && USART1_RX_BUF[0]=='5' && USART1_RX_BUF[1]=='a'&&USART1_RX_BUF[4]=='b'&&USART1_RX_BUF[5]=='4'){
// Judge whether the data is 2 individual , Is the first data “1”, Is the second one “y”
OLED_DISPLAY_8x16_BUFFER(1," TRUE:");
OLED_DISPLAY_8x16(1,8*8,USART1_RX_BUF[0]);// Display valid data
OLED_DISPLAY_8x16(1,9*8,USART1_RX_BUF[1]);//
OLED_DISPLAY_8x16(1,10*8,USART1_RX_BUF[2]);//
OLED_DISPLAY_8x16(1,11*8,USART1_RX_BUF[3]);//
OLED_DISPLAY_8x16(1,12*8,USART1_RX_BUF[4]);//
OLED_DISPLAY_8x16(1,13*8,USART1_RX_BUF[5]);//
printf(" The order is valid !\r\n");
}else{
// If none of the above is , That is, the wrong instruction .
OLED_DISPLAY_8x16_BUFFER(1," FALSE: ");
printf(" Command error !\r\n");
}
USART1_RX_STA=0; // Clear the serial port data flag 0
}
}
}
This method only fixes the length of three bytes , Then the first and last frames are processed as the first and last frames , If it does not conform to the data length and the head and tail matching , It's all wrong .
The interrupt function uses the original judgment to enter
In the interrupt, the original judgment returns , Because the super terminal is used in the yangtao video , He doesn't have the send key, so he needs to judge the carriage return , So because other serial port assistants have a send key, they need to send a new line to judge whether there is a carriage return
usart.c
void USART1_IRQHandler(void){
// A serial port 1 Interrupt service routine ( Fixed function name cannot be modified )
u8 Res;
// The following is the string received USART_RX_BUF[] The program ,(USART_RX_STA&0x3FFF) It's the length of the data ( Not including return )
// When (USART_RX_STA&0xC000) When true, it indicates that the data reception is completed , Press enter in the HyperTerminal .
// Write judgment in the main function if(USART_RX_STA&0xC000), Then read USART_RX_BUF[] Array , Read 0x0d 0x0a That is the end .
// Note that after the main function processes the serial port data , To put USART_RX_STA clear 0
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){
// Receive interrupt ( The data received must be 0x0d 0x0a ending )
Res =USART_ReceiveData(USART1);//(USART1->DR); // Read received data
printf("%c",Res); // Put the data received in a Symbolic variables Send back to the computer
if((USART1_RX_STA&0x8000)==0){
// Reception is not complete
if(USART1_RX_STA&0x4000){
// received 0x0d
if(Res!=0x0a)USART1_RX_STA=0;// Receive error , restart
else USART1_RX_STA|=0x8000; // The reception is complete
}else{
// I haven't received 0X0D
if(Res==0x0d)USART1_RX_STA|=0x4000;
else{
USART1_RX_BUF[USART1_RX_STA&0X3FFF]=Res ; // Put the received data into the array
USART1_RX_STA++; // Data length count plus 1
if(USART1_RX_STA>(USART1_REC_LEN-1))USART1_RX_STA=0;// Receiving data error , Start receiving again
}
}
}
}
}
Another way :
边栏推荐
- LOGO特训营 第四节 字体设计的重要性
- MYSQL架构——用户权限与管理
- 2022-07-04: what is the output of the following go language code? A:true; B:false; C: Compilation error. package main import “fmt“ func main() { fmt.Pri
- sobel过滤器
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- LOGO特训营 第二节 文字与图形的搭配关系
- LOGO特訓營 第一節 鑒別Logo與Logo設計思路
- Shell script implements application service log warehousing MySQL
- 关于栈区、堆区、全局区、文字常量区、程序代码区
- How to reset the password of MySQL root account
猜你喜欢
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)
LOGO特训营 第二节 文字与图形的搭配关系
Tla+ introductory tutorial (1): introduction to formal methods
Logo special training camp section 1 Identification logo and logo design ideas
业务太忙,真的是没时间搞自动化理由吗?
Wake up day, how do I step by step towards the road of software testing
Attack and defense world misc advanced grace-50
新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
MYSQL架构——逻辑架构
随机推荐
Flask 上下文详解
LOGO特训营 第五节 字体结构与设计常用技法
[cooking record] - stir fried 1000 pieces of green pepper
Deployment of JVM sandbox repeater
leetcode 72. Edit distance edit distance (medium)
制作条形码的手机App推荐
达梦数据凭什么被称为国产数据库“第一股”?
Convolutional neural network model -- lenet network structure and code implementation
Logo special training camp Section IV importance of font design
面试必备 LeetCode 链表算法题汇总,全程干货!
蓝队攻防演练中的三段作战
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
Common shortcut keys for hbuilder x
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
More than 30 institutions jointly launched the digital collection industry initiative. How will it move forward in the future?
Alibaba launched a new brand "Lingyang" and is committed to becoming a "digital leader"
堆排序代码详解
攻防世界 misc 高手进阶区 a_good_idea
Mysql root 账号如何重置密码
Unity-VScode-Emmylua配置报错解决