当前位置:网站首页>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 :
边栏推荐
- SQL中MAX与GREATEST的区别
- Li Kou 98: verify binary search tree
- leetcode 72. Edit distance edit distance (medium)
- 如何实现轻松管理1500万员工?
- Logo special training camp section III initial creative techniques
- 30余家机构联合发起数字藏品行业倡议,未来会如何前进?
- 模拟摇杆控制舵机
- 将QA引入软件开发生命周期是工程师要遵循的最佳实践
- MYSQL架构——用户权限与管理
- About stack area, heap area, global area, text constant area and program code area
猜你喜欢
It is said that software testing is very simple, but why are there so many dissuasions?
Lost in the lock world of MySQL
Logo special training camp Section IV importance of font design
LOGO特训营 第二节 文字与图形的搭配关系
Detailed explanation of heap sort code
UML图记忆技巧
攻防世界 MISC 进阶 glance-50
Kdd2022 | what features are effective for interaction?
How to transfer to software testing, one of the high paying jobs in the Internet? (software testing learning roadmap attached)
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
随机推荐
Li Kou 98: verify binary search tree
Logo special training camp section III initial creative techniques
UML diagram memory skills
嵌入式开发:技巧和窍门——提高嵌入式软件代码质量的7个技巧
SPSS installation and activation tutorial (including network disk link)
Test will: bug classification and promotion solution
É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 进阶区 Erik-Baleog-and-Olaf
我在linux里面 通过调用odspcmd 查询数据库信息 怎么静默输出 就是只输出值 不要这个
集群的概述与定义,一看就会
达梦数据凭什么被称为国产数据库“第一股”?
将QA引入软件开发生命周期是工程师要遵循的最佳实践
阿里推出新品牌“瓴羊”,致力成为“数字化领头羊”
NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元
Interview essential leetcode linked list algorithm question summary, whole process dry goods!
SPSS安装激活教程(包含网盘链接)
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse
Force buckle 2_ 1480. Dynamic sum of one-dimensional array
Google Earth Engine(GEE)——以MODIS/006/MCD19A2为例批量下载逐天AOD数据逐天的均值、最大值、最小值、标准差、方差统计分析和CSV下载(北京市各区为例)
Microservices -- Opening