if(EXTI_GetITStatus(KEY2_INT_EXTI_LINE) != RESET) { if(count2<5) { p=p/2; time=time/2; count2++; } if(count2>=5 && count2<10) { p=p*2; time=time*2; count2++; } if(count2>=10) { count2=0; } //清除中断标志位 EXTI_ClearITPendingBit(KEY2_INT_EXTI_LINE); }
时间: 2024-01-12 19:03:16 浏览: 29
这段代码看起来像是 STM32F系列单片机上的外部中断处理函数,当KEY2按键被按下时,会执行相应的处理操作。其中,count2变量的值在5次以内会除以2,同时time也会除以2;在5~10次之间,count2变量的值会乘以2,同时time也会乘以2;当count2大于等于10时,count2会被重置为0。最后,该中断标志位会被清除。
相关问题
写一个stm32单片机的程序:使用4个LED,3个按键,按键1控制正向流水灯,按键2控制反向流水灯,按键3控制流水灯的数量(1个,2个,3个))
本题答案为参考答案,仅供参考。
```c
#include "stm32f10x.h"
#define LED1_PIN GPIO_Pin_0
#define LED2_PIN GPIO_Pin_1
#define LED3_PIN GPIO_Pin_2
#define LED4_PIN GPIO_Pin_3
#define KEY1_PIN GPIO_Pin_4
#define KEY2_PIN GPIO_Pin_5
#define KEY3_PIN GPIO_Pin_6
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
// 使能GPIOA和AFIO时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
// 使能EXTI0和EXTI1中断
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn | EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0x00;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
// 配置LED引脚为推挽输出
GPIO_InitStructure.GPIO_Pin = LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 配置KEY1引脚为上拉输入,并使能其中断
GPIO_InitStructure.GPIO_Pin = KEY1_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource4);
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 配置KEY2引脚为上拉输入,并使能其中断
GPIO_InitStructure.GPIO_Pin = KEY2_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource5);
EXTI_InitStructure.EXTI_Line = EXTI_Line5;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 配置KEY3引脚为上拉输入,并使能其中断
GPIO_InitStructure.GPIO_Pin = KEY3_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
// 初始化流水灯数量为1
uint8_t count = 1;
while (1)
{
// 根据流水灯数量控制LED输出
switch (count)
{
case 1:
GPIO_SetBits(GPIOA, LED1_PIN);
GPIO_ResetBits(GPIOA, LED2_PIN | LED3_PIN | LED4_PIN);
break;
case 2:
GPIO_SetBits(GPIOA, LED1_PIN | LED2_PIN);
GPIO_ResetBits(GPIOA, LED3_PIN | LED4_PIN);
break;
case 3:
GPIO_SetBits(GPIOA, LED1_PIN | LED2_PIN | LED3_PIN);
GPIO_ResetBits(GPIOA, LED4_PIN);
break;
default:
GPIO_SetBits(GPIOA, LED1_PIN | LED2_PIN | LED3_PIN | LED4_PIN);
break;
}
}
}
void EXTI0_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line0) != RESET)
{
// 控制正向流水灯
EXTI_ClearITPendingBit(EXTI_Line0);
}
}
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
// 控制反向流水灯
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line5) != RESET)
{
// 控制流水灯数量
EXTI_ClearITPendingBit(EXTI_Line5);
}
}
```
stm32f4单片机设计电子密码锁的代码
好的,以下是基于STM32F4单片机设计电子密码锁的代码,仅供参考:
```C
#include "stm32f4xx.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_syscfg.h"
#include "misc.h"
#define PASSWORD_SIZE 4 //密码长度
#define PASSWORD "1234" //密码
#define LOCK_TIME 30 //锁定时间
int key_count = 0; //按键次数
int lock_count = 0; //锁定计数器
int lock_flag = 0; //锁定标志位
void GPIO_Configuration(void);
void TIM_Configuration(void);
void EXTI_Configuration(void);
void NVIC_Configuration(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
GPIO_Configuration();
TIM_Configuration();
EXTI_Configuration();
NVIC_Configuration();
while (1)
{
if (lock_flag == 0)
{
if (key_count == PASSWORD_SIZE)
{
if (strcmp(PASSWORD, input_password) == 0)
{
//密码正确
GPIO_SetBits(GPIOG, GPIO_Pin_13);
Delay(5000000);
GPIO_ResetBits(GPIOG, GPIO_Pin_13);
key_count = 0;
}
else
{
//密码错误
key_count = 0;
lock_flag = 1;
}
}
}
else
{
//锁定状态
GPIO_SetBits(GPIOG, GPIO_Pin_14);
Delay(5000000);
GPIO_ResetBits(GPIOG, GPIO_Pin_14);
lock_count++;
if (lock_count >= LOCK_TIME)
{
lock_count = 0;
lock_flag = 0;
}
}
}
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOG, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOE, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOE, &GPIO_InitStructure);
}
void TIM_Configuration(void)
{
TIM_TimeBaseInitTypeDef TIM_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
TIM_InitStructure.TIM_Period = 49;
TIM_InitStructure.TIM_Prescaler = 8399;
TIM_InitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_InitStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM2, &TIM_InitStructure);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
TIM_Cmd(TIM2, ENABLE);
}
void EXTI_Configuration(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource4);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource5);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource6);
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOE, EXTI_PinSource7);
EXTI_InitStructure.EXTI_Line = EXTI_Line4 | EXTI_Line5 | EXTI_Line6 | EXTI_Line7;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(__IO uint32_t nCount)
{
while (nCount--)
{
}
}
void EXTI4_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line4) != RESET)
{
Delay(10000);
if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_4) == SET)
{
//按键按下
input_password[key_count++] = '1';
}
EXTI_ClearITPendingBit(EXTI_Line4);
}
}
void EXTI9_5_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line5) != RESET)
{
Delay(10000);
if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_5) == SET)
{
//按键按下
input_password[key_count++] = '2';
}
EXTI_ClearITPendingBit(EXTI_Line5);
}
else if (EXTI_GetITStatus(EXTI_Line6) != RESET)
{
Delay(10000);
if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_6) == SET)
{
//按键按下
input_password[key_count++] = '3';
}
EXTI_ClearITPendingBit(EXTI_Line6);
}
else if (EXTI_GetITStatus(EXTI_Line7) != RESET)
{
Delay(10000);
if (GPIO_ReadInputDataBit(GPIOE, GPIO_Pin_7) == SET)
{
//按键按下
input_password[key_count++] = '4';
}
EXTI_ClearITPendingBit(EXTI_Line7);
}
}
void TIM2_IRQHandler(void)
{
if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
{
lock_count++;
if (lock_count >= LOCK_TIME)
{
lock_count = 0;
lock_flag = 0;
}
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
```
这份代码主要实现了基于STM32F4单片机的电子密码锁,通过按键输入密码,判断密码是否正确,如果密码正确则亮起绿色LED灯,如果密码错误则亮起红色LED灯并锁定系统一段时间。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)