当前位置:网站首页>串口接收一包数据
串口接收一包数据
2022-07-07 23:10:00 【Zelonal】
以单片机N32G435为例,串口通信为GPS,配合定时器2接收一包数据。
通过在串口接收中断中不断更新定时器2的计数值,直到定时器2中断溢出(一般定时10ms),表示一包数据接收完成。
在调试过程中,发现接收中断只进了一次,一个字符串只接收到了一个字符,在网上查询后得出了几个解决办法,如下:
- 中断程序占用时间过长,导致后面的数据到达的时候无法接收。
- 中断清除标志位。
- 串口中断优先级太低,导致中断嵌套。将优先级设为最高。
本人中断函数中已添加中断清除,也提高了中断优先级,但并没有解决,后来发现将中断接收函数中的printf();函数删除,发现恢复正常,应该是printf();函数占用时间过长导致后面数据无法接到!!
下面将配置过程和处理部分做个笔记:
// 定时器2初始化配置
void TIM2_Int_Init(u16 arr,u16 psc)
{
TIM_TimeBaseInitType TIM_TimeBaseInitStructure;
NVIC_InitType NVIC_InitStructure;
RCC_EnableAPB1PeriphClk(RCC_APB1_PERIPH_TIM2,ENABLE); ///使能TIM2时钟
TIM_TimeBaseInitStructure.Period = arr; //自动重装载值
TIM_TimeBaseInitStructure.Prescaler=psc; //定时器分频
TIM_TimeBaseInitStructure.CntMode=TIM_CNT_MODE_UP; //向上计数模式
TIM_TimeBaseInitStructure.ClkDiv=TIM_CLK_DIV1;
TIM_InitTimeBase(TIM2,&TIM_TimeBaseInitStructure);//初始化TIM2
TIM_ConfigInt(TIM2,TIM_INT_UPDATE,ENABLE); //允许定时器3更新中断
TIM_Enable(TIM2,DISABLE); //先失能定时器3
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn; //定时器3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01; //抢占优先级1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x03; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
TIM_ClrIntPendingBit(TIM2,TIM_INT_UPDATE); //清除中断标志位
}
// 定时器2中断函数处理
void TIM2_IRQHandler(void)
{
if(TIM_GetIntStatus(TIM2,TIM_INT_UPDATE)==SET) //溢出中断
{
Uart3DataNeedProcessFlag=1;//需要处理当前数据
TIM_Enable(TIM2,DISABLE);//停止定时器
TIM_ClrIntPendingBit(TIM2,TIM_INT_UPDATE); //清除中断标志位
}
}
//GPS-UART配置
void Uart3Init (void)
{
GPIO_InitType GPIO_InitStructure;
USART_InitType USART_InitStructure;
NVIC_InitType NVIC_InitStructure;
//开时钟
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_GPIOB,ENABLE);
RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_UART5,ENABLE);
//GPIO口配置
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配置
USART_InitStructure.BaudRate = 9600;//串口波特率
USART_InitStructure.WordLength = USART_WL_8B;//字长为8位数据格式
USART_InitStructure.StopBits = USART_STPB_1;//一个停止位
USART_InitStructure.Parity = USART_PE_NO;//无奇偶校验位
USART_InitStructure.HardwareFlowControl = USART_HFCTRL_NONE;//无硬件数据流控制
USART_InitStructure.Mode = USART_MODE_RX | USART_MODE_TX; //收发模式
USART_Init(UART5, &USART_InitStructure); //
//中断配置
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2 ; //抢占优先级2
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //子优先级2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ConfigInt(UART5, USART_INT_RXDNE, ENABLE);
USART_Enable(UART5, ENABLE);
}
//中断函数处理,需要将此函数放到对应的中断函数中
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);//清除定时器溢出中断
TIM_Enable(TIM2,ENABLE);//开始计时
TIM_SetCnt(TIM2,0);//复位定时器
}
else
{
Uart3ReceiveDataNum=0;
Uart3DataNeedProcessFlag=1;
}
}
}
}
边栏推荐
- Cause analysis and solution of too laggy page of [test interview questions]
- 新库上线 | 中国记者信息数据
- 51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
- ABAP ALV LVC template
- 应用实践 | 数仓体系效率全面提升!同程数科基于 Apache Doris 的数据仓库建设
- [OBS] the official configuration is use_ GPU_ Priority effect is true
- v-for遍历元素样式失效
- 搭建ADG过程中复制报错 RMAN-03009 ORA-03113
- 【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
猜你喜欢

测试流程不完善,又遇到不积极的开发怎么办?
![Cause analysis and solution of too laggy page of [test interview questions]](/img/8d/3ca92ce5f9cdc85d52dbcd826e477d.jpg)
Cause analysis and solution of too laggy page of [test interview questions]

5G NR 系统消息

CVE-2022-28346:Django SQL注入漏洞

DNS series (I): why does the updated DNS record not take effect?

去了字节跳动,才知道年薪 40w 的测试工程师有这么多?

Development of a horse tourism website (optimization of servlet)

How to learn a new technology (programming language)

v-for遍历元素样式失效

【笔记】常见组合滤波电路
随机推荐
Reentrantlock fair lock source code Chapter 0
Qt添加资源文件,为QAction添加图标,建立信号槽函数并实现
Basic mode of service mesh
How is it most convenient to open an account for stock speculation? Is it safe to open an account on your mobile phone
They gathered at the 2022 ecug con just for "China's technological power"
A brief history of information by James Gleick
炒股开户怎么最方便,手机上开户安全吗
ReentrantLock 公平锁源码 第0篇
测试流程不完善,又遇到不积极的开发怎么办?
Is it safe to open an account on the official website of Huatai Securities?
[Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
Single machine high concurrency model design
第一讲:链表中环的入口结点
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
Class head up rate detection based on face recognition
Su embedded training - day4
What if the testing process is not perfect and the development is not active?
Summary of the third course of weidongshan
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
C# 泛型及性能比较