[STM32]中断编程入门

[STM32]中断编程入门

一、中断

  • 中断:在主程序运行过程中,出现了特定的中断触发条件(中断源),使得CPU暂停当前正在运行的程序,转而去处理中断程序,处理完成后又返回原来被暂停的位置继续运行
  • 中断优先级:当有多个中断源同时申请中断时,CPU会根据中断源的轻重缓急进行裁决,优先响应更加紧急的中断源
  • 中断嵌套:当一个中断程序正在运行时,又有新的更高优先级的中断源申请中断,CPU再次暂停当前中断程序,转而去处理新的中断程序,处理完成后依次进行返回

1.中断执行流程

在这里插入图片描述

在这里插入图片描述

2.STM32中断

  • 68个可屏蔽中断通道,包含EXTI、TIM、ADC、USART、SPI、I2C、RTC等多个外设。
  • 使用NVIC统一管理中断,每个中断通道都拥有16个可编程的优先等级,可对优先级进行分组,进一步设置抢占优先级和响应优先级

在这里插入图片描述

3.NVIC

a.基本结构

在这里插入图片描述

b. NVIC优先级分组
  • NVIC的中断优先级由优先级寄存器的4位(0~15)决定,这4位可以进行切分,分为高n位的抢占优先级和低4-n位的响应优先级
  • 抢占优先级高的可以中断嵌套,响应优先级高的可以优先排队,抢占优先级和响应优先级均相同的按中断号排队
分组方式抢占优先级响应优先级
分组00位,取值为04位,取值为0~15
分组11位,取值为0~13位,取值为0~7
分组22位,取值为0~32位,取值为0~3
分组33位,取值为0~71位,取值为0~1
分组44位,取值为0~150位,取值为0

4.EXTI

  • EXTI(Extern Interrupt)外部中断
  • EXTI可以监测指定GPIO口的电平信号,当其指定的GPIO口产生电平变化时,EXTI将立即向NVIC发出中断申请,经过NVIC裁决后即可中断CPU主程序,使CPU执行EXTI对应的中断程序
  • 支持的触发方式:上升沿/下降沿/双边沿/软件触发
  • 支持的GPIO口:所有GPIO口,但相同的Pin不能同时触发中断
  • 通道数:16个GPIO_Pin,外加PVD输出、RTC闹钟、USB唤醒、以太网唤醒
  • 触发响应方式:中断响应/事件响应
a.EXTI基本结构

在这里插入图片描述

b.EXTI框图

在这里插入图片描述

5.AFIO复用IO口

  • AFIO主要用于引脚复用功能的选择和重定义
  • 在STM32中,AFIO主要完成两个任务:复用功能引脚重映射、中断引脚选择

在这里插入图片描述

二、实验

1.点灯+串口发送数据

1)用stm32F103核心板的GPIOA端一管脚接一个LED,GPIOB端口一引脚接一个开关(用杜邦线模拟代替)。采用中断模式编程,当开关接高电平时,LED亮灯;接低电平时,LED灭灯。如果完成后,尝试在main函数while循环中加入一个串口每隔1s 发送一次字符的代码片段,观察按键中断对串口发送是否会带来干扰或延迟。

1)外部中断初始化

EXIT.C与EXIT.H文件如下:

#include "stm32f10x.h" 
#include "LED.h"
void EXIT_key_Init(void)
{
	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);		
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);		

	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOB, &GPIO_InitStructure);						

	GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource14);
	
	EXTI_InitTypeDef EXTI_InitStructure;						
	EXTI_InitStructure.EXTI_Line = EXTI_Line14;					
	EXTI_InitStructure.EXTI_LineCmd = ENABLE;					
	EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;			
	EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;		
	EXTI_Init(&EXTI_InitStructure);								

	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);				
	NVIC_InitTypeDef NVIC_InitStructure;						
	NVIC_InitStructure.NVIC_IRQChannel = EXTI15_10_IRQn;		
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;				
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;	
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;			
	NVIC_Init(&NVIC_InitStructure);							
	
	
}

#ifndef __EXIT_H
#define __EXIT_H

void EXIT_key_Init(void);


#endif

main函数中点灯(封装调用,详见上个博客:串口通信入门)+发送数据

#include "stm32f10x.h"                
#include "Delay.h"
#include "EXIT.h"
#include "LED.h"
#include "Serial.h"
int main(void)
{
	
	LED_Init();
	Serial_Init();
	GPIO_ResetBits(GPIOC,GPIO_Pin_13);
	EXIT_key_Init();
	
	while (1)
	{
		Delay_ms(1000);	
	Serial_Printf("HELLO WINDOWS");
	Serial_Printf("\r\n");
	}
}
void EXTI15_10_IRQHandler(void)
{
	if (EXTI_GetITStatus(EXTI_Line14) == SET)		
	{
		
		if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_14) == 0)
		{
				LED_Turn();	
		}
		EXTI_ClearITPendingBit(EXTI_Line14);		
													
	}
}

结果:点灯与发送数据不会互相干扰

在这里插入图片描述

2.发送与接收——字符中断

只需要Serial.c和Serial.h文件完成实验,修改中断函数,定义标志位开启或者停止发送数据,如下:

#include "stm32f10x.h"                 
#include <stdio.h>
#include <stdarg.h>
volatile uint8_t sending_enabled = 1; 
uint8_t Serial_RxData;		
uint8_t Serial_RxFlag;		
void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	USART_InitTypeDef USART_InitStructure;					
	USART_InitStructure.USART_BaudRate = 9600;				
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;	
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;	
	USART_InitStructure.USART_Parity = USART_Parity_No;		
	USART_InitStructure.USART_StopBits = USART_StopBits_1;	
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;		
	USART_Init(USART1, &USART_InitStructure);				
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);			
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);			
	NVIC_InitTypeDef NVIC_InitStructure;					
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;		
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;		
	NVIC_Init(&NVIC_InitStructure);						
	USART_Cmd(USART1, ENABLE);							
}
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);		
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	
	
}
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)		
	{
		Serial_SendByte(Array[i]);		
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);		
	}
}
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;	
	while (Y --)			
	{
		Result *= X;		
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)		
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');	
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);			
	return ch;
}

