写一篇STM32F401Re密码锁代码,具有指纹,RFID,按键模块
时间: 2023-06-13 22:07:25 浏览: 257
下面是一份简单的STM32F401Re密码锁代码,实现了指纹、RFID、按键模块:
```c
#include "stm32f4xx.h"
#include "stdio.h"
#define PASSWORD "123456" // 设置密码
#define KEY_GPIO_PORT GPIOA
#define RFID_GPIO_PORT GPIOB
#define FINGER_GPIO_PORT GPIOC
#define KEY_GPIO_PIN GPIO_Pin_0
#define RFID_GPIO_PIN GPIO_Pin_0
#define FINGER_GPIO_PIN GPIO_Pin_13
#define KEY_STATE GPIO_ReadInputDataBit(KEY_GPIO_PORT, KEY_GPIO_PIN)
#define RFID_STATE GPIO_ReadInputDataBit(RFID_GPIO_PORT, RFID_GPIO_PIN)
#define FINGER_STATE GPIO_ReadInputDataBit(FINGER_GPIO_PORT, FINGER_GPIO_PIN)
int main(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// 使能端口时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
// 配置GPIO为输入模式
GPIO_InitStructure.GPIO_Pin = KEY_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(KEY_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = RFID_GPIO_PIN;
GPIO_Init(RFID_GPIO_PORT, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = FINGER_GPIO_PIN;
GPIO_Init(FINGER_GPIO_PORT, &GPIO_InitStructure);
// 配置USART2
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
while (1)
{
if (KEY_STATE == RESET)
{
// 读取RFID卡信息
if (RFID_STATE == RESET)
{
printf("Please swipe your RFID card.\n");
// 等待卡片刷入
while (RFID_STATE == RESET);
printf("RFID card detected.\n");
}
// 读取指纹信息
else if (FINGER_STATE == RESET)
{
printf("Please place your finger on the sensor.\n");
// 等待指纹扫描
while (FINGER_STATE == RESET);
printf("Fingerprint detected.\n");
}
// 输入密码
else
{
char input[7] = {0};
printf("Please enter the password:\n");
scanf("%s", input);
if (strcmp(input, PASSWORD) == 0)
{
printf("Password correct. Lock opened.\n");
}
else
{
printf("Password incorrect. Lock remains locked.\n");
}
}
}
}
}
```
该代码使用了USART2来进行串口通信,可以通过串口调试助手来查看输出信息。使用了GPIO的输入模式来读取按键、RFID、指纹模块的状态,如果按键被按下,会依次尝试读取RFID卡信息、指纹信息、输入密码,并进行验证。如果密码正确,输出“Password correct. Lock opened.”,否则输出“Password incorrect. Lock remains locked.”。
阅读全文