中断编程入门篇

一、中断原理-

中断编程原理是指在计算机系统中,当发生某种特定事件或条件时,系统会暂时中止当前正在执行的程序,转而执行另一个特定的程序,然后在执行完这个特定程序后,再返回到原来的程序继续执行。这种机制可以使计算机系统在处理多任务时更加高效和灵活。

中断编程的实现原理主要包括以下几个步骤:

中断请求:当某种特定事件或条件发生时,硬件会发出一个中断请求信号,通知处理器暂停当前执行的程序。

中断处理程序:处理器接收到中断请求信号后,会跳转到事先设定好的中断处理程序,执行其中的指令。

保存现场:在中断处理程序执行之前,处理器会将当前程序的状态(如程序计数器、寄存器值等)保存到内存中,以便在中断处理程序执行完毕后能够恢复到原来的状态。

执行中断处理程序:中断处理程序会根据中断类型进行相应的处理,可能包括处理中断请求、保存现场、执行特定的操作等。

恢复现场:在中断处理程序执行完毕后,处理器会将之前保存的程序状态恢复,然后返回到原来的程序继续执行。

中断编程可以帮助计算机系统实现多任务处理、实时响应和异常处理等功能,提高系统的效率和可靠性。

外部中断优先级:

外部中断的优先级是根据中断源的硬件优先级决定的,通常是由处理器的中断控制器决定。在处理器中断控制器中,每个外部中断源都有一个特定的优先级,当多个中断同时发生时,控制器会根据这些优先级来确定哪个中断应该被优先处理。通常情况下,硬件优先级高的中断会先被处理,而优先级低的中断会被延迟处理。

二、实验内容

1、使用按键控制LED灯的亮灭

实验要求:

用stm32F103核心板的GPIOA端一管脚接一个LED,GPIOB端口一引脚接一个开关(用杜邦线模拟代替)。采用中断模式编程,当开关接高电平时,LED亮灯;接低电平时,LED灭灯。

实验代码:

LED.c:

#include "stm32f10x.h" // Device header

/**

* 函 数:LED初始化

* 参 数:无

* 返 回 值:无

*/

void LED_Init(void)

{

/*开启时钟*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //开启GPIOA的时钟

/*GPIO初始化*/

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStructure); //将PA1和PA2引脚初始化为推挽输出

/*设置GPIO初始化后的默认电平*/

GPIO_SetBits(GPIOA, GPIO_Pin_1 | GPIO_Pin_2); //设置PA1和PA2引脚为高电平

}

/**

* 函 数:LED1开启

* 参 数:无

* 返 回 值:无

*/

void LED1_ON(void)

{

GPIO_ResetBits(GPIOA, GPIO_Pin_1); //设置PA1引脚为低电平

}

/**

* 函 数:LED1关闭

* 参 数:无

* 返 回 值:无

*/

void LED1_OFF(void)

{

GPIO_SetBits(GPIOA, GPIO_Pin_1); //设置PA1引脚为高电平

}

/**

* 函 数:LED1状态翻转

* 参 数:无

* 返 回 值:无

*/

void LED1_Turn(void)

{

if (GPIO_ReadOutputDataBit(GPIOA, GPIO_Pin_1) == 0) //获取输出寄存器的状态,如果当前引脚输出低电平

{

GPIO_SetBits(GPIOA, GPIO_Pin_1); //则设置PA1引脚为高电平

}

else //否则,即当前引脚输出高电平

{

GPIO_ResetBits(GPIOA, GPIO_Pin_1); //则设置PA1引脚为低电平

}

}

LED.h:

#ifndef __LED_H

#define __LED_H

void LED_Init(void);

void LED1_ON(void);

void LED1_OFF(void);

void LED1_Turn(void);

#endif

Key.c:

#include "stm32f10x.h" // Device header

#include "Delay.h"

/**

* 函 数:按键初始化

* 参 数:无

* 返 回 值:无

*/

void Key_Init(void)

{

/*开启时钟*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //开启GPIOB的时钟

/*GPIO初始化*/

GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1 ;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure); //将PB1和PB11引脚初始化为上拉输入

}

/**

* 函 数:按键获取键码

* 参 数:无

* 返 回 值:按下按键的键码值,范围:0~2,返回0代表没有按键按下

* 注意事项:此函数是阻塞式操作,当按键按住不放时,函数会卡住,直到按键松手

*/

uint8_t Key_GetNum(void)

{

uint8_t KeyNum = 0; //定义变量,默认键码值为0

if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) //读PB1输入寄存器的状态,如果为0,则代表按键1按下

{

Delay_ms(20); //延时消抖

while (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0); //等待按键松手

Delay_ms(20); //延时消抖

KeyNum = 1; //置键码为1

}

return KeyNum; //返回键码值,如果没有按键按下,所有if都不成立,则键码为默认值0

}

Key.h:

#ifndef __KEY_H

#define __KEY_H

void Key_Init(void);

uint8_t Key_GetNum(void);

#endif

main.c

#include "stm32f10x.h" // Device header

#include "Delay.h"

#include "LED.h"

#include "Key.h"

uint8_t KeyNum; //定义用于接收按键键码的变量

int main(void)

{

/*模块初始化*/

LED_Init(); //LED初始化

Key_Init(); //按键初始化

while (1)

{

KeyNum = Key_GetNum(); //获取按键键码

if (KeyNum == 1) //按键1按下

{

LED1_Turn(); //LED1翻转

}

}

}

效果图:

LED在A1端口,按键开关在B1端口

由于没有按键,所以用杜邦线模拟代替按键开关。

2、采用串口中断方式实现串口通信

(1)当stm32接收到1个字符“s”时,停止持续发送“hello windows!”; 当接收到1个字符“t”时,持续发送“hello windows!”

