当前位置:网站首页>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 :
边栏推荐
- 测试必会:BUG的分类及推进解决
- PostgreSQLSQL高级技巧透视表
- Logo special training camp Section IV importance of font design
- 傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
- Common shortcut keys for hbuilder x
- PostgreSQL JOIN实践及原理
- leetcode 72. Edit Distance 编辑距离(中等)
- Introducing QA into the software development lifecycle is the best practice that engineers should follow
- Convolutional neural network model -- lenet network structure and code implementation
- La prospérité est épuisée, les choses sont bonnes et mauvaises: Où puis - je aller pour un chef de station personnel?
猜你喜欢
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Attack and defense world misc advanced area Hong
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
Logo special training camp Section V font structure and common design techniques
Locust performance test - environment construction and use
Business is too busy. Is there really no reason to have time for automation?
LOGO特訓營 第三節 首字母創意手法
Scala download and configuration
Attack and defense world misc advanced area ditf
Ascendex launched Walken (WLKN) - an excellent and leading "walk to earn" game
随机推荐
MD5 tool class
攻防世界 MISC 进阶区 hit-the-core
攻防世界 misc 高手进阶区 a_good_idea
Force buckle_ Palindrome number
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
LOGO特训营 第五节 字体结构与设计常用技法
LOGO特训营 第三节 首字母创意手法
Tiktok actual combat ~ the number of comments is updated synchronously
Éducation à la transmission du savoir | Comment passer à un test logiciel pour l'un des postes les mieux rémunérés sur Internet? (joindre la Feuille de route pour l'apprentissage des tests logiciels)
微服务--开篇
攻防世界 MISC 进阶 glance-50
Wake up day, how do I step by step towards the road of software testing
idea中pom.xml依赖无法导入
Common open source codeless testing tools
攻防世界 MISC 進階區 Erik-Baleog-and-Olaf
Detailed explanation of heap sort code
Jvm-Sandbox-Repeater的部署
攻防世界 misc 进阶区 2017_Dating_in_Singapore
串口数据帧
攻防世界 MISC 进阶区 Ditf