門禁系統(tǒng)項(xiàng)目驅(qū)動(dòng)程序
/******************************按鍵控制程序*********************************/
void KeyScan(){//按鍵掃描
if(!KEY1){
DelCount = 0;
while(DelCount < 2);
if(!KEY1){
while(!KEY1);
Function = 1;
}
}
if(!KEY2){
DelCount = 0;
while(DelCount < 2);
if(!KEY2){
while(!KEY2);
Function = 2;
}
}
if(!KEY3){
DelCount = 0;
while(DelCount < 2);
if(!KEY3){
while(!KEY3);
Function = 3;
}
}
if(!KEY4){
DelCount = 0;
while(DelCount < 2);
if(!KEY4){
while(!KEY4);
Function = 4;
}
}
}
void KeyFunc(){//按鍵功能分配
uchar Cnt = 0;
bit CFlag = 0;//待存入的卡與EEPROM中的卡號(hào)比較標(biāo)志位
switch(Function){//具體按鍵功能分配程序
case 1:if(!SwitchFlag){//切換標(biāo)志位,用于存卡及瀏覽卡號(hào)時(shí)上翻
TimeDisFlag = 1;//存儲(chǔ)卡號(hào)期間主函數(shù)運(yùn)行時(shí)不再顯示時(shí)間
KEndFlag = 0;//確認(rèn)在進(jìn)入存卡系統(tǒng)后刷卡有效,否則無效
WXFlag = 1;//按下KEY1后,接著按key2無效
StoreFlag = 1;//存儲(chǔ)標(biāo)志位,0為無效,1為存儲(chǔ)
LCD_All_Clear();//清全屏
LCD_Display5();//顯示存卡界面
while(!KEndFlag);//等待刷卡后讀取卡信息結(jié)束
KEndFlag = 0;//確認(rèn)在進(jìn)入存卡系統(tǒng)后刷卡有效,否則無效
for(Cnt = 1;Cnt < 11;Cnt ++){//顯示讀取的卡號(hào)
if(CardNo[Cnt] >= 0x40)
LCD_Data_DisNum(32 + Cnt * 6,2,num[CardNo[Cnt]-0x40+10]);
elseLCD_Data_DisNum(32 + Cnt * 6,2,num[CardNo[Cnt]-0x30]);
}
LCD_Data_DisWord(35,4,cun);//顯示漢字“存入”“取消”
LCD_Data_DisWord(47,4,ru);
LCD_Data_DisWord(75,4,qu);
LCD_Data_DisWord(87,4,quxiao);
}
if(SwitchFlag){//瀏覽卡號(hào)時(shí)上翻功能
if(!NSK12Flag){
while(1){
if(EEAddr == 0x0000)
EEAddr = 0x0000 + (CardMax - 1) * 0x20;
elseEEAddr -= 0x20;//24C64存儲(chǔ)時(shí)一頁存儲(chǔ)一張卡號(hào)
I2C_Read_AT24C64(EAddrH,EAddrL,12);//讀取24C64中存儲(chǔ)的卡號(hào)
if(RD24C64[0] == 1){//從24C64EEAddr地址中讀取的卡號(hào)有效
for(Cnt = 2;Cnt < 12;Cnt ++){//顯示讀取的卡號(hào)
if(RD24C64[Cnt] >= 0x40)
LCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x40+10]);
elseLCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x30]);
}
break;//顯示一個(gè)有效的卡號(hào)后退出
}
}
}
}
break;
case 2:if(!WXFlag){
if(SwitchFlag){//瀏覽卡號(hào)時(shí)下翻功能
if(!NSK12Flag){
while(1){
EEAddr += 0x20;//往下翻頁
if(EEAddr == 0x0000 + CardMax * 0x20)
EEAddr = 0x0000;
I2C_Read_AT24C64(EAddrH,EAddrL,12);
if(RD24C64[0] == 1){//讀取的卡號(hào)有效
for(Cnt = 2;Cnt < 12;Cnt ++){//顯示讀取的卡號(hào)
if(RD24C64[Cnt] >= 0x40)
LCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x40+10]);
elseLCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x30]);
}
break;
}
}
}
}
if(!SwitchFlag){//刪除卡號(hào)
TimeDisFlag = 1;//刪除卡號(hào)期間主函數(shù)運(yùn)行時(shí)不再顯示時(shí)間
DeleteFlag = 1;//刪除標(biāo)志位,0為無效,1為存儲(chǔ)
SwitchFlag = 1;
LCD_All_Clear();//清全屏
I2C_Read_AT24C64(0x00,0x1e,1);
CardNum = RD24C64[0];
if(CardNum == 0)NSK12Flag = 1;
for(EEAddr = 0x0000;EEAddr < 0x0000 + CardMax * 0x20;EEAddr += 0x20){//讀出卡號(hào)和存儲(chǔ)器中的卡號(hào)進(jìn)行比較
I2C_Read_AT24C64(EAddrH,EAddrL,12);
if(RD24C64[0] == 1){
for(Cnt = 2;Cnt < 12;Cnt ++){//顯示讀取的卡號(hào)
if(RD24C64[Cnt] >= 0x40)
LCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x40+10]);
elseLCD_Data_DisNum(32 + (Cnt - 1) * 6,2,num[RD24C64[Cnt]-0x30]);
}
break;
}
}
LCD_Display6();//顯示存卡界面
}
}
break;
case 3:if(StoreFlag){//存儲(chǔ)卡號(hào)
Cnt = 0;
for(EEAddr = 0x0000;EEAddr < 0x0000 + CardMax * 0x20;EEAddr += 0x20){//讀出卡號(hào)和存儲(chǔ)器中的卡號(hào)進(jìn)行比較
I2C_Read_AT24C64(EAddrH,EAddrL,12);
if(RD24C64[0] == 1){
for(Cnt = 0;Cnt < 11;Cnt ++){//11位卡號(hào)進(jìn)行比較
if(CardNo[Cnt] != RD24C64[Cnt + 1])//比較過程中有不相等的就讀下一內(nèi)存
break;
}
}
if(Cnt == 11){//一組數(shù)據(jù)比較過程中全部相等時(shí),此卡已存在
CFlag = 1;
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Data_DisWord(34,4,ci);//顯示“此卡已存在”
LCD_Data_DisWord(46,4,ka);
LCD_Data_DisWord(58,4,yi);
LCD_Data_DisWord(70,4,cun);
LCD_Data_DisWord(82,4,zai);
break;
}
}
if(!CFlag){
I2C_Read_AT24C64(0x00,0x1e,1);
CardNum = RD24C64[0];
I2C_Read_AT24C64(0x00,0x1f,1);
CardMax = RD24C64[0];
if(CardNum == CardMax){//往后邊空間存儲(chǔ)
EEAddr = 0x0000 + CardMax * 0x20;
I2C_Write_AT24C64(EAddrH,EAddrL,&Sto,1);//存入標(biāo)志位,表示此頁已存入卡號(hào)
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
I2C_Write_AT24C64(EAddrH,EAddrL + 1,CardNo,11);//存入11位卡號(hào)
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
CardMax ++;
CardNum ++;
I2C_Write_AT24C64(0x00,0x1f,&CardMax,1);
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
I2C_Write_AT24C64(0x00,0x1e,&CardNum,1);
}
else if(CardMax > CardNum){//在CardMax范圍內(nèi)找空間存儲(chǔ)
for(EEAddr = 0x0000;EEAddr < 0x0000 + CardMax * 0x20;EEAddr += 0x20){//讀出卡號(hào)和存儲(chǔ)器中的卡號(hào)進(jìn)行比較
I2C_Read_AT24C64(EAddrH,EAddrL,12);
if(RD24C64[0] == 0){
I2C_Write_AT24C64(EAddrH,EAddrL,&Sto,1);//存入標(biāo)志位,表示此頁已存入卡號(hào)
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
I2C_Write_AT24C64(EAddrH,EAddrL + 1,CardNo,11);//存入11位卡號(hào)
CardNum ++;
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
I2C_Write_AT24C64(0x00,0x1e,&CardNum,1);
break;
}
}
}
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Data_DisWord(34,4,cun);//顯示“存入成功”
LCD_Data_DisWord(46,4,ru);
LCD_Data_DisWord(58,4,cheng);
LCD_Data_DisWord(70,4,gong);
}
DelCount = 0;//延時(shí)2s
StoreFlag = 0;
TimeDisFlag = 0;//返回后繼續(xù)顯示時(shí)間,時(shí)間顯示標(biāo)志為清零
while(DelCount < 400);
LCD_All_Clear();//清全屏
}
if(DeleteFlag){//刪除
I2C_Read_AT24C64(0x00,0x1e,1);
CardNum = RD24C64[0];
if(CardNum != 0){
I2C_Write_AT24C64(EAddrH,EAddrL,&Del,1);
DelCount = 0;//延時(shí)10ms
while(DelCount < 2);
CardNum --;//刪除一張卡后卡的數(shù)量減1
I2C_Write_AT24C64(0x00,0x1e,&CardNum,1);//存入標(biāo)志位,表示此頁已存入卡號(hào)
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Data_DisWord(34,4,shan);//顯示“刪除成功”
LCD_Data_DisWord(46,4,chu);
LCD_Data_DisWord(58,4,cheng);
LCD_Data_DisWord(70,4,gong);
DelCount = 0;//延時(shí)2s
while(DelCount < 400);
}
DeleteFlag = 0;
SwitchFlag = 0;
TimeDisFlag = 0;//返回后繼續(xù)顯示時(shí)間,時(shí)間顯示標(biāo)志為清零
LCD_All_Clear();//清全屏
}
NSK12Flag = 0;
WXFlag = 0;
break;
case 4:if(StoreFlag || DeleteFlag){
TimeDisFlag = 0;//推出存儲(chǔ)或刪除,顯示時(shí)間
StoreFlag = 0;
WXFlag = 0;
DeleteFlag = 0;
NSK12Flag = 0;
SwitchFlag = 0;
LCD_All_Clear();//清全屏
}
break;
default:break;
}
Function = 0;
}
void TimeInit(void){//定時(shí)器,串口初始化(定時(shí)器1作為波特率發(fā)生器[9600BPS],定時(shí)器0作為程序中的延時(shí))
TMOD = 0x21;//定時(shí)器0,工作方式1,定時(shí)器1,工作方式2
TH0 = 0xec;//定時(shí)器賦初值:5ms
TL0 = 0x78;
TR0 = 1;//啟動(dòng)定時(shí)器0
ET0 = 1;//開定時(shí)器0中斷
TH1 = 0xfd;//波特率發(fā)生器:9600BPS
TL1 = 0xfd;
TR1 = 1;
SCON = 0x50;//串行工作方式1,允許接收
PCON = 0x00;//SMOD = 0
ES = 1;//開串行通信中斷
EA = 1;//開中斷總開關(guān)
}
/***************************主程序******************************/
main(){
uchar Cnt;
bit ComFlag = 0;
CON_4053 = 1;//允許讀卡
Motor1 = 1;//電機(jī)正轉(zhuǎn)停止
Motor2 = 1;//電機(jī)反轉(zhuǎn)停止
TimeInit();//定時(shí)器,串口初始化
LCD_Reset();//液晶屏初始化設(shè)置
LCD_All_Clear();//清全屏
DS1302_SetTime();//1302設(shè)置初始時(shí)間
AT24C64Init();//24C64初始化
while(1){
if(!TimeDisFlag){
LCD_Display1();
LCD_DisTime();
}
KeyScan();//按鍵掃描
KeyFunc();//按鍵功能分配
if(KEndFlag){//卡號(hào)接收完畢
//KEndFlag = 0;
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Display2();
for(Cnt = 1;Cnt < 11;Cnt ++){//顯示讀取的卡號(hào)
if(CardNo[Cnt] >= 0x40)
LCD_Data_DisNum(32 + Cnt * 6,4,num[CardNo[Cnt]-0x40+10]);
elseLCD_Data_DisNum(32 + Cnt * 6,4,num[CardNo[Cnt]-0x30]);
}
Cnt = 0;//顯示卡號(hào)后Cnt已經(jīng)是11,故先清零,以便下邊比較卡號(hào)時(shí)使用Cnt == 11
for(EEAddr = 0x0000;EEAddr < 0x0000 + CardMax * 0x20;EEAddr += 0x20){//讀出卡號(hào)和存儲(chǔ)器中的卡號(hào)進(jìn)行比較
I2C_Read_AT24C64(EAddrH,EAddrL,12);
if(RD24C64[0] == 1){
for(Cnt = 0;Cnt < 11;Cnt ++){//11位卡號(hào)進(jìn)行比較
if(CardNo[Cnt] != RD24C64[Cnt + 1])//比較過程中有不相等的就讀下一內(nèi)存
break;
}
}
if(Cnt == 11){//一組數(shù)據(jù)比較過程中全部相等時(shí),顯示有效并推出比較
ComFlag = 1;//比較后的卡號(hào)相同
LCD_Data_DisWord(70,6,you);//顯示“有效”
LCD_Data_DisWord(82,6,xiao);
DelCount = 0;//延時(shí)100ms
while(DelCount < 20);
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Page_Clear(6);//清除6,7頁的顯示內(nèi)容
break;
}
}
LCD_DisTime();//若比較卡號(hào)時(shí)耗時(shí)較長,及時(shí)讀取時(shí)間顯示
if(!ComFlag){//比較無此卡號(hào)時(shí)顯示“無效”
LCD_Data_DisWord(100,6,wu);
LCD_Data_DisWord(112,6,xiao);
DelCount = 0;
while(1){
LCD_DisTime();//讀取時(shí)間顯示
if(DelCount > 400){
LCD_Page_Clear(4);//清除4,5頁的顯示內(nèi)容
LCD_Page_Clear(6);//清除6,7頁的顯示內(nèi)容
break;
}
}
}
if(ComFlag){
LCD_Display3();
LCD_Data_DisWord(60,4,da);
LCD_Data_DisWord(72,4,kai);
Motor1 = 0;//電機(jī)正轉(zhuǎn)
DelCount = 0;
while(1){//電機(jī)正轉(zhuǎn)中
LCD_DisTime();//時(shí)間顯示
if(DelCount > 600)
break;
}
Motor1 = 1;//電機(jī)正轉(zhuǎn)停止
LCD_Data_DisWord(60,4,guan);
LCD_Data_DisWord(72,4,bi);
DelCount = 0;
while(1){//等待人過去
LCD_DisTime();//時(shí)間顯示
if(DelCount > 600)
break;
}
Motor2 = 0;//電機(jī)反轉(zhuǎn)
LCD_Data_DisWord(60,4,da);
LCD_Data_DisWord(72,4,kai);
DelCount = 0;
while(1){//電機(jī)反轉(zhuǎn)中
LCD_DisTime();//時(shí)間顯示
if(DelCount > 600)
break;
}
Motor2 = 1;//電機(jī)反轉(zhuǎn)停止
DelCount = 0;
LCD_Data_DisWord(60,4,guan);
LCD_Data_DisWord(72,4,bi);
while(1){//門關(guān)上上后延時(shí)2s
LCD_DisTime();//時(shí)間顯示
if(DelCount > 400)
break;
}
LCD_Page_Clear(4);
LCD_Page_Clear(6);
}
ComFlag = 0;
KEndFlag = 0;
}
}
}
void Serial() interrupt 4{//串行中斷入口地址
if(RI){
CardNo[SCount] = SBUF;
RI = 0;
SCount ++;
if(SCount == 14){
KEndFlag = 1;
SCount = 0;
}
}
}
void Time0() interrupt 1{//定時(shí)器0入口地址
TH0 = 0xec;// 5ms
TL0 = 0x78;
DelCount ++;
}
評(píng)論