stm32什么触发串口接收中断

2024-11-24 09:48:17
推荐回答(2个)
回答1:

串口配置好即配置好GPIO和NVIC并初始化相应外设,然后每接受一个data就触发一次中断,转到中断入口处处理,然后清除中断标志位。一般情况下只有接受的时候采用中断方式,而发送只是放到buff寄存器里,不需要中断方式。以下是以前学习的时候串口通讯点灯的代码:
#include "usart.h"
void usart_config(void)
{
/*
1、打开GPIO,AFIO,USART1的时钟
2、初始化相应串口引脚
3、配置串口中断
4、配置串口参数
*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA
| RCC_APB2Periph_AFIO,ENABLE);
usart_release_gpio_init();
usart_nvic_init();
usart_para_init();

}
void usart_release_gpio_init(void)
{
GPIO_InitTypeDef GPIO_InitStruct;

/*配置PA9为复用推挽输出*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStruct);//配置好的参数初始化PA9

/*配置PA10为浮空输入*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStruct);//配置好的参数初始化PA10
}
void usart_para_init(void)
{
USART_InitTypeDef USART_InitStruct;

USART_InitStruct.USART_BaudRate = 9600; //波特率9600
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //无硬件流控
USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;

USART_InitStruct.USART_WordLength = USART_WordLength_8b; //8
USART_InitStruct.USART_Parity = USART_Parity_No; //n
USART_InitStruct.USART_StopBits = USART_StopBits_1; //1

USART_Init(USART1,&USART_InitStruct);//配置好的参数初始化USART1

USART_ClearFlag(USART1,USART_FLAG_TC);//清除发送完成标志位
USART_Cmd(USART1, ENABLE); //使能USART1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//使能USART1接收中断

}
void usart_nvic_init(void)
{
NVIC_InitTypeDef NVIC_InitStruct;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //设置组优先级

NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; //设置串口1中断
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级0
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; //子优先级0
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; //使能

NVIC_Init(&NVIC_InitStruct);//配置好的参数初始化NVIC
}
void USART1_IRQHandler ()
{
u8 RxData;
if(USART_GetFlagStatus(USART1, USART_IT_RXNE) != RESET) //判断是否有中断
{
RxData = USART_ReceiveData(USART1);
if(RxData == 0x00 )
{
GPIO_SetBits(GPIOB, GPIO_Pin_15);
}
else
{
GPIO_ResetBits(GPIOB, GPIO_Pin_15);
}
}
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除接收终端标志位
}

回答2:

main.c配置:

[cpp] view plain copy print?
/*
*说明:
*PA0:KEY1;PA1:KEY2;
*PA2:LED1;PA3:LED2;
*PA9:USART1_TX;PA10:USART1_RX
*/
#include "stm32f10x.h"
#include "stm32f10x_rcc.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_exti.h"
#include "system_stm32f10x.h"
#include "misc.h"

void RCC_Configuration(void);
void GPIO_Configuration(void);
void USART_Configuration(void);
void NVIC_Configuration(void);

int main()
{
SystemInit();//72m

RCC_Configuration();
GPIO_Configuration();
USART_Configuration();
NVIC_Configuration();

while(1);
}

void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
}

void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //管脚9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}

void USART_Configuration(void)//串口初始化函数
{
//串口参数初始化
USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数
//初始化参数设置
USART_InitStructure.USART_BaudRate = 9600; //波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位
USART_InitStructure.USART_StopBits = USART_StopBits_1; //1位停止字节
USART_InitStructure.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//无流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//打开Rx接收和Tx发送功能
USART_Init(USART1,&USART_InitStructure); //初始化

USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //允许串口1发送中断。
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //允许串口1接收中断。
USART_Cmd(USART1,ENABLE); //启动串口
USART_ClearFlag(USART1,USART_FLAG_TC); //发送完成标志位
}
void NVIC_Configuration(void)//配置中断优先级
{
NVIC_InitTypeDef NVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

stm32f10x_it.c的中断函数接收发送数据(接收和发送的时候改变LED状态):

[cpp] view plain copy print?
#include "..\include\stm32f10x.h"
#include "..\include\stm32f10x_it.h"
#include "..\include\stm32f10x_gpio.h"
#include "..\include\stm32f10x_usart.h"

void USART1_IRQHandler(void)
{
u8 RX_dat;

if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)//USART_IT_RXNE:接收中断
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
RX_dat=USART_ReceiveData(USART1);
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET){} //接收数据寄存器非空标志位
GPIO_WriteBit(GPIOA,GPIO_Pin_2,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_2)));

USART_SendData(USART1,RX_dat);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET){} //发送数据寄存器空标志位
GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(Bit_SET-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));
}
}