文章目录

  • 前言
  • 一、硬件设计
  • 二、软件开发
  • 三、程序设计
  • 成果展示



前言

最近看到HLK出了个人脸识别,准备画个板子做个小项目。


一、硬件设计

这里使用AD进行原理图以及电路的绘制。

java门禁系统 门禁系统程序设计_java门禁系统


java门禁系统 门禁系统程序设计_#define_02


java门禁系统 门禁系统程序设计_单片机_03


使用stm32F103C8T6作为主控,语音选择的是ASRPRO,屏幕选择的是OLED 0.96寸屏幕。

二、软件开发

利用cubemx进行配置,使用freertos操作系统进行开发,配置了三个任务,按键任务,串口任务,显示任务

java门禁系统 门禁系统程序设计_单片机_04


java门禁系统 门禁系统程序设计_#define_05


屏幕使用的是模拟IIC进行控制,人脸识别模块是串口以及语音模块都是使用串口通讯。ASRPRO是使用天问软件开发,比较简单,都不能用开发来形容,只能说使用天问的图形化编程,上手比较快

java门禁系统 门禁系统程序设计_java门禁系统_06

三、程序设计

写了一天的BUG,完成的差不多了。
首先是变量

struct 
{
	u8 key_falg;
	u8 module_result;
	u8 module_command0[20];//识别指令
	u8 module_command1[20];//注册指令
	u8 module_command2[20];//. 删除用户指令
	u8 module_command3[20];//删除全部指令
	u8 module_command4[20];//打开背光控制指令
	u8 module_command5[20];//打开显示控制指令
	u8 module_command6[20];//打开白光灯控制指令
	u8 module_command7[20];//重启指令
	u8 module_command8[20];//读取已注册用户数量指令
	u8 module_command9[20];//关闭背光控制指令
	u8 module_command10[20];//关闭显示控制指令
	u8 module_command11[20];//关闭白光灯控制指令
	u8 asr_command[10];//语音模块控制指令
	u8 key_ud_num;//标记按键上下位置
	u8 key_lr_num;//标记按键左右位置
	u8 function_falg;//功能标记
	u8 lim_key_flag;//按键限时标志位
	u8 module_control_falg[3];//模块控制标志位
	u8 sg90_falg;//舵机转动标志位
	u8 sg90_turn_time;//舵机转动时间
}door_guard_data;
u8 Usart1_Rx[50];
u8 Usart2_Rx[50];

宏定义

#define KEY_OK HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_15)
#define KEY_UP HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)
#define KEY_DOWN HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_8)
#define KEY_LEFT HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)
#define KEY_RIGHT HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
#define KEY_ALL_NUM 5 //按键总数
#define MODULE_FH1 0xEF//人脸识别模块帧头
#define MODULE_FH2 0xAA
#define ASR_USART_FH 0xAA//语音识别通讯帧头
#define ASR_USART_FE 0xEE//语音识别通讯帧尾
#define OLED_UD_NUM 3
#define OLED_RL_NUM 2
#define KEY_DESHAKE_TIME 100//按键消抖时间 ms
#define SG90_TURN_1 500//舵机转动角度控制
#define SG90_TURN_2 1500
#define SG90_TIME 100 //舵机转动时间

自定义函数

