STM32 -USART中断方式
来源:互联网 发布:一元购网站源码 编辑:程序博客网 时间:2024/05/18 09:48
前面我们接收了串口通信的查询方式,现在我们来介绍中断方式。
步骤一:初始化GPIO
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
步骤二:开时钟
/* Enable USART1, GPIOA, GPIOD and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD
| RCC_APB2Periph_AFIO, ENABLE);
在此说明,不用设置RCC_APB2Periph_AFIO也是可以的,也就是在此没有使用复用功能。
这两个步骤与查询方式是一样的。
步骤三:初始化USART1
USART_InitStructure.USART_BaudRate = 115200;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_2;
USART_InitStructure.USART_Parity = USART_Parity_No; //设置奇校验时,通信出现错误
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART Transmoit interrupt: this interrupt is generated when the
USART1 transmit data register is empty */
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);
在这里要使能USART1的外设中断,如USART_ITConfig(USART1, USART_IT_TXE, ENABLE);这就是使能发送中断,但发送寄存器空时能产生中断。
步骤四:编写中断函数
uint8_t TxBuffer[] = "\n\rUSART Hyperterminal Interrupts Example: USART-Hyperterminal\
communication using Interrupt\n\r";
uint8_t RxBuffer[RxBufferSize];
uint8_t NbrOfDataToTransfer = TxBufferSize;
uint8_t NbrOfDataToRead = RxBufferSize;
uint8_t TxCounter = 0;
uint16_t RxCounter = 0;
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = (USART_ReceiveData(USART1) & 0x7F);
if(RxCounter == NbrOfDataToRead)
{
/* Disable the USART Receive interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
}
}
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, TxBuffer[TxCounter++]);
if(TxCounter == NbrOfDataToTransfer)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
}
至此程序就结束了。
我们就会有个疑问,main()只包括前三个步骤的初始化和一个死循环,那么中断又是如何触发的呢,main()的结构如下:
int main(void)
{
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
USART_Configuration();
while (1)
{
}
}
原来是这样的:状态寄存器USART_SR的复位值为0x00C0H, 也就是第七位TXE和第六位TC复位值为1,而TXE=1,表明发送数据寄存器为空, TC=1表明发送已完成。而在USART的设置中有
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
这两句使能中断,也就是说当TXE=1就会进入中断,所以程序初始化后就能进入中断,执行
if(USART_GetITStatus(USART1, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART1, TxBuffer[TxCounter++]);
if(TxCounter == NbrOfDataToTransfer)
{
/* Disable the USART1 Transmit interrupt */
USART_ITConfig(USART1, USART_IT_TXE, DISABLE);
}
}
- STM32 -USART中断方式
- STM32 -USART中断方式
- STM32学习之:USART中断方式
- STM32 USART中断小程序
- STM32 USART中断小程序
- STM32 USART寄存器方式编程
- stm32 USART中断标志位什么时候清除
- STM32 USART DMA发送 中断接收
- STM32 USART
- STM32 USART
- STM32-USART
- [STM32] 关于USART接收中断的BUG和注意事项
- Stm32使用Usart代码例子(轮询、中断、DMA)
- 关于stm32的usart的接收中断使能问题
- STM32串口中断方式接收
- STM32串口中断方式接收
- STM32 学习七 USART通信2 寄存器方式编程
- STM32 学习八 USART通信3 库函数方式编程
- Irrlicht引擎
- 《怪诞行为学2》读书笔记,第三章 宜家效应:为什么我们会高估自己的劳动成果?
- java中访问修饰符的使用,方法重写定义,final修饰符,布局的特点等等:
- Scheme基本概念之External Representations
- 动态规划(算法导论学习笔记)
- STM32 -USART中断方式
- 素数定理(素数的分布)
- careercup1.7
- iOS6的framework运行在iOS5或者以下版本机器上报错的解决办法
- Google Maps API编程资源大全
- Ubuntu 12.04中文输入法的安装
- [转]Android xmlns 的作用及其自定义
- 使用ASP.NET上传图片汇总
- 双链表的插入删除操作