代码:

main.c:

#include "stm32f10x.h" // Device header

#include

#include "Serial.h"

#include "Delay.h"

#include

int main(void)

{

Serial_Init();

while(1)

{

if(a==1)

{

Serial_SendString("hello windows!\n");

Delay_ms(800);

}

else if(a==0)

{

}

}

}

Serial.c:

#include "stm32f10x.h" // Device header

#include "stdio.h"

#include

#include

uint8_t Serial_RxFlag;//定义一个接收标志位

int a=0;

void Serial_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

USART_InitTypeDef USART_InitStruct;

NVIC_InitTypeDef NVIC_InitStructure;

//开启时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);

//输出

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStruct);

//串口输入

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStruct);

//串口初始化

USART_InitStruct.USART_BaudRate = 9600;//设置波特率

USART_InitStruct.USART_HardwareFlowControl =USART_HardwareFlowControl_None;

USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//发送 | 接收

USART_InitStruct.USART_Parity = USART_Parity_No;//无校验位

USART_InitStruct.USART_StopBits = USART_StopBits_1;//1位停止位

USART_InitStruct.USART_WordLength = USART_WordLength_8b;

USART_Init(USART1,&USART_InitStruct);

//开启中断

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

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);

}

uint8_t Serial_GetRxFlag(void) //用来返回一个是否接收完一个字符串的标志

{

if(Serial_RxFlag == 1)

{

Serial_RxFlag = 0;

return 1;

}

return 0;

}

void Serial_SendByte(uint8_t Byte)

{

USART_SendData(USART1,Byte);

while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

}

//发送字符串

void Serial_SendString(char *String)

{

uint8_t i;

for(i=0;String[i]!=0;i++)

{

Serial_SendByte(String[i]);

}

}

//中断函数

void USART1_IRQHandler()

{

if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)

{

u8 RxData = USART_ReceiveData(USART1);

if(RxData=='s')//如果是s,则返回全局变量0

{a=0;

}

if(RxData=='t')//如果是t,则返回全局变量1

{a=1;

}

USART_ClearITPendingBit(USART1,USART_IT_RXNE);

}

}

Serial.h:

#ifndef _Serial_H

#define _Serial_H

extern int a;//这里的a是全局变量,所以要加extern

void Serial_Init(void);

void Serial_SendByte(uint8_t Byte);

void Serial_SendString(char *String);

uint8_t Serial_GetRxFlag(void);

#include

#endif

实验效果:

main.c:

#include "stm32f10x.h" // Device header

#include

#include "Serial.h"

#include "Delay.h"

#include

int main(void)

{

Serial_Init();

while(1)

{

if(Serial_GetRxFlag()==1)//数组存入完毕标志

{

if(strcmp(Serial_RxPacket,"go stm32!")==0)

{

while(1)

{

Serial_SendString("hello windows!\n");

Delay_ms(800);

if(strcmp(Serial_RxPacket,"stop stm32!")==0)

{

break;

}

}

}

}

}

}

Serial.c:

#include "stm32f10x.h" // Device header

#include "stdio.h"

#include

#include

uint8_t Serial_RxFlag;

char Serial_RxPacket[100];

void Serial_Init(void)

{

GPIO_InitTypeDef GPIO_InitStruct;

USART_InitTypeDef USART_InitStruct;

NVIC_InitTypeDef NVIC_InitStructure;

//开启时钟

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//打开PA9和PA10的时钟

//输出

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStruct);

//串口输入

GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN_FLOATING;

GPIO_InitStruct.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOA, &GPIO_InitStruct);

//串口初始化

USART_InitStruct.USART_BaudRate = 9600;//波特率

USART_InitStruct.USART_HardwareFlowControl =USART_HardwareFlowControl_None;

USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;//TX | RX

USART_InitStruct.USART_Parity = USART_Parity_No;

USART_InitStruct.USART_StopBits = USART_StopBits_1;

USART_InitStruct.USART_WordLength = USART_WordLength_8b;

USART_Init(USART1,&USART_InitStruct);

//开启中断

USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

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);

}

uint8_t Serial_GetRxFlag(void) //用来返回一个是否接收完一个字符串的标志

{

if(Serial_RxFlag == 1)

{

Serial_RxFlag = 0;

return 1;

}

return 0;

}

void Serial_SendByte(uint8_t Byte)

{

USART_SendData(USART1,Byte);

while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);

}

//发送字符串

void Serial_SendString(char *String)

{

uint8_t i;

for(i=0;String[i]!=0;i++)

{

Serial_SendByte(String[i]);

}

}

//中断函数

void USART1_IRQHandler()

{

static u8 number = 0;//数组下标

static u8 Rxstate = 0; //判断是否读取完毕

if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)

{

u8 RxData = USART_ReceiveData(USART1);

if(Rxstate==0)

{

if(RxData=='[')//起始标志符为'['

{

memset(Serial_RxPacket,'\0',sizeof(Serial_RxPacket));

number = 0;

Rxstate=1;

}

}

else if(Rxstate==1)

{

if(RxData==']')//结束标志符为']'

{

Rxstate=0;

Serial_RxFlag=1;

}

else

{

Serial_RxPacket[number] = RxData;

number++;

}

}

USART_ClearITPendingBit(USART1,USART_IT_RXNE);

}

}

Serial.h:

#ifndef _Serial_H

#define _Serial_H

extern int a;//这里的a是全局变量,所以要加extern

void Serial_Init(void);

void Serial_SendByte(uint8_t Byte);

void Serial_SendString(char *String);

uint8_t Serial_GetRxFlag(void);

extern char Serial_RxPacket[100];

#include

#endif