|
简单门禁系统电路图及C++源码
门禁系统的电路原理图:
门禁系统的单片机程序源码(部分预览):
- #include <reg51.h>
- #include <intrins.h>
- #include "system.h"
- #include "interface.h"
- #include "IDCard.h"
- #include "display.h"
- #include "eeprom.h"
- #include "DS18B20.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define ulong unsigned long
- #define On 0
- #define Off 1
- #define Open 1
- #define Close 0
- #define Short 1
- #define Long 0
- #define KeyPort P2 //键盘接口
- sbit BEEP = P3^2;
- sbit Lock = P3^7; //锁控制端
- #define CardNumAddr 0x123 //存放卡个数的地址
- #define CaedAddr 0x700 //存放卡号的地址
- #define CipherNumAddr 0x1234 //存放密码个数的地址
- #define CipherAddr 0x2000 //存放密码的地址
- #define RootCipherAddr 0x4567 //存放系统管理密码的地址
- #define SelfCheckAddr 0x3868 //自检地址
- uchar CardNum;
- uchar CipherNum;
- uchar Mode;
- #define ModeAddr 0x5678
- bit bdata BITinputing = 0;
- ulong xdata Cipher = 0;
- uchar xdata CipherBitNum = 0;
- uchar xdata InputCipherNum = 0;
- bit bdata BITinputnum0ut = 0;
- bit bdata BITcipherOK = 0;
- extern bit GetOneCard;
- extern uchar Card[];
- uint xdata Temperature = 0; //温度值
- bit bdata BITtemp = 0;
- uint TempTimeCon = 48;
- uchar SecondCon = 0x25; //0.5S
- bit bdata BITdispsec = 0;
- uint xdata KeyTimeCon = 0;
- bit bdata BITkeyout = 0;
- bit bdata BITbeep = 0;
- bit bdata BITbeepwait = 0;
- uchar xdata BeepTimeCon;
- uchar xdata BeepCopyTime;
- uchar xdata BeepNum;
- extern uchar code mimacuowu[];
- /*******************************************************
- * 函数名称:Init_Timer()
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void Init_Timer(void)
- {
- TMOD = TMOD|0x01; //定时器0: 20ms定时
- TL0 = 0x00;
- TH0 = 0x70;
- ET0 = 1; //开定时器0中断
- TR0 = 1; //开是始定时器0计时
- TMOD = TMOD|0x10; //定时器1: 20ms定时
- TL1 = 0x00;
- TH1 = 0x70;
- ET1 = 0; //禁止定时器1中断
- TR1 = 0;
- }
- /*******************************************************
- * 函数名称:Time0_INT()
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void Time0_INT(void) interrupt 1
- {
- ET0 = 0;
- TR0 = 0;
- TL0 = 0x00;
- TH0 = 0x70;
-
- if(KeyTimeCon)
- {
- KeyTimeCon -= 1;
- if(KeyTimeCon == 0)
- BITkeyout = 1;
- }
- if(TempTimeCon)
- {
- TempTimeCon -= 1;
- if(TempTimeCon == 0)
- BITtemp = 1;
- }
- if(SecondCon)
- {
- SecondCon -= 1;
- if(SecondCon == 0)
- {
- SecondCon = 0x25;
- BITdispsec = 1;
- }
- }
-
- if(BITbeep)
- {
- if(BeepTimeCon == BeepCopyTime)
- {
- BEEP = ~BEEP;
- }
- BeepTimeCon -= 1;
- if(BeepTimeCon == 0)
- {
- BeepNum -= 1;
- BeepTimeCon = BeepCopyTime;
- }
-
- if(BeepNum == 0)
- {
- BITbeep = 0;
- BITbeepwait = 0;
- BEEP = Off;
- }
- }
-
- TR0=1;
- ET0=1;
- }
- /*******************************************************
- * 函数名称:Time1_INT()
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void Time1_INT(void) interrupt 3
- {
- ET1=0;
- TR1=0;
- TL1=0x00;
- TH1=0x70;
-
- TR1=1;
- ET1=1;
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void SystemInit(void)
- {
- uint dat;
- // Lock = Open;
- LCDInit();
- Init_Timer();
- DS18B20Init(0x7f,0x64,0x8a);
- DS18B20StartConvert();
- dat = ReadEEPROM(SelfCheckAddr+1); //读系统自检标饰位
- dat = (dat<<8) | ReadEEPROM(SelfCheckAddr);
- if(dat == 0xAA55) //系统已经初始化过
- {
- CardNum = ReadEEPROM(CardNumAddr);
- CipherNum = ReadEEPROM(CipherNumAddr);
- Mode = ReadEEPROM(ModeAddr);
- }
- else //系统第一次使用
- {
- WriteEEPROM(SelfCheckAddr,0xAA55); //标记系统已设置过
- WriteEEPROM(CipherAddr,0x5678); //初始化系统管理员密码为 12345678
- WriteEEPROM(CipherAddr,0x1234);
- CardNum = 0;
- WriteEEPROM(CardNumAddr,CardNum);
- CipherNum = 0;
- WriteEEPROM(CipherNumAddr,CipherNum);
- Mode = 0x88;
- WriteEEPROM(ModeAddr,Mode); //默认为密码模式
- }
- TimeInterface();
- EA = 1;
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void Beep(uchar num,bit state)
- {
- while(BITbeepwait);
- BeepNum = (num<<1)-1; //蜂鸣器响的次数
- if(state) //鸣叫时间=BeepCopyTime*20ms
- BeepCopyTime = 10; //短鸣
- else
- BeepCopyTime = 30; //长鸣
- BeepTimeCon = BeepCopyTime;
- BITbeep = 1;
- BITbeepwait = 1;
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- uchar CheckKey(void)
- {
- uchar key;
- KeyPort = 0x0f;
- key = KeyPort;
- if(key&0x0f != 0x0f)
- {
- DelayMS(10);
- if(KeyPort == key)
- {
- KeyPort = 0xf0;
- key = (key&0x0f) | (KeyPort&0xf0);
- // Beep(1,Short);
- switch(key)
- {
- case 0x00: return 1; break;
- case 0x01: return 2; break;
- case 0x02: return 3; break;
- case 0x03: return 4; break;
- case 0x04: return 5; break;
- case 0x05: return 6; break;
- case 0x06: return 7; break;
- case 0x07: return 8; break;
- case 0x08: return 9; break;
- case 0x09: return 10; break;
- case 0x0a: return 11; break;
- case 0x0b: return 12; break;
- case 0x0c: return 13; break;
- case 0x0d: return 14; break;
- case 0x0e: return 15; break;
- case 0x0f: return 16; break;
- default : return 0; break;
- }
- }
- return 0;
- }
- return 0;
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void KeyWork(uchar key)
- {
- if(key)
- {
- if(key == 1)
- SetupMode();
-
- else if(key>=0 && key<=9)
- InputCipher(key);
- else if(key==12 || key==13)
- {
- if(BITinputing)
- ValidateCipher(key);
- }
- }
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:输入密码
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void InputCipher(cph)
- {
- InputCipherInterface(); //输入密码的界面
- if(BITinputing == 0)
- BITinputing = 1;
- Cipher = Cipher<<4 | cph;
- if(CipherBitNum < 8)
- CipherBitNum += 1;
- KeyTimeCon = 0x400; //8S回主界面
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:确认或重输密码
- * 入口参数:无
- * 出口参数:无
- * 函数说明:将输入的密码和已设定的多个密码相比较
- ********************************************************/
- void ValidateCipher(uchar key)
- {
- uchar i,j;
- uint cphaddr=CipherAddr;
- ulong cph=0;
- if(key == 12) //确定键
- {
- InputCipherNum += 1;
- for(j=0;j<CipherNum+1;j++)
- {
- for(i=0;i<4;i++)
- cph = cph<<8 + ReadEEPROM(cphaddr+i);
- if(cph == Cipher) //密码正确
- {
- Lock = Open;
- Cipher = 0;
- CipherBitNum = 0;
- InputCipherNum = 0;
- BITinputnum0ut = 0;
- BITinputing = 0;
- KeyTimeCon = 0;
- BITcipherOK = 1;
- TimeInterface();
- Beep(1,Long);
- return;
- }
- cphaddr += 0x100;
- }
-
- if(j > CipherNum) //密码不正确
- {
- Cipher = 0;
- CipherBitNum = 0;
- DisplayMoreWord(3,28,mimacuowu,16,0,5); //密码错误!
- KeyTimeCon = 200;
- if(InputCipherNum == 3)
- {
- BITinputnum0ut = 1;
- InputCipherNum = 0;
- }
- Beep(2,Short);
- }
- }
- else //取消键
- {
- if(CipherBitNum == 0) //连按两次取消键则返回主界面
- {
- Cipher = 0;
- BITinputing = 0;
- KeyTimeCon = 0;
- TimeInterface();
- Beep(1,Long);
- }
- else
- {
- CipherBitNum = 0;
- Cipher = 0;
- ClearRect(5,32,6,96);
- }
- }
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- uchar ValidateIDCard(void)
- {
- uchar i,j,idH,idM,idL,RidH,RidM,RidL;
- uint idaddr=CaedAddr;
- EX1 = 0;
- for(i=0;i<6;i++)
- {
- Card[i+5] -= 0x30;
- if(Card[i+5]>0x10)
- Card[i+5] -= 0x07;
- }
- idH = (Card[5]<<4) | Card[6];
- idM = (Card[7]<<4) | Card[8];
- idL = (Card[9]<<4) | Card[10];
- for(j=0;j<CardNum;j++)
- {
- RidL = ReadEEPROM(idaddr);
- RidM = ReadEEPROM(idaddr+1);
- RidH = ReadEEPROM(idaddr+2);
- if((idL == RidL) && (idM == RidM) && (idH == RidH))
- {
- Lock = Open;
- Beep(1,Long);
- EX1 = 1;
- return 1;
- }
- idaddr + 0x100;
- }
- if(j == CardNum)
- Beep(2,Short);
- EX1 = 1;
-
- return 0;
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:密码模式主循环
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void CipherMode(void)
- {
- EX1 = 0; //关闭刷卡功能
- while(Mode == 0x88)
- {
- KeyWork(CheckKey());
- if(_testbit_(BITdispsec) && !BITinputing)
- RefurbishTime();
-
- if(_testbit_(BITtemp) && !BITinputing)
- RefurbishTemp();
- if(_testbit_(BITkeyout))
- {
- Cipher = 0;
- BITinputing = 0;
- CipherBitNum = 0;
- TimeInterface();
- }
- }
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:ID卡模式主循环
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void IDCardMode(void)
- {
- EX1 = 1;
- while(Mode == 0x44)
- {
- if(_testbit_(BITdispsec))
- RefurbishTime();
-
- if(_testbit_(BITtemp))
- RefurbishTemp();
- if(_testbit_(GetOneCard))
- ValidateIDCard();
- if(CheckKey() == 1)
- SetupMode();
- }
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:双保险模式
- * 入口参数:无
- * 出口参数:无
- * 函数说明:先刷卡在输密码
- ********************************************************/
- void TwoSafetyMode(void)
- {
- EX1 = 1;
- while(Mode == 0x22)
- {
- if(_testbit_(BITdispsec))
- RefurbishTime();
-
- if(_testbit_(BITtemp))
- RefurbishTemp();
- if(_testbit_(GetOneCard))
- {
- if(ValidateIDCard() == 1)
- {
- KeyTimeCon = 500; //10S
- while(!BITkeyout && !BITinputnum0ut)
- KeyWork(CheckKey());
- if(!_testbit_(BITcipherOK))
- {
- BITinputnum0ut = 0;
- BITkeyout = 0;
- KeyTimeCon = 0;
- Cipher = 0;
- BITinputing = 0;
- CipherBitNum = 0;
- TimeInterface();
- }
- }
- }
- if(CheckKey() == 1)
- SetupMode();
- }
- }
- /*******************************************************
- * 函数名称:
- * 函数功能:系统设置模式
- * 入口参数:无
- * 出口参数:无
- * 函数说明:
- ********************************************************/
- void SetupMode(void)
- {
-
- }
复制代码
|
评分
-
查看全部评分
|