本设计开发了一款基于单片机的指纹识别电子密码锁系统。该系统以ATmeg16单片机作为模块核心,通过串口通信控制ZAZ-010指纹模块实现录取指纹并存储指纹数据,并通过HS12864-15C液晶显示比对流程及比对结果,辅以直流继电器与发光二极管模拟开锁的动作。本系统具有体积小、性价比高、传输速度快、适合家庭及单位使用。
程序的巡检过程: 首先对各模块进行初始化,检测在有无按键按下,如果按下判断是那一个按键,并作出判断是否调用相应子程序;当按键1按下后,调用通信模块子程序,录入指纹并将其存入模块缓冲区,同理当按键2按下后,效果相同。
依次当按键3按下时,将指纹模块两缓冲区中的指纹特征文件合成特征模板并存储与指纹模板库中,当4按键按下后搜索指纹并比对.当指纹模块中有指纹存储时直接按4键同样也可进行比对。
程序关键的就是对指纹模块的通信控制, 考虑到处理过程太过冗长,限于篇幅只能将其省略,如需查看,可以看附录1。其它命令发送子函数因大部分的命令大体格式基本相同。
液晶键盘测试程序见附录2:
液晶键盘测试程序结果如图 4-3所示:
- #include<avr/io.h>
- #include<util/delay.h>
- //#include<avr/pgmspace.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define key1 0x01
- #define key2 0x02
- #define key3 0x04
- #define key4 0x08
- static uchar k=0;
- uchar name00[]={" "};
- uchar name01[]={" 欢迎使用 "};
- uchar name02[]={"单片机指纹密码锁"};
- uchar name03[]={" "};
- uchar name04[]={" 指纹采集开始 "};
- uchar name05[]={" 指纹对比开始 "};
- uchar name06[]={" 指纹存储开始 "};
- uchar name07[]={" 一次采集 "};
- uchar name08[]={" 二次采集 "};
- uchar name09[]={" 录入成功 "};
- uchar name10[]={" 录入失败 "};
- uchar name11[]={" 收包有错 "};
- uchar name12[]={"传感器上无手指 "};
- uchar name13[]={" 生成特征成功 "};
- uchar name14[]={"图想乱生不成特征"};
- uchar name15[]={"图象正常特征点少"};
- uchar name16[]={"指纹模板合并成功"};
- uchar name17[]={"指纹模板合并失败"};
- uchar name18[]={" 非同一手指 "};
- uchar name19[]={" 存储成功 "};
- uchar name20[]={" 写指纹库出错 "};
- uchar name21[]={" 指纹匹配 "};
- uchar name22[]={" 指纹不匹配 "};
- uchar name23[]={" 密码正确 "};
- uchar name24[]={" 密码不正确 "};
- uchar FP_1[6]={};
- uchar FP_2[6]={};
- uchar FP_Pack_Head[6]={0xEF,0x01,0xFF,0xFF,0xFF,0xFF}; //协议包头
- uchar FP_Get_Img[6]={0x01,0x00,0x03,0x01,0x0,0x05}; //获得指纹图像
- uchar FP_Search_0_9[11]={0x01,0x0,0x08,0x04,0x01,0x0,0x0,0x0,0x13,0x0,0x21}; //搜索0-9号指纹
- uchar FP_Img_To_Buffer1[7]={0x01,0x0,0x04,0x02,0x01,0x0,0x08}; //将图像放入到BUFFER1
- uchar FP_Img_To_Buffer2[7]={0x01,0x0,0x04,0x02,0x02,0x0,0x09}; //将图像放入到BUFFER2
- uchar FP_Reg_Model[6]={0x01,0x0,0x03,0x05,0x0,0x09}; //将BUFFER1跟BUFFER2合成特征模版
- volatile uchar FP_Save_Finger1[9]={0x01,0x00,0x06,0x06,0x01,0x00,0x0B,0x00,0x19};//将BUFFER1中的特征码存放到指定的位置
- volatile uchar FP_Save_Finger2[9]={0x01,0x00,0x06,0x06,0x01,0x00,0x0C,0x00,0x1A};//将BUFFER1中的特征码存放到指定的位置
- volatile uchar FP_Save_Finger3[9]={0x01,0x00,0x06,0x06,0x01,0x00,0x0D,0x00,0x1B};//将BUFFER1中的特征码存放到指定的位置
- uchar yb=0x80;
- //初始化 UART 子程序
- void UART_Init(void)
- {
- UCSRB = 0x00; //disable while setting baud rate
- UCSRA = 0x00; //Bit1为1则倍速发送
- UCSRC = 0x06; //传送一桢数据位为8位
- UBRRL = 0x07; //波特率:57600 Bps
- UBRRH = 0x00; //误差率:0.000%
- UCSRB = 0x18;
- }
- //发送八位数据
- void UART_Send_Byte( uchar ucData)
- {
- while(!(UCSRA&(1<<UDRE))); //等待缓冲区为空
- UDR = ucData;
- }
- //接收八位数据
- uchar UART_Receive_Byte(void)
- {
- while(!(UCSRA&(1<<RXC)));//等待缓冲区为空
-
- return UDR;
- }
- //_获得指纹图像命令
- void Cmd_Get_Img(void)
- {
- uchar i,j;
- for(i=0;i<6;i++) //发送包头与模块地址
- UART_Send_Byte(FP_Pack_Head[i]);
- for(i=0;i<6;i++) //发送命令 0x1d
- UART_Send_Byte(FP_Get_Img[i]);
- for(i=0;i<6;i++)
- FP_1[i]=UART_Receive_Byte();
- for(i=0;i<6;i++)
- FP_2[i]=UART_Receive_Byte();
- j=FP_2[3];
- if (j==0x00)
- {_delay_ms(100000);
- reset ();
- display6();}
- if (j==0x01)
- {reset();
- wr_com(yb);
- outChinese(0x90,8,name11);}
- if (j==0x02)
- {_delay_ms(100000);
- reset();
- display7();}
- if (j==0x03)
- { reset();
- wr_com(yb);
- outChinese(0x90,8,name10);}
- }
- //讲图像转换成特征码存放在Buffer1中
- void Cmd_Img_To_Buffer1(void)
- {
- uchar i,j;
- for(i=0;i<6;i++) //发送包头与模块地址
- {
- UART_Send_Byte(FP_Pack_Head[i]);
- }
-
- for(i=0;i<7;i++) //发送命令 将图像转换成 特征码 存放在 CHAR_buffer1
- {
- UART_Send_Byte(FP_Img_To_Buffer1[i]);
- }
- for(i=0;i<6;i++)
- {
- FP_1[i]=UART_Receive_Byte();}
- for(i=0;i<6;i++)
- {
- FP_2[i]=UART_Receive_Byte();}
- j=FP_2[3];
- if (j==0x00)
- {_delay_ms(100000);
- reset ();
- display8();}
- if (j==0x06)
- {_delay_ms(100000);
- reset ();
- display9();}
- if (j==0x07)
- {_delay_ms(100000);
- reset ();
- display10();}
- }
- //将图像转换成特征码存放在Buffer2中
- void Cmd_Img_To_Buffer2(void)
- {
- uchar i,j;
- for(i=0;i<6;i++) //发送包头
- {
- UART_Send_Byte(FP_Pack_Head[i]);
- }
-
- for(i=0;i<7;i++) //发送命令 将图像转换成 特征码 存放在 CHAR_buffer2
- {
- UART_Send_Byte(FP_Img_To_Buffer2[i]);
- }
- for(i=0;i<6;i++)
- {
- FP_1[i]=UART_Receive_Byte();}
- for(i=0;i<6;i++)
- {
- FP_2[i]=UART_Receive_Byte();}
- j=FP_2[3];
- if (j==0x00)
- {_delay_ms(100000);
- reset ();
- display8();}
- if (j==0x06)
- {_delay_ms(100000);
- reset ();
- display9();}
- if (j==0x07)
- {_delay_ms(100000);
- reset ();
- display10();}
- }
- //将BUFFER1 跟 BUFFER2 中的特征码合并成指纹模版
- void Cmd_Reg_Model(void)
- {
- uchar i,j;
- for(i=0;i<6;i++) //发送包头与模块地址
- {
- UART_Send_Byte(FP_Pack_Head[i]);
- }
- for(i=0;i<6;i++) //命令合并指纹模版
- {
- UART_Send_Byte(FP_Reg_Model[i]);
- }
- for(i=0;i<6;i++)
- {
- FP_1[i]=UART_Receive_Byte();}
- for(i=0;i<6;i++)
- {
- FP_2[i]=UART_Receive_Byte(); }
- j=FP_2[3];
- if (j==0x00)
- {_delay_ms(400000);
- reset ();
- display11();}
- if (j==0x0a)
- {_delay_ms(400000);
- reset ();
- display12();
- while(1);}
- }
- //将并成后的指纹模版存储到指纹模块flash模板库中
- void Store_Char_Model(void)
- { uchar i,j;
- for(i=0;i<6;i++) //发送包头与模块地址
- {
- UART_Send_Byte(FP_Pack_Head[i]);
- }
- if(k==0)
- { for(i=0;i<9;i++) //命令存储指纹模版
- {
- UART_Send_Byte( FP_Save_Finger1[i] );
- }
- }
- if(k==1)
- { for(i=0;i<9;i++) //命令存储指纹模版
- {
- UART_Send_Byte( FP_Save_Finger2[i] );
- }
- }
- if(k==2)
- { for(i=0;i<9;i++) //命令存储指纹模版
- {
- UART_Send_Byte( FP_Save_Finger3[i] );
-
- }
- }
- for(i=0;i<6;i++)
- {
- FP_1[i]=UART_Receive_Byte();}
- for(i=0;i<6;i++)
- {
- FP_2[i]=UART_Receive_Byte(); }
- j=FP_2[3];
- if(j==0x00)
- {_delay_ms(400000);
- reset ();
- display13();}
- if(j==0x18)
- {_delay_ms(300000);
- reset ();
- display14();}
- }
- //搜索与BUFFER1中特征码相符合的指纹模版
- void search_Char_Mode(void)
- { uchar i,j;
- for(i=0;i<6;i++) //发送包头与模块地址
- {
- UART_Send_Byte(FP_Pack_Head[i]);
- }
- for(i=0;i<11;i++) //命令搜索指纹模版
- {
- UART_Send_Byte(FP_Search_0_9[i]);
-
- }
- for(i=0;i<6;i++)
- {
- FP_1[i]=UART_Receive_Byte();}
- for(i=0;i<10;i++)
- {
- FP_2[i]=UART_Receive_Byte(); }
- j=FP_2[3];
- if(j==0x00)
- {_delay_ms(100000);
- reset ();
- display15();}
- if(j==0x09)
- {_delay_ms(100000);
- reset ();
- display16();}
- void keyboard( void)
- { unsigned char Keyvalue;//键盘值
- //端口初始化
- DDRC=~_BV(PC0)& ~_BV(PC1)& ~_BV(PC2)& ~_BV(PC3);
- while(1)
- { Keyvalue=(PINC&0x0F);
- if (Keyvalue!=0x0f)
- {//有键按下
- _delay_ms(20);
- Keyvalue=(PINC&0x0F);
- if (Keyvalue==0X0F)
- break;//如果延时去抖后,没有检测到键按下,退出本次循环
- //如果有按键
- if(! (Keyvalue& key1))
- {reset ();
- display2();
- UART_Init();
- Cmd_Get_Img();
- Cmd_Img_To_Buffer1();
- }//按键1的处理部分
- if(! (Keyvalue& key2))
- { reset ();
- display3();
- UART_Init();
- Cmd_Get_Img();
- Cmd_Img_To_Buffer2();
- }//按键2的处理部分
- if(! (Keyvalue& key3))
- { reset ();
- display4();
- UART_Init();
- Cmd_Reg_Model();
- Store_Char_Model();
- k++; }//按键3的处理部分
- if(! (Keyvalue& key4))
- { reset ();
- display5();
- UART_Init();
- Cmd_Get_Img();
- Cmd_Img_To_Buffer1();
- search_Char_Mode();
- }//按键4的处理部分
- }
- _delay_ms(20);
- }
- }
- void check_busy (void) //读取忙碌状态
- {
- DDRA=0;
- PORTA=0xff;
- PORTB&=0xfb; //RS=0
- PORTB|=0x02; //RW=1;
- PORTB|=0x01;PORTB|=0x01; //E=1;
- while((PINA&0x80)==0x80);
- PORTB&=0xfe;
- //E =0;
- }
- void wr_com(unsigned char value) //写指令,写指令时必须为RS=0;RW=0;
- {check_busy();
- PORTB&=0xfe; //E=0;
- PORTB&=0xfb; //RS=0
- PORTB&=0xfd; //RW=0;
- DDRA=0xff;
- PORTA=value;
- PORTB|=0x01; //E=1;
- _delay_us(20); //如果没有延时就必须要加查忙指令
- PORTB&=0xfe;
- DDRA=0x00; //E=0;
- }
- void wr_data(unsigned char sj) //写数据,写数据时必须为 RS=1;RW=0;
- {check_busy();
- PORTB&=0xfe; //E=0;
- PORTB|=0x04; //RS=1;
- PORTB&=0xfd; //RW=0;
- DDRA=0xff;
- PORTA=sj;
- PORTB|=0x01; //E=1;
- _delay_us(20); //如果没有延时就必须要加查忙指令
- PORTB&=0xfe;
- DDRA=0x00; ////E=0;
- }
- void reset (void)
- {
- wr_com(0x01);//清屏
- wr_com(0x08);//关显示
- wr_com(0x03);//归位
- wr_com(0x30);//功能设置
- wr_com(0x0f);//开显示
- wr_com(0x01);//清屏
- }
- //******************************************************************/
- //outChinese 为函数名
- //place 为显示地址的首地址
- //unit 字符长度
- //charcode[] 要显示数据的内容
- void outChinese(unsigned char place,unsigned char unit,unsigned char charcode[] )
- {
- unsigned char i;
- wr_com(place);
- for(i=0;i<unit*2;i++)//一个汉字为两个字符
- wr_data(charcode[i]);
- }
- void display1(void)
- {wr_com(yb);
- //ydgb();
- outChinese(0x80,8,name00);//第一行:80-87H
- outChinese(0x90,8,name01);//第二行:90-97H
- outChinese(0x88,8,name02);//第三行:88-8FH
- outChinese(0x98,8,name03);//第四行:98-9FH
-
- }
复制代码 以上的Word格式文档51黑下载地址:
设计文档.doc
(1.27 MB, 下载次数: 14)
|