嵌入式STM32课程实验-外部中断控制LED灯

课程实验要求:通过配置外部中断实现按键按下,LED灯的状态由闪烁->保持->点亮->熄灭如此循环

步骤:配置好中断、LED的GPIO口、通过中断服务函数改变标志位,在主循环里判断标志位的变量。

1、EXTI.c文件

#include "stm32f10x.h"                  // Device header
#include "LED.h"

void MyEXTI_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);    		//开启AFIO时钟
	
	GPIO_InitTypeDef GPIO_InitStructure;
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	
	GPIO_EXTILineConfig(GPIO_PortSourceGPIOE, GPIO_PinSource4);	//实际操作AFIO

	EXTI_InitTypeDef EXTI_InitStructure;          					//外部中断配置
	EXTI_InitStructure.EXTI_Line = EXTI_Line4;
	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 = EXTI4_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
	NVIC_Init(&NVIC_InitStructure);
}

extern uint8_t mode;

void EXTI4_IRQHandler(void)
{
	if(EXTI_GetITStatus(EXTI_Line4) == SET)
	{
        for(int i = 0; i < 1000; i ++);
		if(GPIO_ReadInputDataBit(GPIOE,GPIO_Pin_4) == RESET)
		{
			mode++;
			if(mode > 4)
				mode = 1;
		}			
	}
	EXTI_ClearITPendingBit(EXTI_Line4);
}

2、LED.c文件

#include "stm32f10x.h"                  // Device header

void LED_Init(void)										    //LED初始化函数
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE);   //APB2外设时钟控制(接在GPIOA上,使能)
	
	GPIO_InitTypeDef GPIO_InitStructure;                    //定义一个结构体变量
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;        //GPIO模式:推挽输出
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_8;  //用到GPIO1、2号口
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;       //GPIO的速度
	GPIO_Init(GPIOG,&GPIO_InitStructure);     		        //初始化GPIOA外设,结构体变量名取地址(地址传递)
	
	GPIO_SetBits(GPIOG, GPIO_Pin_6 | GPIO_Pin_8);           //GPIOA 1、2口设置为高电平
}

void LED1_ON(void)
{
	GPIO_ResetBits(GPIOG,GPIO_Pin_6);
}

void LED1_OFF(void)
{
	GPIO_SetBits(GPIOG,GPIO_Pin_6);
}

void LED1_Turn(void)
{
	if(GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_6)==0)         //读取现在的输出值
	{
		GPIO_SetBits(GPIOG, GPIO_Pin_6);
	}
	else
	{
		GPIO_ResetBits(GPIOG,GPIO_Pin_6);
	}
}

void LED2_ON(void)
{
	GPIO_ResetBits(GPIOG,GPIO_Pin_8);
}

void LED2_OFF(void)
{
	GPIO_SetBits(GPIOG,GPIO_Pin_8);
}

void LED2_Turn(void)
{
	if(GPIO_ReadInputDataBit(GPIOG, GPIO_Pin_8)==0)
	{
		GPIO_SetBits(GPIOG, GPIO_Pin_8);
	}
	else
	{
		GPIO_ResetBits(GPIOG,GPIO_Pin_8);
	}
}

3、main.c文件

#include "stm32f10x.h"                  // Device header
#include "LED.h"
#include "MyEXTI.h"

uint8_t mode;

int main(void)
{
	LED_Init();
	MyEXTI_Init();
	
	while(1)                       
	{
		switch(mode)
		{
			case 1:
				LED1_Turn();
			break;
			case 2:

			break;
			case 3:
				LED1_ON();
			break;
			case 4:
				LED1_OFF();
			break;
		}
	}
}

.h文件自己添加或不用也可以。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

慢慢丶丶

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

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

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

打赏作者

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

抵扣说明:

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

余额充值