这次把中断相关的知识学习了
代码的目的是按键中断,按下按键1会逆向流水灯,按下按钮2会正向流水灯,按键1的抢占优先级和中断优先级都更高
代码部分:
counsensor.c:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "led.h"
uint16_t CountSensor_Count;
int a;
void CountSensor_Init(void)
{
LED_Init();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitTypeDef GPIO_B_IS;
GPIO_B_IS.GPIO_Mode = GPIO_Mode_IPU;
GPIO_B_IS.GPIO_Pin = GPIO_Pin_1 | GPIO_Pin_11;
GPIO_B_IS.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_B_IS);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource11);
EXTI_InitTypeDef EXTI_InitStructure;
EXTI_InitStructure.EXTI_Line = EXTI_Line1 | EXTI_Line11;
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 = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; // 0-3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; // 0-3
NVIC_Init(&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);
}
uint16_t CountSensor_Get(void)
{
return CountSensor_Count;
}
void EXTI1_IRQHandler()
{
Delay_ms(10);
if (EXTI_GetITStatus(EXTI_Line1) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0)
{
LED2_ON();
for (a = 0; a <= 1000000; a++);
LED2_OFF();
for (a = 0; a <= 1000000; a++);
LED1_ON();
for (a = 0; a <= 1000000; a++);
LED1_OFF();
for (a = 0; a <= 1000000; a++);
LED0_ON();
for (a = 0; a <= 1000000; a++);
LED0_OFF();
CountSensor_Count ++;
}
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI15_10_IRQHandler(void)
{
Delay_ms(10);
if (EXTI_GetITStatus(EXTI_Line11) == SET)
{
/*如果出现数据乱跳的现象,可再次判断引脚电平,以避免抖动*/
if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_11) == 0)
{
LED0_ON();
for (a = 0; a <= 1000000; a++);
LED0_OFF();
for (a = 0; a <= 1000000; a++);
LED1_ON();
for (a = 0; a <= 1000000; a++);
LED1_OFF();
for (a = 0; a <= 1000000; a++);
LED2_ON();
for (a = 0; a <= 1000000; a++);
LED2_OFF();
CountSensor_Count --;
}
EXTI_ClearITPendingBit(EXTI_Line11);
}
}
代码很普通,唯一值得注意的是EXTI_Line5-9
是对应于 EXTI9_5_IRQn,EXTI_Line10-15
是对应于 EXTI15_10_IRQn
,led在之前流水灯的代码里有,要注意的是在声明时不用声明
void EXTI1_IRQHandler()
main.c
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include "CountSensor.h"
#include "LED.h"
int main(void)
{
OLED_Init();
CountSensor_Init();
LED_Init();
OLED_ShowString(1, 1, "Count:");
LED1_ON();
Delay_ms(50);
LED1_OFF();
while (1)
{
OLED_ShowNum(1, 7, CountSensor_Get(), 5);
// if(CountSensor_Get()%2==0){
// LED0_ON();
// LED1_OFF();
// }
// else{
// LED0_OFF();
// LED1_ON();
// }
}
}
附接线图:
图其实和之前的流水灯一样
实机演示图:
以后再改为gif吧