void Serial_Printf(char *format, ...)
{
	char String[100];				
	va_list arg;					
	va_start(arg, format);			
	vsprintf(String, format, arg);	
	va_end(arg);					
	Serial_SendString(String);		
}

uint8_t Serial_GetRxFlag(void)
{
	if (Serial_RxFlag == 1)			
	{
		Serial_RxFlag = 0;
		return 1;					
	}
	return 0;					
}

uint8_t Serial_GetRxData(void)
{
	return Serial_RxData;	
	
}
void send_hello_windows(void) {
    if (sending_enabled) {
			 Serial_Printf("HELLO WINDOWS");
            Serial_Printf("\r\n");}}

						
void USART1_IRQHandler(void)
{
	if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)		
	{
		Serial_RxData = USART_ReceiveData(USART1);	
		
	Serial_RxFlag = 1;										
		USART_ClearITPendingBit(USART1, USART_IT_RXNE);																	
		if(Serial_RxData==0x73)
			{
			Serial_Printf("\r\nStop stm32!\r\n"); 
				sending_enabled = 0;
			}else if (Serial_RxData == 't') {
           sending_enabled = 1;
        }													
	}
}

main.c

#include "stm32f10x.h"                 
#include "Delay.h"
#include "Serial.h"

uint8_t RxData;		

int main(void)
{
    Serial_Init();
    while (1)
    {
       send_hello_windows();
			Delay_ms(500);
    }
}

结果:

在这里插入图片描述

3.发送与接收——字符串中断

Serial.c函数做如下修改,main不变:

#include "stm32f10x.h"                 
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
volatile uint8_t sending_enabled = 1; 
char received_string[20] = {0}; 
uint8_t string_index = 0;     
uint8_t Serial_RxFlag = 0;				
void Serial_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);	
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOA, &GPIO_InitStructure);					
	USART_InitTypeDef USART_InitStructure;					
	USART_InitStructure.USART_BaudRate = 9600;				
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;	
	USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;	
	USART_InitStructure.USART_Parity = USART_Parity_No;		
	USART_InitStructure.USART_StopBits = USART_StopBits_1;	
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;		
	USART_Init(USART1, &USART_InitStructure);				
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);			
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);			
	NVIC_InitTypeDef NVIC_InitStructure;					
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;		
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;			
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;		
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;		
	NVIC_Init(&NVIC_InitStructure);						
	USART_Cmd(USART1, ENABLE);							
}
void Serial_SendByte(uint8_t Byte)
{
	USART_SendData(USART1, Byte);		
	while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);	
	
}
void Serial_SendArray(uint8_t *Array, uint16_t Length)
{
	uint16_t i;
	for (i = 0; i < Length; i ++)		
	{
		Serial_SendByte(Array[i]);		
	}
}
void Serial_SendString(char *String)
{
	uint8_t i;
	for (i = 0; String[i] != '\0'; i ++)
	{
		Serial_SendByte(String[i]);		
	}
}
uint32_t Serial_Pow(uint32_t X, uint32_t Y)
{
	uint32_t Result = 1;	
	while (Y --)			
	{
		Result *= X;		
	}
	return Result;
}

void Serial_SendNumber(uint32_t Number, uint8_t Length)
{
	uint8_t i;
	for (i = 0; i < Length; i ++)		
	{
		Serial_SendByte(Number / Serial_Pow(10, Length - i - 1) % 10 + '0');	
	}
}

int fputc(int ch, FILE *f)
{
	Serial_SendByte(ch);			
	return ch;
}

void Serial_Printf(char *format, ...)
{
	char String[100];				
	va_list arg;					
	va_start(arg, format);			
	vsprintf(String, format, arg);	
	va_end(arg);					
	Serial_SendString(String);		
}

uint8_t Serial_GetRxFlag(void)
{
	if (Serial_RxFlag == 1)			
	{
		Serial_RxFlag = 0;
		return 1;					
	}
	return 0;					
}



void send_hello_windows(void) {
    if (sending_enabled) {
			 Serial_Printf("HELLO WINDOWS");
            Serial_Printf("\r\n");}}

						

						
void USART1_IRQHandler(void)
{
    if (USART_GetITStatus(USART1, USART_IT_RXNE) == SET)
    {
        char received_char = USART_ReceiveData(USART1);
        USART_ClearITPendingBit(USART1, USART_IT_RXNE);

        if (received_char == '\n' || received_char == '\r')
        {
            received_string[string_index] = '\0'; 
            string_index = 0;                     
            if (strcmp(received_string, "stop stm32!") == 0)
            {
                sending_enabled = 0; 
                Serial_Printf("\r\nStop stm32!\r\n");
            }
            else if (strcmp(received_string, "go stm32!") == 0)
            {
                sending_enabled = 1; 
                Serial_Printf("Go on stm32!\r\n");
            }
            else
            {
                
            }
        }
        else
        {
            
            received_string[string_index++] = received_char;
            if (string_index >= 19)
            {
                // ?????????,???????
                string_index = 0;
                memset(received_string, 0, sizeof(received_string));
            }
        }
    }
}

结果:

在这里插入图片描述

三、总结

本次学习使用中断点灯+发送接收数据。初学多有不足,请多指教。

  • 19
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值