目录
一、主要功能
二、硬件资源
三、程序编程
四、实现现象
一、主要功能
基于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);elseBEEP(0);if(flag_led==1)LED(1);elseLED(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