当前位置:网站首页>Serial port receives a packet of data
Serial port receives a packet of data
2022-07-08 00:56:00 【Zelonal】
With single chip microcomputer N32G435 For example , Serial communication is GPS, Cooperation timer 2 Receive a packet of data .
By continuously updating the timer in the serial port receiving interrupt 2 The count of , Until the timer 2 Interrupt overflow ( General timing 10ms), Indicates that a packet of data has been received .
In the course of debugging , It is found that the receive interrupt has only entered once , A string received only one character , After online inquiry, several solutions have been obtained , as follows :
- The interrupt program takes too long , As a result, subsequent data cannot be received when it arrives .
- Interrupt clear flag bit .
- Serial port interrupt priority is too low , Cause interrupt nesting . Set the priority to the highest .
Interrupt clearing has been added to my interrupt function , It also improves interrupt priority , But it didn't solve , Later, it was found that... In the receiving function would be interrupted printf(); Function delete , Found to return to normal , Should be printf(); The function takes too long to receive the following data !!
Next, I will make a note of the configuration process and processing :
// Timer 2 Initialize configuration
void TIM2_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitType TIM_TimeBaseInitStructure;
NVIC_InitType NVIC_InitStructure;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2,ENABLE); /// Can make TIM2 The clock
TIM_TimeBaseInitStructure.Period = arr; // Automatic reload load value
TIM_TimeBaseInitStructure.Prescaler=psc; // Timer frequency division
TIM_TimeBaseInitStructure.CntMode=TIM_CNT_MODE_UP; // Upcount mode
TIM_TimeBaseInitStructure.ClkDiv=TIM_CLK_DIV1;
TIM_InitTimeBase(TIM2,&TIM_TimeBaseInitStructure);// initialization TIM2
TIM_ConfigInt(TIM2,TIM_INT_UPDATE,ENABLE); // Allow timer 3 Update interrupt
TIM_Enable(TIM2,DISABLE); // First disable timer 3
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; // Timer 3 interrupt
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; // preemption 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; // Sub priority 3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClrIntPendingBit(TIM2,TIM_INT_UPDATE); // Clears the interrupt flag bit
}
// Timer 2 Interrupt function processing
void TIM2_IRQHandler(void)
{
if(TIM_GetIntStatus(TIM2,TIM_INT_UPDATE)==SET) // Overflow interrupt
{
Uart3DataNeedProcessFlag=1;// Need to process current data
TIM_Enable(TIM2,DISABLE);// Stop timer
TIM_ClrIntPendingBit(TIM2,TIM_INT_UPDATE); // Clears the interrupt flag bit
}
}
//GPS-UART To configure
void Uart3Init (void)
{
GPIO_InitType GPIO_InitStructure;
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
// Turn on the clock
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB,ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_UART5,ENABLE);
//GPIO Mouth configuration
GPIO_InitStructure.Pin = GPIO_PIN_8 ; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_UART5;
GPIO_InitPeripheral(GPIOB,&GPIO_InitStructure); //
GPIO_InitStructure.Pin = GPIO_PIN_9;
GPIO_InitStructure.GPIO_Pull = GPIO_Pull_Up;
GPIO_InitStructure.GPIO_Alternate = GPIO_AF6_UART5;
GPIO_InitPeripheral(GPIOB,&GPIO_InitStructure); //
//UART To configure
USART_InitStructure.BaudRate = 9600;// Serial port baud rate
USART_InitStructure.WordLength = USART_WL_8B;// The word is 8 Bit data format
USART_InitStructure.StopBits = USART_STPB_1;// A stop bit
USART_InitStructure.Parity = USART_PE_NO;// No parity bit
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;// No hardware data flow control
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX; // Transceiver mode
USART_Init(UART5, &USART_InitStructure); //
// Interrupt configuration
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2 ; // preemption 2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; // Sub priority 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ Channel enable
NVIC_Init(&NVIC_InitStructure); // Initialize... According to the specified parameters VIC register
USART_ConfigInt(UART5, USART_INT_RXDNE, ENABLE);
USART_Enable(UART5, ENABLE);
}
// Interrupt function processing , You need to put this function into the corresponding interrupt function
void GPS_IRQHandler(void)
{
volatile u8 Res;
if(USART_GetIntStatus(UART5,USART_INT_RXDNE)!=RESET)
{
Res=USART_ReceiveData(UART5);
if(Uart3DataNeedProcessFlag==0)
{
if(Uart3ReceiveDataNum<=UART3_RECEIVE_MAX_NUM)
{
Uart3ReceiveData[Uart3ReceiveDataNum++] = Res;
TIM_ClrIntPendingBit(TIM2,TIM_INT_UPDATE);// Clear timer overflow interrupt
TIM_Enable(TIM2,ENABLE);// Start timing
TIM_SetCnt(TIM2,0);// Reset timer
}
else
{
Uart3ReceiveDataNum=0;
Uart3DataNeedProcessFlag=1;
}
}
}
}
边栏推荐
- Introduction to ML regression analysis of AI zhetianchuan
- 华泰证券官方网站开户安全吗?
- 1293_FreeRTOS中xTaskResumeAll()接口的实现分析
- C # generics and performance comparison
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- 9. Introduction to convolutional neural network
- ABAP ALV LVC template
- Class head up rate detection based on face recognition
- v-for遍历元素样式失效
- 深潜Kotlin协程(二十三 完结篇):SharedFlow 和 StateFlow
猜你喜欢

"An excellent programmer is worth five ordinary programmers", and the gap lies in these seven key points

Class head up rate detection based on face recognition

Service mesh introduction, istio overview

CVE-2022-28346:Django SQL注入漏洞
![[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation](/img/76/b048e100d2c964ac00bc4f64e97e7a.png)
[go record] start go language from scratch -- make an oscilloscope with go language (I) go language foundation

Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知

新库上线 | CnOpenData中国星级酒店数据
![[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output](/img/79/f5cffe62d5d1e4a69b6143aef561d9.png)
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output

51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up

5G NR 系统消息
随机推荐
13. Enregistrement et chargement des modèles
赞!idea 如何单窗口打开多个项目?
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
STL--String类的常用功能复写
Course of causality, taught by Jonas Peters, University of Copenhagen
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
Analysis of 8 classic C language pointer written test questions
Invalid V-for traversal element style
13. Model saving and loading
第四期SFO销毁,Starfish OS如何对SFO价值赋能?
Codeforces Round #804 (Div. 2)(A~D)
Jouer sonar
tourist的NTT模板
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
CVE-2022-28346:Django SQL注入漏洞
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
Hotel
Reentrantlock fair lock source code Chapter 0