u8 KEY_GetStatus(u8 keynum)
{
	u8 temp;
	switch(keynum)
	{
		case 0x01:
			temp = KEY_OK;
			break;
		case 0x02:
			temp = KEY_UP;
			break;
		case 0x03:
			temp = KEY_DOWN;
			break;
		case 0x04:
			temp = KEY_LEFT;
			break;
		case 0x05:
			temp = KEY_RIGHT;
			break;
		default: 
					break;
	}
	return temp;
}
u8 key_scan_t(void)
{
	u8 k_num;
	for(k_num=1;k_num<KEY_ALL_NUM+1;k_num++)
	{
		if(KEY_GetStatus(k_num))
		{
			break;
		}
		
	}
	if(k_num == KEY_ALL_NUM+1)
	{
		k_num = 0;
	}
	return k_num;
}
void lim_key_num()
{
	if(door_guard_data.key_lr_num<= 0)
	{
		door_guard_data.key_lr_num = 0;
	}
	if(door_guard_data.key_ud_num<= 0)
	{
		door_guard_data.key_ud_num = OLED_RL_NUM;
	}
	if(door_guard_data.key_lr_num>= OLED_RL_NUM)
	{
		door_guard_data.key_lr_num = OLED_RL_NUM;
	}
	if(door_guard_data.key_ud_num>= OLED_UD_NUM+1)
	{
		door_guard_data.key_ud_num = 1;
	}
}
void lim_key_num1()
{
	if(door_guard_data.key_ud_num<= 2)
	{
		door_guard_data.key_ud_num = 2;
	}
	if(door_guard_data.key_ud_num>= 3)
	{
		door_guard_data.key_ud_num = 3;
	}
	
}
u8 Negation_Var(u8 variable)
{
	if(variable == 0) variable = 1;
	else  variable = 0;
	return variable;
}
void MY_OLED_Clear()
{
	OLED_ShowString(0,2,(u8*)"                    ",16);
	OLED_ShowString(0,4,(u8*)"                  ",16);
	OLED_ShowString(0,6,(u8*)"                 ",16);
}
void OLED_Detect()
{
	OLED_ShowCHinese(15,2,6);
	OLED_ShowCHinese(30,2,7);
	OLED_ShowCHinese(15,4,2);
	OLED_ShowCHinese(30,4,3);
	OLED_ShowCHinese(15,6,4);
	OLED_ShowCHinese(30,6,5);
}
void OLED_Input()
{
	OLED_ShowCHinese(15,2,0);
	OLED_ShowCHinese(30,2,1);
	OLED_ShowCHinese(15,4,8);
	OLED_ShowCHinese(30,4,9);
	OLED_ShowCHinese(15,6,4);
	OLED_ShowCHinese(30,6,5);
}
void OLED_Delete()
{
	OLED_ShowCHinese(15,2,0);
	OLED_ShowCHinese(30,2,1);
	OLED_ShowCHinese(15,4,2);
	OLED_ShowCHinese(30,4,3);
	OLED_ShowCHinese(15,6,10);
	OLED_ShowCHinese(30,6,11);
}
void OLED_Delete_All()
{
	OLED_ShowCHinese(15,2,14);
	OLED_ShowCHinese(30,2,15);
	OLED_ShowCHinese(15,4,16);
	OLED_ShowCHinese(30,4,17);
}
void OLED_Delete_One()
{
	OLED_ShowCHinese(15,2,18);
	OLED_ShowCHinese(30,2,19);
	OLED_ShowCHinese(15,4,12);
	OLED_ShowCHinese(30,4,13);
}
void OLED_Display()
{
	OLED_ShowCHinese(15,2,26);
	OLED_ShowCHinese(30,2,27);
	OLED_ShowCHinese(45,2,34);
	OLED_ShowCHinese(60,2,35);
	OLED_ShowCHinese(15,4,22);
	OLED_ShowCHinese(30,4,23);
	OLED_ShowCHinese(45,4,32);
	OLED_ShowCHinese(60,4,33);
	OLED_ShowCHinese(15,6,24);
	OLED_ShowCHinese(30,6,25);
	OLED_ShowCHinese(45,6,32);
	OLED_ShowCHinese(60,6,33);
}
void OLED_Backlight()
{
	OLED_ShowCHinese(15,2,20);
	OLED_ShowCHinese(30,2,21);
	OLED_ShowCHinese(45,2,32);
	OLED_ShowCHinese(60,2,33);
	OLED_ShowCHinese(15,4,28);
	OLED_ShowCHinese(30,4,29);
	OLED_ShowCHinese(45,4,34);
	OLED_ShowCHinese(60,4,35);
	OLED_ShowCHinese(15,6,24);
	OLED_ShowCHinese(30,6,25);
	OLED_ShowCHinese(45,6,32);
	OLED_ShowCHinese(60,6,33);
}
void OLED_Whitelight()
{
	OLED_ShowCHinese(15,2,20);
	OLED_ShowCHinese(30,2,21);
	OLED_ShowCHinese(45,2,32);
	OLED_ShowCHinese(60,2,33);
	OLED_ShowCHinese(15,4,22);
	OLED_ShowCHinese(30,4,23);
	OLED_ShowCHinese(45,4,32);
	OLED_ShowCHinese(60,4,33);
	OLED_ShowCHinese(15,6,30);
	OLED_ShowCHinese(30,6,31);
	OLED_ShowCHinese(45,6,34);
	OLED_ShowCHinese(60,6,35);
}

