http://blog.csdn.net/shu_8708/article/details/73480939
串口的中断类型:
[cpp]view plain
copy
- #define USART_IT_PE((uint16_t)0x0028)
- #define USART_IT_TXE((uint16_t)0x0727)
- #define USART_IT_TC((uint16_t)0x0626)
- #define USART_IT_RXNE((uint16_t)0x0525)
- #define USART_IT_IDLE((uint16_t)0x0424)
- #define USART_IT_LBD((uint16_t)0x0846)
- #define USART_IT_CTS((uint16_t)0x096A)
- #define USART_IT_ERR((uint16_t)0x0060)
- #define USART_IT_ORE((uint16_t)0x0360)
- #define USART_IT_NE((uint16_t)0x0260)
- #define USART_IT_FE((uint16_t)0x0160)
USART_IT_TXE发送中断
USART_IT_TC 传输完成中断
USART_IT_RXNE 接收中断
USART_IT_IDLE 空闲总线中断
USART_IT_LBD LIN中断检测中断
USART_IT_CTS CTS中断
USART_IT_ERR 错误中断
该程序中用到的就是串口的空闲中断:当总线是一个字节周期内没有收到数据时触发
串口的配置如下:
[cpp]view plain copy
- void uart_init(u32 bound){
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);
- NVIC_InitTypeDef NVIC_InitStructure;
- //IO口相关配置
- USART_DeInit(USART1);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速率
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //串口中断配置
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- USART_InitStructure.USART_Parity = USART_Parity_No; //没有奇偶校验位
- USART_InitStructure.USART_BaudRate = bound; //波特率设置
- USART_InitStructure.USART_WordLength = USART_WordLength_8b; //8位数据位
- USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止位
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件数据流配置
- USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //串口收发模式
- USART_Init(USART1, &USART_InitStructure);
- USART_ITConfig(USART1, USART_IT_IDLE, ENABLE);
- USART_Cmd(USART1, ENABLE);
- }
串口中断配置: void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel5,DISABLE);
recok=1;
lenth=USART1->DR; //软件清空空闲中断标志位
lenth=USART1->SR;
lenth=32-DMA_GetCurrDataCounter(DMA1_Channel5); //获取当前接收的数据量
USART_RX_BUF[lenth]=0; //在buff最后加入空字符
DMA1_Channel5->CNDTR=32; //重新设置传输量为32
DMA_Cmd(DMA1_Channel5,ENABLE);
}
}
DMA配置:
void MYDMA_Config(DMA_Channel_TypeDef* DMA_CHx,u32 cpar,u32 cmar,u16 cndtr)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); //使能DMA时钟
DMA_DeInit(DMA_CHx);
DMA1_MEM_LEN=cndtr;
DMA_InitStructure.DMA_PeripheralBaseAddr = cpar; //外设地址
DMA_InitStructure.DMA_MemoryBaseAddr = cmar; //内存地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //传输方向外设到内存
DMA_InitStructure.DMA_BufferSize = cndtr; //传输量
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址不自增
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存地址自增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte;
DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; //普通模式
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //高优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA_CHx, &DMA_InitStructure);
}
主函数中需要进行的配置: 【STM32空闲中断+DMA解决接收不定长数据问题】
[cpp]view plain copy
- MYDMA_Config(DMA1_Channel5,(u32)&USART1->DR,(u32)USART_RX_BUF,32); //传输方向设置为USART1->DR到USART_RX_BUF 传输大小为32字节
- USART_DMACmd(USART1,USART_DMAReq_Rx,ENABLE); //允许DMA请求
- DMA_Cmd(DMA1_Channel5,ENABLE);
当标志为recok位1时表示接受完成在主函数中等待recok位1就可以了
推荐阅读
- stm32|基于STM32和freeRTOS智能门锁设计方案
- 日常分享|共享充电宝方案原理,具体部件组成以及主控MUC参数
- #|ARM裸机开发(汇编LED灯实验(I.MX6UL芯片))
- STM32|STM32的四种IO输出模式
- STM32 远程升级(ISP / IAP)
- stm32|stm32f103can总线过滤器配置
- STM32CubeMX配置SDIO模式(非DMA方式)
- STM32|如何建一个STM32F030标准库工程模板
- STM32 时钟RCC相关配置参考stm32f10x_rcc.h
- STM32 NVIC