当前位置:网站首页>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 :
边栏推荐
- Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
- 【OpenGL】笔记二十九、抗锯齿(MSAA)
- Force buckle 3_ 383. Ransom letter
- 【烹饪记录】--- 青椒炒千张
- Shell 脚本实现应用服务日志入库 Mysql
- 傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
- [Yugong series] go teaching course 003-ide installation and basic use in July 2022
- Logo special training camp Section IV importance of font design
- MySQL storage data encryption
- BigFilter全局交易防重组件的介绍与应用
猜你喜欢
Attack and defense world misc advanced area Hong
Common open source codeless testing tools
Embedded development: skills and tricks -- seven skills to improve the quality of embedded software code
Convolutional neural network model -- lenet network structure and code implementation
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Close system call analysis - Performance Optimization
The sandbox has reached a cooperation with digital Hollywood to accelerate the economic development of creators through human resource development
Logo special training camp section III initial creative techniques
达梦数据凭什么被称为国产数据库“第一股”?
页面关闭前,如何发送一个可靠请求
随机推荐
Introduction and application of bigfilter global transaction anti duplication component
PHP short video source code, thumb animation will float when you like it
攻防世界 MISC 进阶区 Erik-Baleog-and-Olaf
Close system call analysis - Performance Optimization
sobel过滤器
Short video system source code, click the blank space of the screen, the keyboard does not automatically stow
将QA引入软件开发生命周期是工程师要遵循的最佳实践
Postgresqlql advanced skills pivot table
Logo special training camp Section IV importance of font design
制作条形码的手机App推荐
Logo special training camp Section V font structure and common design techniques
共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
[Yugong series] go teaching course 003-ide installation and basic use in July 2022
傳智教育|如何轉行互聯網高薪崗比特之一的軟件測試?(附軟件測試學習路線圖)
繁華落盡、物是人非:個人站長該何去何從
LOGO特训营 第三节 首字母创意手法
Business is too busy. Is there really no reason to have time for automation?
攻防世界 MISC 进阶 glance-50
odps 中 对表进行了一次备份,为什么在元数据库查m_table 时,两张表的逻辑大小不一致,但数
Attack and defense world misc advanced area Hong