|
#include <reg51.h>
typedef unsigned char uchar;
typedef unsigned int uint;
#define BLANKCHAR 10
#define PCHAR 11
#define OPENCHAR 12
#define ALARMCHAR 13
#define LINECHAR 14
#define BACKKEY 0X0D
#define ENTERKEY 0X0F
#define LOCKKEY 0X0E
#define NO_KEY 20
uchar code KEYCODE[] = {0XEE,0XDE,0XBE,0X7E,0XED,0XDD,0XBD,0X7D,0XEB,0XDB,0XBB,0X7B,0XE7,0XD7,0XB7,0X77};
uchar KeyPre;
uchar KeyUp;
#define LEDPORT P0
#define LEDCON P1
#define KEYPORT P2
sbit ALARMCON = P3^3;
sbit LOCKCON = P3^5;
uchar code SEGCODE[] ={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90, 0XFF,0X8C,0X8F,0X88,0XBF,0X9f, };
uchar code BITCODE[6] = {0XC1,0XC2,0XC4,0XC8,0XD0,0XE0};
uchar DispBuf[6];
bit DispNormal;
uchar DispCnt;
#define SHORT_TIME 10
#define LONG_TIME 100
#define LONGER_TIME 9000
bit AlarmEnable;
uint AlarmTime;
uchar code PassWord[] = {1,2,3,4,5,6};
uchar PassInBuf[5];
uchar PassPosi;
bit TimerBit;
uchar SysMode;
uchar ErrorCnt;
void Delay(int z)
{
uchar y;
int x;
for(x=z; x>0; x--)
for(y=255; y>0; y--);
}
void Fill_Buf(uchar FillChar)
{
uchar i;
for (i=0; i<6; i++)
{
DispBuf[i] = FillChar;
PassInBuf[i] = FillChar;
}
}
void Fill_Buf_P()
{
Fill_Buf(BLANKCHAR);
DispBuf[0] = PCHAR;
LEDPORT = SEGCODE[PCHAR];
LEDCON = BITCODE[5];
}
void Fill_Buf_O()
{
Fill_Buf(BLANKCHAR);
DispBuf[0] = OPENCHAR;
LEDPORT = SEGCODE[OPENCHAR];
LEDCON = BITCODE[5];
}
void Fill_Buf_A()
{
Fill_Buf(BLANKCHAR);
DispBuf[0] = ALARMCHAR;
LEDPORT = SEGCODE[ALARMCHAR];
LEDCON = BITCODE[5];
}
void Disp_Led_Sin(uchar DispChar, uchar DispPosi)
{
LEDPORT = SEGCODE[DispChar];//輸出顯示段碼
LEDCON = BITCODE[DispPosi];//輸出顯示位碼
Delay(500);
LEDCON = 0X00;//關(guān)閉顯示器
}
void Sys_Speaker(uint stime)
{
AlarmEnable = 1;//允許報(bào)警
AlarmTime = stime;//報(bào)警時(shí)間長(zhǎng)短
}
/*********系統(tǒng)報(bào)警函數(shù)**********/
void Sys_Alarm()
{
if (AlarmEnable == 1) //允許報(bào)警
{
ALARMCON = 0;//報(bào)警
AlarmTime--;
if (AlarmTime == 0) //停止報(bào)警時(shí)間到
{
AlarmEnable = 0;
ALARMCON = 1;
if (SysMode == 1) //報(bào)警發(fā)生在模式1.要返回模式0
{
SysMode = 0;
Fill_Buf_P();//顯示P
}
}
}
}
uchar Find_Key()
{
uchar KeyTOP, i;
KEYPORT = 0XF0;//行線輸出全0,列線輸出全1
KeyTOP = KEYPORT;//讀按鍵端口值
if(KeyTOP == 0XF0)
return NO_KEY;
KEYPORT = KeyTOP | 0X0F;//列線輸出,行線輸入
KeyTOP = KEYPORT;
for (i=0; i<16; i++)
{
if(KeyTOP == KEYCODE[i])//根據(jù)按鍵端口掃描值,查找按鍵值
return i;//返回按鍵值
else if(i >= 15)
{
i = NO_KEY;
return i;
}
}
}
uchar Scan_Key()
{
uchar KeyTOP;
KeyTOP = Find_Key();//接收按鍵檢測(cè)的返回值
if (KeyTOP != NO_KEY)
{
Delay(100);
if (KeyTOP != NO_KEY)
KeyPre = KeyTOP;
while(KeyTOP == Find_Key());
return KeyPre;
}
else
return NO_KEY;
}
void Key_Process(uchar Key)
{
uchar i;
if (Key == NO_KEY)
return;//無(wú)按鍵,不處理
switch(SysMode)
{
case 0://輸入密碼
switch(Key)
{
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
Disp_Led_Sin(Key,PassPosi);
PassInBuf[PassPosi] = Key;//保存用戶輸入的密碼
if (PassPosi<5)
PassPosi++;//調(diào)整密碼輸入位置
Sys_Speaker(SHORT_TIME);//發(fā)按鍵提示音
break;
case 20:break;
case BACKKEY:
DispBuf[PassPosi] = BLANKCHAR;//顯示空鍵
PassInBuf[PassPosi] = BLANKCHAR;//清除當(dāng)前位置的密碼
if (PassPosi>0)
{
PassPosi--;//調(diào)整顯示位置
Disp_Led_Sin(15,5);
}
Sys_Speaker(SHORT_TIME);//發(fā)按鍵提示音
break;
case ENTERKEY:
for (i=0; i<5; i++)//比較用戶密碼是否與預(yù)設(shè)密碼一致
{
if (PassInBuf[i] != PassWord[i])
break;
}
if (i>=5)//輸入密碼正確
{
Fill_Buf_O();//顯示開(kāi)鎖狀態(tài)
PassPosi = 0;
LOCKCON = 1;//開(kāi)鎖
ErrorCnt = 0;
Sys_Speaker(LONG_TIME);//發(fā)長(zhǎng)提示音
SysMode = 2;//轉(zhuǎn)模式2
}
else
{
ErrorCnt++;
if(ErrorCnt>2)//超過(guò)3次
{
ErrorCnt = 0;
Fill_Buf_A();//顯示報(bào)警狀態(tài)
PassPosi = 0;
Sys_Speaker(LONGER_TIME);//發(fā)報(bào)警音
SysMode = 1;
}
else //少于3次,重新輸入
{
Fill_Buf_P();
PassPosi = 0;
Sys_Speaker(LONG_TIME);//發(fā)長(zhǎng)提示音
}
}
break;
case LOCKKEY://閉鎖鍵
Fill_Buf_P();
PassPosi = 0;
Sys_Speaker(SHORT_TIME);//發(fā)短提示音
break;
}
break;
case 2://開(kāi)鎖狀態(tài)
if (Key == LOCKKEY)//用戶按動(dòng)閉鎖按鍵
{
Fill_Buf_P();
SysMode = 0;
LOCKCON = 0;//閉鎖
Sys_Speaker(SHORT_TIME);
}
break;
}
}
void Init_Timer()
{
TMOD &= 0XF0;
TMOD |= 0X01;
TR0 = 0;
TH0 = (65536-20000) / 256;
TL0 = (65536-20000) % 256;
TR0 = 1;
ET0 = 1;
}
void Timer0() interrupt 1
{
TR0 = 0;
TH0 = (65536-20000) / 256;
TL0 = (65536-20000) % 256;
TR0 = 1;
TimerBit = 1;//20ms定時(shí)時(shí)間到
}
void Init_System()
{
PassPosi = 1;
LOCKCON = 0; //閉鎖
Init_Timer();
Fill_Buf_P();
EA = 1;
}
void main()
{
uchar KeyTemp;
Init_System();
PassPosi = 0;
while(1)
{
if(TimerBit == 1)
{
Sys_Alarm();//報(bào)警函數(shù)
KeyTemp = Scan_Key();//按鍵掃描
Key_Process(KeyTemp);//按鍵處理
TimerBit = 0;
}
}
}
|
|