#include < at89x51.h>
#define uchar unsigned char
#define uint unsigned int
void rs232_init();
uchar key=0; //接收計(jì)數(shù)器
int i=0;
uchar data table1[15];
sbit K2=P2^1;
sbit K1=P2^0;
sbit led= P3^7;
sbit p1=P0^0;
sbit cumo=P1^4; //觸摸輸入信號(hào)
sbit fmp=P3^2; //蜂鳴器控制腳低電平響
uchar flag=0; //觸摸標(biāo)志 0是有效 1為無(wú)效
uchar zcid=0; //指紋注冊(cè)標(biāo)志1為有效0無(wú)效
void delay1ms(uint x)
{
uchar q=124;
while(x--)
while(q--);
}
void rs232_init()
{
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
TR1=1;
SM0=0;
SM1=1;
REN=1; //先設(shè)定號(hào)工作方式,在打開(kāi)允許接收
EA=1;
ES=1;
TI=0;
RI=0;
for( i=0;i<15;i++) //初始化table1[]數(shù)組
{
table1[i]=0xFF;
}
}
void ser()interrupt 4
{
RI=0;
table1[key++]=SBUF; //存數(shù)據(jù)到接收緩存
}
void GR_Enroll() // 自動(dòng)注冊(cè)模板
{
uchar sendbuffer[]={0xef,0x01,0xff,0xff,0xff,0x0ff,0x01,0x00,0x03,0x33,0x00,0x37};
uchar j=0;
ES=0; //禁止中斷
for(j=0;j<12;j++)
{
SBUF=sendbuffer[j];
while(!TI);
TI=0; }
ES=1; //允許中斷
while(key<13); //等待發(fā)送到0-13即14位數(shù)據(jù)
if(table1[9]==0x00) //判斷命令數(shù)組10位返回0x00即指紋驗(yàn)證通過(guò)返回其它數(shù)就不通過(guò)
{
fmp=0; //蜂鳴器響
delay1ms(200);
fmp=1; //停止蜂鳴器響
delay1ms(500);
}
else
{
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
delay1ms(80);
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
}
for( i=0;i<13;i++) //接收完數(shù)據(jù)把數(shù)據(jù)置為0xFF
{
table1[i]=0xff;
}
key=0; //接收完畢把數(shù)據(jù)個(gè)數(shù)標(biāo)志清零
zcid=0; //清空指紋注冊(cè)標(biāo)志
}
void GR_Identity() // 自動(dòng)驗(yàn)證指紋
{
uchar j=0;
uchar sendbuffer[]={0xef,0x01,0xff,0xff,0xff,0x0ff,0x01,0x00,0x03,0x34,0x00,0x38}; //指紋驗(yàn)證數(shù)組命令
if(cumo==0 && flag==0) //滿足指紋感應(yīng)有手指并且感應(yīng)標(biāo)志為0時(shí)候才執(zhí)行指紋驗(yàn)證程序
{
ES=0; //禁止中斷
for(j=0;j<12;j++) //發(fā)送指紋驗(yàn)證數(shù)組命令
{
SBUF=sendbuffer[j];
while(!TI);
TI=0; }
ES=1; //允許中斷
while(key<15) ; //等待發(fā)送到0-14個(gè)數(shù)據(jù)即15位數(shù)據(jù)接收完畢
if(table1[9]==0x00) //判斷命令數(shù)組10位返回0x00即指紋驗(yàn)證通過(guò)返回其它數(shù)就不通過(guò)
{
if( table1[10]==0x00 && table1[11]==0x00) //如果指紋庫(kù)里0個(gè)指紋那就啟動(dòng)注冊(cè)新指紋函數(shù)
{
zcid=1;
fmp=0; //蜂鳴器響
delay1ms(200);
fmp=1; //停止蜂鳴器響
delay1ms(500);
}
else
{ p1=0; //驗(yàn)證通過(guò)p1指示燈亮 不通過(guò)即滅
fmp=0; //蜂鳴器響
delay1ms(200);
fmp=1; //停止蜂鳴器響
delay1ms(500);
p1=1; //1秒時(shí)間到關(guān)閉
}
}
else
{ p1=1;
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
delay1ms(80);
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
}
for( i=0;i<15;i++) //接收完數(shù)據(jù)把數(shù)據(jù)置為0xFF
{
table1[i]=0xff;
}
flag=1; //標(biāo)志置1待重新拿出手指置0指紋驗(yàn)證才起效
delay1ms(250);//延時(shí)等待
}
key=0; //接收完畢把數(shù)據(jù)個(gè)數(shù)標(biāo)志清零
}
void GR_Empty() // 全部清空指紋庫(kù)函數(shù)
{
uchar sendbuffer[]={0xef,0x01,0xff,0xff,0xff,0x0ff,0x01,0x00,0x03,0x0D,0x00,0x11};
uchar j=0;
ES=0; //禁止中斷
for(j=0;j<12;j++)
{
SBUF=sendbuffer[j];
while(!TI);
TI=0; }
ES=1; //允許中斷
while(key<11); //等待發(fā)送到0-11即12位數(shù)據(jù)
if(table1[9]==0x00) //判斷命令數(shù)組10位返回0x00即指紋驗(yàn)證通過(guò)返回其它數(shù)就不通過(guò)
{
fmp=0; //蜂鳴器響
delay1ms(200);
fmp=1; //停止蜂鳴器響
delay1ms(500);
}
else
{
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
delay1ms(80);
fmp=0; //蜂鳴器響
delay1ms(80);
fmp=1; //停止蜂鳴器響
}
for( i=0;i<11;i++) //接收完數(shù)據(jù)把數(shù)據(jù)置為0xFF
{
table1[i]=0xff;
}
key=0; //接收完畢把數(shù)據(jù)個(gè)數(shù)標(biāo)志清零
}
main()
{
led=0;
P0 = 0xFF;
fmp=1;
zcid=0;
delay1ms(550); //開(kāi)機(jī)等待模塊延時(shí)函數(shù)
rs232_init(); //初始化函數(shù)
delay1ms(1000);//開(kāi)機(jī)等待模塊延時(shí)函數(shù)
while(1)
{
key=0; //接收完畢把數(shù)據(jù)個(gè)數(shù)標(biāo)志清零
if(cumo==1) flag=0; //待放開(kāi)手指后標(biāo)志重新置0
GR_Identity(); // 自動(dòng)驗(yàn)證指紋
if(K1==0 || zcid==1 ) //按下按鍵k1
{
delay1ms(20); //防抖動(dòng)延時(shí)
if(K1==1 ||zcid==1) //松開(kāi)k1按鍵
{
GR_Enroll(); // 自動(dòng)注冊(cè)模板
} }
if(K2==0) //按下按鍵k2
{
delay1ms(20); //防抖動(dòng)延時(shí)
if(K2==1) //松開(kāi)k1按鍵
{
GR_Empty(); // 全部清空指紋庫(kù)函數(shù)
} }
}
}