历史上的今天
今天是:2024年10月21日(星期一)
2018年10月21日 | STM32学习之:USART中断方式
2018-10-21 来源:eefocus
前面我们接收了串口通信的查询方式,现在我们来介绍中断方式。
步骤一:初始化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);
}
}
史海拾趣
|
电子尺/位移/电位器/角度 线性传感器专用变送器电子尺/位移/电位器/角度等线性传感器信号的放大、转换、隔离及变送 顺源科技研发生产的位移传感器专用变送器,采用输入滤波保护电路,高精度稳压基准电源电路,放大转换电路,零位满度调节变换电 ...… 查看全部问答> |
|
本帖最后由 paulhyde 于 2014-9-15 03:43 编辑 该电机驱动电路已经跟随我经历了校内电子竞赛,07年电子竞赛和08年电子竞赛。 … 查看全部问答> |
|
// 预定义函数 inline f32 Mulfx(f32 x, f32 y) { return ((y) * (x)) >> 12; } inline f32 Divfx(f32 x, f32 y) { return (((x) 12); } inline float f32toflo ...… 查看全部问答> |
|
我的本科毕业设计是做wince下的应用程序开发, 现在还没选定题目,不知道做什么比较好? 本人VC基础不是很好,如果太难肯定做不出来, 请问有比较简单的应用程序开发比较适合本科毕业设计吗?… 查看全部问答> |
|
现在有一个基于WINCE5.0的GPS设备,想在他的存储卡上写一个临时文件保存信息 开发环境是VS2005和开发板导出的SDK 代码如下 void WriteFileEx() { ...… 查看全部问答> |
|
FLASH锁死了这是为什么,有没有办法解锁。为什么会有类似下面这几种报错: Read status value 0x0001 from symbol PRG_status(24x系列) ADC Calibration not complete, check if device is unlocked and recalibrate.(2833x系列) 答:FL ...… 查看全部问答> |