按键任务

door_guard_data.key_falg = key_scan_t();
    osDelay(KEY_DESHAKE_TIME);
		switch(door_guard_data.key_falg)
		{
			case 1://KEY_OK
				door_guard_data.key_falg = 0;
				door_guard_data.lim_key_flag = 0;
				if(door_guard_data.key_lr_num == 1)
				{
					door_guard_data.function_falg = door_guard_data.key_ud_num ;
				}
				if(door_guard_data.key_lr_num == 0)
				{
					door_guard_data.function_falg = door_guard_data.key_ud_num + 3;
				}
			break;
			case 2://KEY_DOWN
				door_guard_data.key_falg = 0;
				door_guard_data.key_ud_num--;
				osDelay(KEY_DESHAKE_TIME);
			break;
			case 3://KEY_UP
				door_guard_data.key_falg = 0;
				door_guard_data.key_ud_num++;
				osDelay(KEY_DESHAKE_TIME);
			break;
			case 4://KEY_LEFT
				door_guard_data.key_falg = 0;
				door_guard_data.key_lr_num--;
				osDelay(KEY_DESHAKE_TIME);
			break;
			case 5://KEY_RIGHT
				door_guard_data.key_falg = 0;
				door_guard_data.key_lr_num++;
				osDelay(KEY_DESHAKE_TIME);
			break;
		}
		if(door_guard_data.function_falg == 3)
		{
			door_guard_data.function_falg = 0;
			door_guard_data.key_lr_num = 0;
		}
		if(door_guard_data.lim_key_flag == 0)
		{
			lim_key_num();
		}
		if(door_guard_data.sg90_falg)
		{
			__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,SG90_TURN_1);
			door_guard_data.sg90_turn_time++;
			if(door_guard_data.sg90_turn_time >= SG90_TIME)
			{
				__HAL_TIM_SET_COMPARE(&htim3,TIM_CHANNEL_4,SG90_TURN_2);
				door_guard_data.sg90_falg = 0;
			}
		}
    osDelay(KEY_DESHAKE_TIME);

串口任务

switch(door_guard_data.function_falg)
		{
			case 1://检测
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command0,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0x12)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0) door_guard_data.sg90_falg = 1;
					door_guard_data.asr_command[1] = door_guard_data.module_result;
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
					osDelay(500);
				}
				break;
			case 2://录入
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command1,10,100);
				osDelay(1500);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0x13)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x02;
					}
					else if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x04;
					}
					else
					{
						door_guard_data.asr_command[1] = door_guard_data.module_result;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 7://删除单个
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				door_guard_data.module_command2[8] = door_guard_data.key_ud_num;
				HAL_UART_Transmit(&huart2,door_guard_data.module_command2,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0x20)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x05;
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x0B;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 5://删除全部
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command3,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0x21)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x05;
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x10;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 8://关闭显示
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command10,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC1)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,2,(u8*)"OFF   ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 9://关闭背光
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command9,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC0)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,4,(u8*)"OFF  ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 10://开启白光
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command6,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC2)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,6,(u8*)"ON ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 11://开启显示
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command5,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC1)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,2,(u8*)"ON ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 12://打开背光
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command4,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC0)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,4,(u8*)"ON ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
			case 13://关闭白光
				door_guard_data.function_falg = 0;
				memset(Usart2_Rx,0,sizeof(Usart2_Rx));
				HAL_UART_Transmit(&huart2,door_guard_data.module_command11,10,100);
				osDelay(1000);
				if(Usart2_Rx[0]==MODULE_FH1&&Usart2_Rx[1] == MODULE_FH2 && Usart2_Rx[7] == 0xC2)
				{
					door_guard_data.module_result = Usart2_Rx[8];
					if(door_guard_data.module_result == 0)
					{
						door_guard_data.asr_command[1] = 0x11;
						OLED_ShowString(90,6,(u8*)"OFF  ",12);
					}
					if(door_guard_data.module_result == 1)
					{
						door_guard_data.asr_command[1] = 0x12;
					}
					HAL_UART_Transmit(&huart1,door_guard_data.asr_command,3,100);
				}
				break;
		}
    osDelay(100);

