目录
一、主要功能
基于STM32单片机,六个按键,分别代表指纹、蓝牙、刷卡的正确进门与错误进门;
比如第一个按键按下,表示指纹正确,OLED显示指纹正确,开门,
第二个按键按下的话,则指纹错误,OLED显示指纹错误,请重试,
第二个按键连续按三次以上,则OLED显示开门失败,此时启动报警系统,
即蜂鸣器报警。同理,蓝牙与刷卡的操作同样如此。
二、硬件资源
基于KEIL5编写C++代码,PROTEUS8.15进行仿真,全部资源在页尾,提供安装包。
编辑
三、程序编程
#include "main.h"#include "gpio.h"#include "./HAL/key/key.h"#include "./HAL/OLED/OLED\_NEW.H"void Key\_function(void); //按键函数void Display\_function(void); //显示函数void Manage\_function(void); //处理函数#define LED(a) (a?HAL\_GPIO\_WritePin(LED\_GPIO\_Port, LED\_Pin, GPIO\_PIN\_RESET):HAL\_GPIO\_WritePin(LED\_GPIO\_Port, LED\_Pin, GPIO\_PIN\_SET)) #define BEEP(a) (a?HAL\_GPIO\_WritePin(BEEP\_GPIO\_Port, BEEP\_Pin, GPIO\_PIN\_RESET):HAL\_GPIO\_WritePin(BEEP\_GPIO\_Port, BEEP\_Pin, GPIO\_PIN\_SET)) uint8\_t key\_num,flag\_display; //按键与显示变量static int flag=0;uint8\_t flag\_led,flag\_beep; //灯、报警标志位uint16\_t time\_num;static int flag1=0;void SystemClock_Config(void);/****
*******按键设置函数
*****/void Key_function(void){
key\_num = Chiclet\_Keyboard_Scan(); //按键扫描
if(key_num != 0) //有按键按下
{ switch(key_num)
{ case 1:flag=1;break; case 2:flag=3;break; case 3:flag=5;break; case 4:flag=2;flag1++;break; case 5:flag=4;flag1++;break; case 6:flag=6;flag1++;break;
}
}
}/****
*******显示函数
*****/void Display_function(void){ switch(flag)
{ case 1: Oled\_ShowCHinese(0,0,"指纹正确"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请开门"); break; case 2: Oled\_ShowCHinese(0,0,"指纹错误"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请重试"); break; case 3: Oled\_ShowCHinese(0,0,"蓝牙正确"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请开门"); break; case 4: Oled\_ShowCHinese(0,0,"蓝牙错误"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请重试"); break; case 5: Oled\_ShowCHinese(0,0,"刷卡正确"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请开门"); break; case 6: Oled\_ShowCHinese(0,0,"刷卡错误"); Oled\_ShowString(64,0,","); Oled\_ShowCHinese(80,0,"请重试"); break;
}
}/****
*******处理函数
*****/void Manage_function(void){ if(flag1 == 3)
{
flag_beep=1;
flag\_led=1; Oled\_ShowCHinese(0,2,"开门失败"); Oled_ShowCHinese(0,4,"报警");
} else
{
flag_beep=0;
flag_led=0;
} if(flag_beep==1) BEEP(1); else
BEEP(0); if(flag_led==1) LED(1); else
LED(0);
}int main(void){ HAL\_Init(); SystemClock\_Config(); MX\_GPIO\_Init(); OLED_Init(); //OLED初始化
OLED_Clear(); //OLED清屏
while (1)
{ Key_function(); //按键函数
Display_function(); //显示函数
Manage_function(); //处理函数
}
}void SystemClock_Config(void){
RCC\_OscInitTypeDef RCC\_OscInitStruct = {0};
RCC\_ClkInitTypeDef RCC\_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
RCC\_OscInitStruct.OscillatorType = RCC\_OSCILLATORTYPE_HSI;
RCC\_OscInitStruct.HSIState = RCC\_HSI_ON;
RCC\_OscInitStruct.HSICalibrationValue = RCC\_HSICALIBRATION_DEFAULT;
RCC\_OscInitStruct.PLL.PLLState = RCC\_PLL_ON;
RCC\_OscInitStruct.PLL.PLLSource = RCC\_PLLSOURCE\_HSI\_DIV2;
RCC\_OscInitStruct.PLL.PLLMUL = RCC\_PLL\_MUL4; if (HAL\_RCC\_OscConfig(&RCC\_OscInitStruct) != HAL_OK)
{ Error_Handler();
}
RCC\_ClkInitStruct.ClockType = RCC\_CLOCKTYPE\_HCLK|RCC\_CLOCKTYPE_SYSCLK
|RCC\_CLOCKTYPE\_PCLK1|RCC\_CLOCKTYPE\_PCLK2;
RCC\_ClkInitStruct.SYSCLKSource = RCC\_SYSCLKSOURCE_PLLCLK;
RCC\_ClkInitStruct.AHBCLKDivider = RCC\_SYSCLK_DIV1;
RCC\_ClkInitStruct.APB1CLKDivider = RCC\_HCLK_DIV2;
RCC\_ClkInitStruct.APB2CLKDivider = RCC\_HCLK\_DIV1; if (HAL\_RCC\_ClockConfig(&RCC\_ClkInitStruct, FLASH\_LATENCY\_0) != HAL_OK)
{ Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC\_PERIPHCLK\_ADC;
PeriphClkInit.AdcClockSelection = RCC\_ADCPCLK2\_DIV2; if (HAL\_RCCEx\_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{ Error_Handler();
}
}/* USER CODE BEGIN 4 *//* USER CODE END 4 *//**
* @brief This function is executed in case of error occurrence.
* @retval None
*/void Error\_Handler(void){ /* USER CODE BEGIN Error\_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
\_\_disable\_irq(); while (1)
{
} /* USER CODE END Error\_Handler\_Debug */}#ifdef USE\_FULL\_ASSERT/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/void assert\_failed(uint8\_t \*file, uint32_t line){ /\* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\\r\\n", file, line) */
/* USER CODE END 6 */}#endif /* USE\_FULL\_ASSERT */
四、实现现象
具体动态效果看B站演示视频:
基于STM32的智能门禁系统(指纹、蓝牙、刷卡、OLED、电机)
全部资料(源程序、仿真文件、安装包、演示视频):
链接:https://pan.baidu.com/s/1B3YvL6RwC3TdHNIVC0yLSw?pwd=xl3o
提取码:xl3o