学习stm32中断原理和开发编程方法。使用标准库完成以下任务:
1)用stm32F103核心板的GPIOA端一管脚接一个LED,GPIOB端口一引脚接一个开关(用杜邦线模拟代替)。采用中断模式编程,当开关接高电平时,LED亮灯;接低电平时,LED灭灯。如果完成后,尝试在main函数while循环中加入一个串口每隔1s 发送一次字符的代码片段,观察按键中断对串口发送是否会带来干扰或延迟。
在keil上编写代码
#include "stm32f10x.h"
void GPIO_Configuration(void);
void EXTI_Configuration(void);
void USART_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
GPIO_Configuration();
EXTI_Configuration();
USART_Configuration();
while (1)
{
// 在此添加串口发送代码片段
USART_SendData(USART1, 'A');
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
Delay(1000); // 延时1秒
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
// 配置GPIOA的引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置GPIOB的引脚为浮空输入
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling; // 上升沿和下降沿触发中断
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x01;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
// 在此添加串口初始化配置代码
}
void Delay(__IO uint32_t nCount)
{
for (; nCount != 0; nCount--);
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
EXTI_ClearITPendingBit(EXTI_Line0);
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == GPIO_Pin_0)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0); // 点亮LED
}
else
{
GPIO_ResetBits(GPIOA, GPIO_Pin_0); // 熄灭LED
}
}
}
2)采用串口中断方式重做上周查询方式的串口通信作业,分别实现:
(1)当stm32接收到1个字符“s”时,停止持续发送“hello windows!”; 当接收到1个字符“t”时,持续发送“hello windows!”(提示:采用一个全局标量做信号灯);
#include "stm32f10x.h"
#include <string.h>
#define BUFFER_SIZE 20
uint8_t rxBuffer[BUFFER_SIZE];
volatile uint8_t rxIndex = 0;
volatile uint8_t rxComplete = 0;
volatile uint8_t sendFlag = 0; // 全局标量做信号灯
void GPIO_Configuration(void);
void USART_Configuration(void);
void USART_SendString(const char* str);
void ProcessReceivedData(void);
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
if (rxComplete)
{
ProcessReceivedData();
// 清除接收标志位并重置接收缓冲区
rxComplete = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
rxIndex = 0;
}
if (sendFlag)
{
USART_SendString("hello windows!");
// 延时一段时间,以便观察输出
for (volatile int i = 0; i < 1000000; ++i);
// 清除信号灯
sendFlag = 0;
}
}
}
void GPIO_Configuration(void)
{
// 配置GPIO引脚
// ...
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// 配置USART引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART参数
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
USART_Init(USART1, &USART_InitStructure);
// 使能USART接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 使能USART
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(const char* str)
{
while (*str)
{
USART_SendData(USART1, *str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void ProcessReceivedData(void)
{
if (strcmp((char*)rxBuffer, "s") == 0)
{
// 停止持续发送
sendFlag = 0;
}
else if (strcmp((char*)rxBuffer, "t") == 0)
{
// 持续发送
sendFlag = 1;
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART1);
if (data != '\r' && data != '\n')
{
rxBuffer[rxIndex++] = data;
if (rxIndex >= BUFFER_SIZE)
rxIndex = 0;
}
if (data == '\n')
{
rxComplete = 1;
}
}
}
(2)当stm32接收到字符“stop stm32!”时,停止持续发送“hello windows!”; 当接收到字符“go stm32!”时,持续发送“hello windows!”(提示:要将接收到的连续字符保存到一个字符数组里,进行判别匹配。写一个接收字符串的函数。
#include "stm32f10x.h"
#include <string.h>
#define BUFFER_SIZE 20
uint8_t rxBuffer[BUFFER_SIZE];
volatile uint8_t rxIndex = 0;
volatile uint8_t rxComplete = 0;
volatile uint8_t sendFlag = 0; // 全局标量做信号灯
void GPIO_Configuration(void);
void USART_Configuration(void);
void USART_SendString(const char* str);
void ProcessReceivedData(void);
int main(void)
{
GPIO_Configuration();
USART_Configuration();
while (1)
{
if (rxComplete)
{
ProcessReceivedData();
// 清除接收标志位并重置接收缓冲区
rxComplete = 0;
memset(rxBuffer, 0, sizeof(rxBuffer));
rxIndex = 0;
}
if (sendFlag)
{
USART_SendString("hello windows!");
// 延时一段时间,以便观察输出
for (volatile int i = 0; i < 1000000; ++i);
// 清除信号灯
sendFlag = 0;
}
}
}
void GPIO_Configuration(void)
{
// 配置GPIO引脚
// ...
}
void USART_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
// 配置USART引脚
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置USART参数
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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;
USART_Init(USART1, &USART_InitStructure);
// 使能USART接收中断
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 使能USART
USART_Cmd(USART1, ENABLE);
}
void USART_SendString(const char* str)
{
while (*str)
{
USART_SendData(USART1, *str++);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
void ProcessReceivedData(void)
{
if (strcmp((char*)rxBuffer, "stop stm32!") == 0)
{
// 停止持续发送
sendFlag = 0;
}
else if (strcmp((char*)rxBuffer, "go stm32!") == 0)
{
// 持续发送
sendFlag = 1;
}
}
void USART1_IRQHandler(void)
{
if (USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
uint8_t data = USART_ReceiveData(USART1);
if (data != '\r' && data != '\n')
{
rxBuffer[rxIndex++] = data;
if (rxIndex >= BUFFER_SIZE)
rxIndex = 0;
}
if (data == '\n')
{
rxComplete = 1;
}
}
}
作者水平有限,不足之处欢迎指正。