STM32学习-27-[中断]中断编程实验
编写闪灯的代码
#include "stm32f10x.h"
uint32_t blinkInterval = 1000; // 闪灯间隔
void App_OnBoardLED_Init(void);
int main(void)
{
App_OnBoardLED_Init();
while(1)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
Delay(blinkInterval);
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
Delay(blinkInterval);
}
}
void App_OnBoardLED_Init(void){
RCC_APB2PriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 开启GPIOC的时钟
GPIO_InitTypeDef GPIO_InitStruct = {0}; // 定义结构体
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct); // 对引脚进行初始化
}
初始化串口
void App_USART1_Init(void){
GPIO_InitTypeDef GPIO_InitStruct; // 定义初始化结构体
// Tx PA9 复用输出推挽
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 开启时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // 设置配置的引脚编号
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; // 模式
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; // 最大速度
GPIO_Init(GPIOA.&GPIO_InitStruct);
// Rx PA10 输入浮空 输入上拉
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; // 模式
GPIO_Init(GPIOA, &GPIO_InitStruct);
RCC_APB2PriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 启动UASRT1模块的时钟
// 对串口进行初始化
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 115200; // 设置波特率
USART_InitStruct.USART_Mode = USART_Tx | USART_Rx; // 设置收发方向
USART_InitStruct.USART_WordLength = USART_WordLength_8b; // 设置数据位数
USART_InitStruct.USART_StopBits = USART_StopBits_1; // 设置停止位
USART_InitStruct.USART_Parity = USART_Parity_No; // 设置校验位
// 调用初始化函数
USART_Init(USART1, &USART_InitStruct);
// 启用USART模块 闭合总开关
USART_Cmd(USART1, ENABLE);
}
让RxNE标志位触发中断

只有闭合了开关的寄存器才能触发中断,他们共用一个中断触发,使用的是或门
// 配置中断
USART_ITConfig(USART1, USART_IT_RCNE, ENABLE);
配置NVIC模块
NVIC属于内核的一部分,不是片上外设,所以时钟不需要我们手动开启,当然,他的时钟也无法关闭

// 配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

// 配置NVIC
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; // 设置串口1的优先级
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; // 子优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // 使能开关
NVIC_Init(&NVIC_InitStruct);
编写中断响应函数

下面这个文件是单片机启动的文件

这里面记录了我们需要的中断响应函数

void USART1_IRQHandLer(void){ // 中断响应函数
// 判断中断的产生原因
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET){
uint8_t byte = USART_ReceiveData(USART1); // 读取数据,清除标志位
if(byte == '0')
blinkInterval = 1000; // 慢
if(byte == '1')
blinkInterval = 200; // 中
if(byte == '2')
blinkInterval = 50;// 快
}
}
汇总代码
#include "stm32f10x.h"
uint32_t blinkInterval = 1000; // 闪灯间隔
void App_OnBoardLED_Init(void);
void App_USART1_Init(void);
int main(void)
{
// 配置NVIC
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
App_OnBoardLED_Init();
App_USART1_Init();
while(1)
{
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_RESET);
Delay(blinkInterval);
GPIO_WriteBit(GPIOC, GPIO_Pin_13, Bit_SET);
Delay(blinkInterval);
}
}
void USART1_IRQHandLer(void){ // 中断响应函数
// 判断中断的产生原因
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == SET){
uint8_t byte = USART_ReceiveData(USART1); // 读取数据,清除标志位
if(byte == '0')
blinkInterval = 1000; // 慢
if(byte == '1')
blinkInterval = 200; // 中
if(byte == '2')
blinkInterval = 50;// 快
}
}
void App_OnBoardLED_Init(void){
RCC_APB2PriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); // 开启GPIOC的时钟
GPIO_InitTypeDef GPIO_InitStruct = {0}; // 定义结构体
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOC, &GPIO_InitStruct); // 对引脚进行初始化
}
void App_USART1_Init(void){
GPIO_InitTypeDef GPIO_InitStruct; // 定义初始化结构体
// Tx PA9 复用输出推挽
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); // 开启时钟
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9; // 设置配置的引脚编号
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; // 模式
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_10MHz; // 最大速度
GPIO_Init(GPIOA.&GPIO_InitStruct);
// Rx PA10 输入浮空 输入上拉
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IPU; // 模式
GPIO_Init(GPIOA, &GPIO_InitStruct);
RCC_APB2PriphClockCmd(RCC_APB2Periph_USART1, ENABLE); // 启动UASRT1模块的时钟
// 对串口进行初始化
USART_InitTypeDef USART_InitStruct;
USART_InitStruct.USART_BaudRate = 115200; // 设置波特率
USART_InitStruct.USART_Mode = USART_Tx | USART_Rx; // 设置收发方向
USART_InitStruct.USART_WordLength = USART_WordLength_8b; // 设置数据位数
USART_InitStruct.USART_StopBits = USART_StopBits_1; // 设置停止位
USART_InitStruct.USART_Parity = USART_Parity_No; // 设置校验位
// 调用初始化函数
USART_Init(USART1, &USART_InitStruct);
// 启用USART模块 闭合总开关
USART_Cmd(USART1, ENABLE);
// 配置中断
USART_ITConfig(USART1, USART_IT_RCNE, ENABLE);
// 配置NVIC
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel = USART1_IRQn; // 设置串口1的优先级
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0; // 抢占优先级
NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0; // 子优先级
NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE; // 使能开关
NVIC_Init(&NVIC_InitStruct);
}

浙公网安备 33010602011771号