程序为早年所写,逻辑不是很好,勿喷
STM32单片机源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "oled.h"
- #include "usart2.h"
- #include "gpio.h"
- #include "timer.h"
- #include "as608.h"
- #include "24cxx.h"
- #include "myiic.h"
- #define moto_down 1400
- #define moto_up 2100 //最大2500
- #define time 1000 //ms
- void ifmima(void); //密码判断函数
- void recivemima(void); //串口接收密码函数
- void turncode(void); //串口密码更改函数
- void press_FR(void); //刷指纹
- void Add_FR(void); //验证指纹
- void gui(u8 i); //界面函数
- void gpioinit(void); //gpio初始化函数
- void Del_FR(void); //指纹删除函数
- void incode(void); //按键输入函数
- void tcode(void); //按键更改密码函数
- u8 key(u8 mode); //按键扫描函数
- u8 turnstr(u8 d); //字符转换还函数
- u16 ValidN; //模块内有效指纹个数
- u8 mima[6]= {'1','1','1','1','1','1'}; //默认密码
- u16 shurumima[6]= {'0','0','0','0','0','0'}; //输入密码
- u16 turnmima[7]= {'0','0','0','0','0','0','0'}; //更改密码
- u16 i,j;
- u8 len; //接收数据的长度;
- u8 change=0; //密码更改标志位 更改1,不更改为0
- u8 over=0; //输入密码结束标志位 完成1
- u8 ture=2; //密码判断标志位 正确1,错误0,等待2
- u8 t=0;
- u8 reok=0; //回到主界面标志位 为一回到主界面
- int main(void)
- {
- PS_StaGPIO_Init();
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //设置系统中断优先级分组2
- delay_init(168); //初始化延时函数
- pwminit1(84-1,20000-1);
- usart2_init(9600); //初始化串口2,用于与指纹模块通讯
- uart_init(9600); //初始化串口波特率为115200
- OLED_Init(); //初始化OLED
- gpioinit(); //gpio初始化(按键)
- AT24CXX_Init(); //24c02初始化
- while(AT24CXX_Check()) //等待24c02初始化完毕
- OLED_Clear();
- OLED_ShowCH(0,16,"准备中");
- OLED_Refresh_Gram();
- // AT24CXX_Write(0,(u8*)mima,6); // 此条用于初始密码 如果密码忘记恢复此条密码初始为111111
- AT24CXX_Read(0,mima,6);
- delay_ms(2000);
- OLED_Clear(); //清屏
- gui(0); //主界面
- OLED_Refresh_Gram(); //更新显示到OLED
- TIM_SetCompare1(TIM3,moto_down); //舵机1500-2400 中值1850
- while(1)
- {
- recivemima(); //调用串口密码接收
- if(reok==1) {
- reok=0; //如果标志位置一回到主屏
- goto OVER;
- }
- switch(key(0)) //选择哪个按键被按下
- {
- case 0: //按键0被按下
- gui(1); //进入界面1指纹解锁
- while(1) //进入循环等待(等待操作结束)
- {
- delay_ms(100);
- t++; //延时100ms t加一(5秒无操作退出)
- switch(key(0)) //选择次界面 指纹功能选择
- {
- case 0: {
- Add_FR(); //指纹添加
- goto OVER;
- }
- case 10: {
- Del_FR(); //指纹删除
- goto OVER;
- }
- case 11: {
- press_FR(); //指纹验证
- goto OVER;
- }
- }
- if(t==50) {
- t=0; //5秒无操作退出
- goto OVER;
- }
- }
- case 10: {
- gui(2);
- while(1)
- {
- delay_ms(100);
- t++;
- switch (key(0))
- {
- case 0:
- while(1) {
- incode();
- goto OVER;
- }
- case 10:
- while(1) {
- tcode();
- goto OVER;
- }
- case 11:
- goto OVER;
- }
- if(t==50) {
- t=0;
- goto OVER;
- }
- }
- /* case 11:
- {
- gui(4);
- while(1)
- {
- if(key(0)==11)goto OVER;
- if(reok==1)
- {goto OVER;}
- }
- } */
- }
- OVER: {
- OLED_Clear();
- gui(0);
- break;
- }
- }
- }
- }
- void recivemima(void) //串口接收密码函数
- {
- reok=0; //标志位清零
- if(USART_RX_STA&0x8000) //如果串口有数据发来
- {
- len=USART_RX_STA&0x3fff; //得到此次接收到的数据长度
- for(i=0; i<len; i++) //数据转存
- {
- if(USART_RX_BUF[0]!='t') {
- shurumima[i]=USART_RX_BUF[i]; //如果没有接收到密码更改标志位数据存储在shurumima中
- change=0;
- }
- if(USART_RX_BUF[0]=='t') {
- turnmima[i]=USART_RX_BUF[i]; //如果接收到密码更改标志位数据存储在turnmima中 密码更改标志位置一
- change=1;
- }
- }
- if(change==0) //密码更改
- {
- ifmima(); //调用判断函数
- if(ture==1) {
- OLED_Clear();
- OLED_ShowCH(0,16,"密码正确");
- OLED_Refresh_Gram();
- reok=1;
- TIM_SetCompare1(TIM3,moto_up);
- delay_ms(time);
- TIM_SetCompare1(TIM3,moto_down);
- }
- else {
- OLED_Clear();
- OLED_ShowCH(0,16,"密码错误");
- OLED_Refresh_Gram();
- reok=1;
- delay_ms(800);
- }
- }
- if(change==1) {
- turncode();
- }
- USART_RX_STA=0; //清除中断 标志位
- }
- }
- void tcode(void)//按键更改密码函数
- {
- u8 key1,count=0;
- ture=2;
- incode(); //密码输入函数调用
- if(ture==1) //输入原密码正确
- {
- OLED_Clear();
- OLED_ShowCH(0,0,"请输新密码");
- OLED_Refresh_Gram();
- key1=key(0); //获取当前按键值(无按键按下是为15)
- while(1)
- {
- key1=key(0);
- while(key1==15) //等待按键按下
- {
- key1=key(0);
- }
- count++; //计算密码个数
- OLED_ShowNum(12,23,count,2,12);
- OLED_Refresh_Gram();
- switch(count) //密码转存
- {
- case 1:
- mima[0]=turnstr(key1);
- break;
- case 2:
- mima[1]=turnstr(key1);
- break;
- case 3:
- mima[2]=turnstr(key1);
- break;
- case 4:
- mima[3]=turnstr(key1);
- break;
- case 5:
- mima[4]=turnstr(key1);
- break;
- case 6:
- mima[5]=turnstr(key1);
- break;
- }
- if(count==6) //密码个数为6位
- {
- count=0;
- delay_ms(100);
- break;
- }
- delay_ms(200);
- }
- AT24CXX_Write(0,(u8*)mima,6);
- OLED_Clear();
- OLED_ShowCH(0,16,"密码修改成功");
- OLED_Refresh_Gram();
- delay_ms(800);
- }
- else
- {
- OLED_Clear();
- OLED_ShowCH(0,16,"密码修改错误");
- OLED_Refresh_Gram();
- delay_ms(800);
- reok=1;
- }
- }
- void incode(void) //按键输入函数
- {
- u8 key1,count=0;
- OLED_Clear();
- OLED_ShowCH(0,0,"请输入密码");
- OLED_Refresh_Gram();
- key1=key(0); //返回15
- while(1)
- {
- key1=key(0);
- while(key1==15)
- {
- key1=key(0);
- }
- count++;
- switch(count)
- {
- case 1:
- shurumima[0]=turnstr(key1);
- break;
- case 2:
- shurumima[1]=turnstr(key1);
- break;
- case 3:
- shurumima[2]=turnstr(key1);
- break;
- case 4:
- shurumima[3]=turnstr(key1);
- break;
- case 5:
- shurumima[4]=turnstr(key1);
- break;
- case 6:
- shurumima[5]=turnstr(key1);
- break;
- }
- if(count==6)
- {
- count=0;
- ifmima();
- delay_ms(100);
- break;
- }
- delay_ms(200);
- }
- if(ture==1) {
- OLED_Clear();
- OLED_ShowCH(0,16,"密码正确");
- OLED_Refresh_Gram();
- TIM_SetCompare1(TIM3,moto_up);
- delay_ms(time);
- TIM_SetCompare1(TIM3,moto_down);
- } //舵机开锁等待1秒后关闭
- if(ture==0) {
- OLED_Clear();
- OLED_ShowCH(0,16,"密码错误");
- OLED_Refresh_Gram();
- delay_ms(800);
- }
- }
- void turncode(void)//串口更改密码函数
- {
- for(i=0; i<6; i++)
- {
- mima[i]=turnmima[i+1]; //数据转存(第一位为标志位省去)
- }
- AT24CXX_Write(0,(u8*)mima,6);
- OLED_Clear();
- OLED_ShowCH(0,16,"密码修改成功");
- OLED_Refresh_Gram();
- reok=1; //标志位置一 返回主界面
- delay_ms(800);
- }
- void ifmima(void)//密码判断函数
- {
- for(i=0; i<6; i++)
- {
- if(shurumima[i]==mima[i])ture=1; //数组比较正确标志位置一
- else {
- ture=0; //不正确标志位置0
- break;
- }
- }
- for(j=0; j<6; j++)
- {
- shurumima[j]='0'; //清空输入密码的数组
- }
- }
- //刷指纹
- void press_FR(void)
- {
- SearchResult seach;
- u8 ensure;
- u8 t=0;
- while(1)
- {
- t++;
- ensure = PS_GetImage();
- OLED_Clear();
- OLED_ShowCH(0,0,"请按手指");
- OLED_Refresh_Gram();
- if(ensure == 0x00) //获取图像成功
- {
- ensure = PS_GenChar(CharBuffer1);
- if(ensure == 0x00) //生成特征成功
- {
- ensure = PS_HighSpeedSearch(CharBuffer1, 0, 99, &seach);
- if(ensure == 0x00) //搜索成功
- {
- OLED_Clear();
- OLED_ShowCH(0, 16, "指纹验证成功");
- OLED_Refresh_Gram();
- TIM_SetCompare1(TIM3,moto_up);
- delay_ms(1000);
- TIM_SetCompare1(TIM3,moto_down);
- // delay_ms(1500);
- delay_ms(1500);
- break;
- }
- else
- { OLED_Clear();
- OLED_ShowCH(0, 16, "验证失败");
- OLED_Refresh_Gram();
- delay_ms(1500);
- break;
- }
- }
- else
- {};
- }
- if(t==10)
- {
- t=0;
- break;
- }
- delay_ms(500);
- }
- }
- //添加指纹
- void Add_FR(void)
- {
- u8 i, ensure, processnum = 0;
- u8 ID_NUM = 0;
- while(1)
- {
- switch (processnum)
- {
- case 0:
- i++;
- OLED_Clear();
- OLED_ShowCH(0, 16, "请按手指");
- OLED_Refresh_Gram();
- ensure = PS_GetImage();
- if(ensure == 0x00)
- {
- ensure = PS_GenChar(CharBuffer1); //生成特征
- if(ensure == 0x00)
- {
- OLED_Clear();
- OLED_ShowCH(0, 16, "指纹正常");
- OLED_Refresh_Gram();
- i = 0;
- processnum = 1; //跳到第二步
- }
- }
- break;
- case 1:
- i++;
- OLED_Clear();
- ensure = PS_GetImage();
- if(ensure == 0x00)
- {
- ensure = PS_GenChar(CharBuffer2); //生成特征
- if(ensure == 0x00)
- {
- OLED_Clear();
- OLED_ShowCH(0, 16, "指纹正常");
- OLED_Refresh_Gram();
- i = 0;
- processnum = 2; //跳到第三步
- }
- else {
- OLED_ShowCH(0, 16,"失败");
- OLED_Refresh_Gram();
- }
- }
- else {
- OLED_ShowCH(0, 16, "失败");
- OLED_Refresh_Gram();
- }
- break;
- case 2:
- OLED_Clear();
- OLED_ShowCH(0, 16, "对比两次指纹");
- OLED_Refresh_Gram();
- ensure = PS_Match();
- if(ensure == 0x00)
- { OLED_Clear();
- OLED_ShowCH(0, 16, "对比成功");
- OLED_Refresh_Gram();
- processnum = 3; //跳到第四步
- }
- else
- {
- OLED_ShowCH(0, 16, "对比失败");
- OLED_Refresh_Gram();
- i = 0;
- processnum = 0; //跳回第一步
- }
- delay_ms(500);
- break;
- case 3:
- OLED_Clear();
- OLED_ShowCH(0, 16, "可松开手指");
- OLED_Refresh_Gram();
- delay_ms(800);
- OLED_Clear();
- OLED_ShowCH(0, 16, "生成指纹模板");
- OLED_Refresh_Gram();
- delay_ms(500);
- ensure = PS_RegModel();
- if(ensure == 0x00)
- { OLED_Clear();
- OLED_ShowCH(0, 16, "生成指纹模板成功");
- OLED_Refresh_Gram();
- processnum = 4; //跳到第五步
- }
- else
- {
- processnum = 0;
- OLED_ShowCH(0, 16, "失败");
- OLED_Refresh_Gram();
- }
- delay_ms(1000);
- break;
- case 4:
- OLED_Clear();
- OLED_ShowCH(0, 16, "输入用户编号");
- OLED_Refresh_Gram();
- while(ID_NUM==0|ID_NUM==15)
- {
- ID_NUM=key(0);
- }
- OLED_Clear();
- OLED_ShowCH(0, 0, "用户编号为");
- OLED_ShowNum(0, 32, ID_NUM, 2, 24);
- OLED_Refresh_Gram();
- ensure = PS_StoreChar(CharBuffer2, ID_NUM); //储存模板
- delay_ms(800);
- if(ensure == 0x00)
- {
- OLED_Clear();
- OLED_ShowCH(0, 16, "录入指纹成功");
- OLED_Refresh_Gram();
- delay_ms(1500);
- OLED_Clear();
- return ;
- }
- else
- {
- OLED_Clear();
- processnum = 0;
- OLED_ShowCH(0, 16, "失败");
- OLED_Refresh_Gram();
- }
- break;
- }
- delay_ms(400);
- if(i == 10) //超过5次没有按手指则退出
- {
- break;
- }
- }
- }
- //删除指纹
- void Del_FR(void)
- {
- u8 ensure;
- u16 ID_NUM = 0;
- OLED_Clear();
- OLED_ShowCH(0, 2, "输入清除用户编号");
- OLED_Refresh_Gram();
- while(ID_NUM==0|ID_NUM==15)
- {
- ID_NUM=key(0);
- }
- ensure = PS_DeletChar(ID_NUM, 1); //删除单个指纹
- if(ensure == 0)
- {
- OLED_Clear();
- OLED_ShowNum(0, 12, ID_NUM, 2, 12);
- OLED_ShowCH(12, 12, "号删除指纹成功");
- OLED_Refresh_Gram();
- }
- else
- OLED_ShowCH(0, 16, "删除失败");
- delay_ms(1500);
- }
- void gui(u8 i)
- {
- switch(i)
- {
- /*******************************************/
- /*
- 主界面
- */
- /*******************************************/
- case 0:
- OLED_Clear();
- OLED_ShowCH(8, 0, "控智控能控锁控");
- OLED_ShowCH(32, 16, "指纹解锁");
- OLED_ShowCH(32, 32, "密码解锁");
- // OLED_ShowCH(32, 48, "软件解锁");
- OLED_Refresh_Gram();
- break;
- /*******************************************/
- /*
- 指纹解锁界面
- */
- /*******************************************/
- case 1:
- OLED_Clear();
- OLED_ShowCH(8, 0, "指控纹控解控锁");
- OLED_ShowCH(32, 16, "添加指纹");
- OLED_ShowCH(32, 32, "删除指纹");
- OLED_ShowCH(32, 48, "验证指纹");
- OLED_Refresh_Gram();
- break;
- /*******************************************/
- /*******************************************/
- /*
- 按键解锁主界面
- */
- /*******************************************/
- case 2:
- OLED_Clear();
- OLED_ShowCH(8, 0, "密控码控解控锁");
- OLED_ShowCH(32, 16, "输入密码");
- OLED_ShowCH(32, 32, "修改密码");
- OLED_ShowCH(40, 48, "返控回");
- OLED_Refresh_Gram();
- break;
- /*******************************************/
- /*
- 按键解锁次界面
- */
- /*******************************************/
- case 3:
- OLED_Clear();
- OLED_ShowCH(8, 0, "密控码控解控锁");
- OLED_ShowCH(0, 16, "请输入密码");
- //OLED_ShowCH(32, 32, "修改密码");
- OLED_ShowCH(88, 48, "返回");
- OLED_Refresh_Gram();
- break;
- /*******************************************/
- /*
- 软件解锁次界面
- */
- /*******************************************/
- case 4:
- OLED_Clear();
- OLED_ShowCH(8, 0, "软控件控解控锁");
- OLED_ShowCH(0, 16, "请连接软件");
- OLED_ShowCH(88, 48, "返回");
- OLED_Refresh_Gram();
- break;
- }
- }
- u8 turnstr(u8 d)
- {
- switch(d)
- {
- case 0: {
- return '0';
- break;
- }
- case 1: {
- return '1';
- break;
- }
- case 2: {
- return '2';
- break;
- }
- case 3: {
- return '3';
- break;
- }
- case 4: {
- return '4';
- break;
- }
- case 5: {
- return '5';
- break;
- }
- case 6: {
- return '6';
- break;
- }
- case 7: {
- return '7';
- break;
- }
- case 8: {
- return '8';
- break;
- }
- case 9: {
- return '9';
- break;
- }
- }
- }
复制代码
|