中断编程入门

学习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;
        }
    }
}

作者水平有限,不足之处欢迎指正。

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

珏决

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值