采用STM32F103单片机为主控芯片在SM332cubeIDE软件中实现以下系统功能设计模块功能代码编写: 1. 在矩阵键盘输入密码,如果密码正确则OLED 屏幕显示开锁成功,智能锁也会被打开。 2. 在矩阵键盘输入密码,密码输入错误则蜂鸣器发出警告。 3. 在矩阵键盘第一次输入密码错误后,本人忘记密码,则需要修改密码再次开锁并对密码两次确认。 4. 在开智能锁的过程中,设备突然断电,密码掉电保存功能,重新加电后系统能恢复原来的工作状态。 5. 手机利用WIFI模块输入密码(WIFI模块功能)。
时间: 2023-12-10 07:42:28 浏览: 38
以下是代码实现:
1. 输入密码并开锁
```c
#include "stm32f1xx_hal.h"
#include "keypad.h"
#include "oled.h"
#include "lock.h"
int main(void)
{
HAL_Init();
keypad_init();
oled_init();
lock_init();
char password[4] = "1234"; // 设置密码为1234
while (1)
{
char input[4];
keypad_scan(input); // 扫描键盘输入
if (strcmp(input, password) == 0) // 比较密码是否正确
{
oled_show_message("Unlock success!"); // OLED屏幕显示开锁成功
lock_open(); // 打开智能锁
}
}
}
```
2. 密码输入错误时蜂鸣器发出警告
```c
#include "stm32f1xx_hal.h"
#include "keypad.h"
#include "oled.h"
#include "lock.h"
#include "beeper.h"
int main(void)
{
HAL_Init();
keypad_init();
oled_init();
lock_init();
beeper_init();
char password[4] = "1234"; // 设置密码为1234
int error_count = 0; // 输入密码错误计数器
while (1)
{
char input[4];
keypad_scan(input); // 扫描键盘输入
if (strcmp(input, password) == 0) // 比较密码是否正确
{
oled_show_message("Unlock success!"); // OLED屏幕显示开锁成功
lock_open(); // 打开智能锁
}
else
{
error_count++; // 输入密码错误计数器加1
if (error_count >= 3) // 如果密码输入错误3次
{
beeper_on(); // 蜂鸣器发出警告声
HAL_Delay(1000); // 延时1秒
beeper_off(); // 关闭蜂鸣器
error_count = 0; // 清空错误计数器
}
}
}
}
```
3. 忘记密码时需要修改密码
```c
#include "stm32f1xx_hal.h"
#include "keypad.h"
#include "oled.h"
#include "lock.h"
#include "beeper.h"
int main(void)
{
HAL_Init();
keypad_init();
oled_init();
lock_init();
beeper_init();
char password[4] = "1234"; // 设置密码为1234
int error_count = 0; // 输入密码错误计数器
int forget_password = 0; // 忘记密码标志位
while (1)
{
char input[4];
keypad_scan(input); // 扫描键盘输入
if (strcmp(input, password) == 0) // 比较密码是否正确
{
oled_show_message("Unlock success!"); // OLED屏幕显示开锁成功
lock_open(); // 打开智能锁
}
else
{
error_count++; // 输入密码错误计数器加1
if (error_count >= 3 && forget_password == 0) // 如果密码输入错误3次且没有忘记密码
{
beeper_on(); // 蜂鸣器发出警告声
HAL_Delay(1000); // 延时1秒
beeper_off(); // 关闭蜂鸣器
// 修改密码
oled_show_message("Forget password!"); // OLED屏幕显示忘记密码
char new_password[4];
keypad_scan(new_password); // 扫描新密码
oled_show_message("Confirm password!"); // OLED屏幕显示确认密码
char confirm_password[4];
keypad_scan(confirm_password); // 扫描确认密码
if (strcmp(new_password, confirm_password) == 0) // 比较新密码和确认密码是否一致
{
strcpy(password, new_password); // 将新密码更新为密码
oled_show_message("Password changed!"); // OLED屏幕显示密码修改成功
forget_password = 1; // 设置忘记密码标志位
error_count = 0; // 清空错误计数器
}
else
{
oled_show_message("Password mismatch!"); // OLED屏幕显示新密码和确认密码不一致
error_count = 0; // 清空错误计数器
}
}
}
}
}
```
4. 密码掉电保存功能
```c
#include "stm32f1xx_hal.h"
#include "keypad.h"
#include "oled.h"
#include "lock.h"
#include "beeper.h"
#include "flash.h"
int main(void)
{
HAL_Init();
keypad_init();
oled_init();
lock_init();
beeper_init();
flash_init();
char password[4]; // 定义密码
flash_read(password); // 从Flash中读取密码
int error_count = 0; // 输入密码错误计数器
int forget_password = 0; // 忘记密码标志位
while (1)
{
char input[4];
keypad_scan(input); // 扫描键盘输入
if (strcmp(input, password) == 0) // 比较密码是否正确
{
oled_show_message("Unlock success!"); // OLED屏幕显示开锁成功
lock_open(); // 打开智能锁
}
else
{
error_count++; // 输入密码错误计数器加1
if (error_count >= 3 && forget_password == 0) // 如果密码输入错误3次且没有忘记密码
{
beeper_on(); // 蜂鸣器发出警告声
HAL_Delay(1000); // 延时1秒
beeper_off(); // 关闭蜂鸣器
// 修改密码
oled_show_message("Forget password!"); // OLED屏幕显示忘记密码
char new_password[4];
keypad_scan(new_password); // 扫描新密码
oled_show_message("Confirm password!"); // OLED屏幕显示确认密码
char confirm_password[4];
keypad_scan(confirm_password); // 扫描确认密码
if (strcmp(new_password, confirm_password) == 0) // 比较新密码和确认密码是否一致
{
strcpy(password, new_password); // 将新密码更新为密码
flash_write(password); // 将密码保存到Flash中
oled_show_message("Password changed!"); // OLED屏幕显示密码修改成功
forget_password = 1; // 设置忘记密码标志位
error_count = 0; // 清空错误计数器
}
else
{
oled_show_message("Password mismatch!"); // OLED屏幕显示新密码和确认密码不一致
error_count = 0; // 清空错误计数器
}
}
}
}
}
```
5. 手机利用WIFI模块输入密码
```c
#include "stm32f1xx_hal.h"
#include "oled.h"
#include "lock.h"
#include "wifi.h"
#include "beeper.h"
int main(void)
{
HAL_Init();
oled_init();
lock_init();
wifi_init();
beeper_init();
char password[4] = "1234"; // 设置密码为1234
while (1)
{
char input[4];
wifi_scan(input); // 扫描WIFI输入
if (strcmp(input, password) == 0) // 比较密码是否正确
{
oled_show_message("Unlock success!"); // OLED屏幕显示开锁成功
lock_open(); // 打开智能锁
}
else
{
beeper_on(); // 蜂鸣器发出警告声
HAL_Delay(1000); // 延时1秒
beeper_off(); // 关闭蜂鸣器
}
}
}
```