stm32f4单片机设计电子密码锁的代码
时间: 2023-08-03 13:04:37 浏览: 84
毕业设计:基于STM32单片机电子密码锁控制电路设计资料(BOM表、电路图、程序源代码).rar
5星 · 资源好评率100%
好的,以下是基于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灯并锁定系统一段时间。
阅读全文