OLED显示任务

if(door_guard_data.key_lr_num == 1 )//处于第一界面
		{
			oled_flag = 0;
			if(oled_flag1)
			{
				oled_flag1 = 0;
				MY_OLED_Clear();
				OLED_ShowString(30,6,(u8*)"     ",16);
			}
			switch(door_guard_data.key_ud_num)
			{
				case 1:
					OLED_Detect();
					break;
				case 2:
					OLED_Input();
					break;
				case 3:
					OLED_Delete();
					break;
			}
		}
		if(door_guard_data.key_lr_num  == 0)//处于第0界面
		{
			oled_flag1 = 1;
			if(oled_flag == 0)
			{
				oled_flag = 1;
				MY_OLED_Clear();
			}
			if(door_guard_data.lim_key_flag == 0)
			{
				lim_key_num1();
				switch(door_guard_data.key_ud_num)
				{
					case 0x02:
						OLED_Delete_All();
						break;
					case 0x03:
						OLED_Delete_One();
						break;
				}
			}
			if(door_guard_data.function_falg == 5)
			{
				door_guard_data.key_lr_num  = 1;
			}
			if(door_guard_data.function_falg == 6)
			{
				door_guard_data.lim_key_flag = 1;
//				door_guard_data.key_ud_num = 1;
				OLED_ShowString(15,6,(u8*)"ID:",16);
				OLED_ShowNumber(50,6,door_guard_data.key_ud_num,2,16);
				if(door_guard_data.key_falg == 1)
				{
					door_guard_data.function_falg = 7;
					door_guard_data.key_lr_num  = 1;
					door_guard_data.key_falg = 0;
				}
			}
		}
		if(door_guard_data.key_lr_num  == 2)//处于第2界面
		{
			oled_flag1 = 1;
			if(oled_flag == 0)
			{
				OLED_ShowString(90,2,(u8*)"ON",12);
				OLED_ShowString(90,4,(u8*)"ON",12);
				OLED_ShowString(90,6,(u8*)"OFF",12);
				oled_flag = 1;
			}
			switch(door_guard_data.key_ud_num)
			{
				case 1:
					OLED_Display();
					if(door_guard_data.key_falg == 1)
					{
						door_guard_data.key_falg = 0;
						door_guard_data.module_control_falg[0] =	Negation_Var(door_guard_data.module_control_falg[0]);
						if(door_guard_data.module_control_falg[0])
						{
							door_guard_data.function_falg = 8;
						}
						else
						{
							door_guard_data.function_falg = 11;
						}
					}
					break;
				case 2:
					OLED_Backlight();
					if(door_guard_data.key_falg == 1)
					{
						door_guard_data.key_falg = 0;
						door_guard_data.module_control_falg[1] = Negation_Var(door_guard_data.module_control_falg[1]);
						if(door_guard_data.module_control_falg[1])
						{
							door_guard_data.function_falg = 9;
						}
						else
						{
							door_guard_data.function_falg = 12;
						}
					}
					break;
				case 3:
					OLED_Whitelight();
					if(door_guard_data.key_falg == 1)
					{
						door_guard_data.key_falg = 0;
						door_guard_data.module_control_falg[2] = Negation_Var(door_guard_data.module_control_falg[2]);
						if(door_guard_data.module_control_falg[2])
						{
							door_guard_data.function_falg = 10;
						}
						else
						{
							door_guard_data.function_falg = 13;
						}
					}
					break;
			}
		}
    osDelay(10);

成果展示

java门禁系统 门禁系统程序设计_java门禁系统_07


java门禁系统 门禁系统程序设计_stm32_08