找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 9299|回复: 17
收起左侧

门禁系统电路图及51单片机源码

  [复制链接]
ID:128158 发表于 2016-6-26 12:06 | 显示全部楼层 |阅读模式
简单门禁系统电路图及C++源码

0.png
门禁系统的电路原理图:

0.png

门禁系统的单片机程序源码(部分预览):

  1. #include <reg51.h>
  2. #include <intrins.h>
  3. #include "system.h"
  4. #include "interface.h"
  5. #include "IDCard.h"
  6. #include "display.h"
  7. #include "eeprom.h"
  8. #include "DS18B20.h"


  9. #define uchar unsigned char
  10. #define uint  unsigned int
  11. #define ulong unsigned long


  12. #define On  0
  13. #define Off 1

  14. #define Open  1
  15. #define Close 0

  16. #define Short 1
  17. #define Long  0


  18. #define KeyPort P2                //键盘接口
  19. sbit BEEP = P3^2;
  20. sbit Lock = P3^7;                //锁控制端

  21. #define CardNumAddr    0x123          //存放卡个数的地址
  22. #define CaedAddr       0x700          //存放卡号的地址
  23. #define CipherNumAddr  0x1234          //存放密码个数的地址
  24. #define CipherAddr     0x2000          //存放密码的地址
  25. #define RootCipherAddr 0x4567     //存放系统管理密码的地址
  26. #define SelfCheckAddr  0x3868     //自检地址

  27. uchar CardNum;
  28. uchar CipherNum;

  29. uchar Mode;
  30. #define ModeAddr 0x5678

  31. bit bdata BITinputing = 0;
  32. ulong xdata Cipher = 0;
  33. uchar xdata CipherBitNum = 0;
  34. uchar xdata InputCipherNum = 0;
  35. bit bdata BITinputnum0ut = 0;
  36. bit bdata BITcipherOK = 0;

  37. extern bit GetOneCard;
  38. extern uchar Card[];

  39. uint xdata Temperature = 0;      //温度值
  40. bit bdata BITtemp = 0;
  41. uint TempTimeCon = 48;

  42. uchar SecondCon = 0x25;    //0.5S
  43. bit bdata BITdispsec = 0;

  44. uint xdata KeyTimeCon = 0;
  45. bit bdata BITkeyout = 0;

  46. bit bdata BITbeep = 0;
  47. bit bdata BITbeepwait = 0;
  48. uchar xdata BeepTimeCon;
  49. uchar xdata BeepCopyTime;
  50. uchar xdata BeepNum;



  51. extern uchar code mimacuowu[];


  52. /*******************************************************
  53. * 函数名称:Init_Timer()
  54. * 函数功能:        
  55. * 入口参数:无
  56. * 出口参数:无
  57. * 函数说明:
  58. ********************************************************/
  59. void Init_Timer(void)
  60. {
  61.         TMOD = TMOD|0x01;         //定时器0: 20ms定时
  62.         TL0 = 0x00;   
  63.         TH0 = 0x70;
  64.         ET0 = 1;                     //开定时器0中断
  65.         TR0 = 1;                     //开是始定时器0计时

  66.         TMOD = TMOD|0x10;         //定时器1: 20ms定时
  67.         TL1 = 0x00;   
  68.         TH1 = 0x70;
  69.         ET1 = 0;                     //禁止定时器1中断
  70.         TR1 = 0;
  71. }

  72. /*******************************************************
  73. * 函数名称:Time0_INT()
  74. * 函数功能:        
  75. * 入口参数:无
  76. * 出口参数:无
  77. * 函数说明:
  78. ********************************************************/
  79. void Time0_INT(void) interrupt 1
  80. {
  81.         ET0 = 0;
  82.         TR0 = 0;
  83.         TL0 = 0x00;
  84.         TH0 = 0x70;
  85.        
  86.         if(KeyTimeCon)
  87.         {
  88.                 KeyTimeCon -= 1;
  89.                 if(KeyTimeCon == 0)
  90.                         BITkeyout = 1;
  91.         }

  92.         if(TempTimeCon)
  93.         {
  94.                  TempTimeCon -= 1;
  95.                 if(TempTimeCon == 0)
  96.                         BITtemp = 1;
  97.         }

  98.         if(SecondCon)
  99.         {
  100.                 SecondCon -= 1;
  101.                 if(SecondCon == 0)
  102.                 {
  103.                          SecondCon = 0x25;
  104.                         BITdispsec = 1;
  105.                 }
  106.         }
  107.        
  108.         if(BITbeep)
  109.         {
  110.                 if(BeepTimeCon == BeepCopyTime)
  111.                 {
  112.                         BEEP = ~BEEP;
  113.                 }

  114.                 BeepTimeCon -= 1;
  115.                 if(BeepTimeCon == 0)
  116.                 {
  117.                         BeepNum -= 1;
  118.                         BeepTimeCon = BeepCopyTime;
  119.                 }
  120.                        
  121.                 if(BeepNum == 0)
  122.                 {
  123.                         BITbeep = 0;
  124.                         BITbeepwait = 0;
  125.                         BEEP = Off;
  126.                 }
  127.         }
  128.                                                           
  129.         TR0=1;
  130.         ET0=1;
  131. }


  132. /*******************************************************
  133. * 函数名称:Time1_INT()
  134. * 函数功能:        
  135. * 入口参数:无
  136. * 出口参数:无
  137. * 函数说明:
  138. ********************************************************/
  139. void Time1_INT(void) interrupt 3
  140. {
  141.         ET1=0;
  142.         TR1=0;
  143.         TL1=0x00;
  144.         TH1=0x70;
  145.        

  146.         TR1=1;
  147.         ET1=1;       
  148. }


  149. /*******************************************************
  150. * 函数名称:
  151. * 函数功能:        
  152. * 入口参数:无
  153. * 出口参数:无
  154. * 函数说明:
  155. ********************************************************/
  156. void SystemInit(void)
  157. {
  158.         uint dat;

  159. //        Lock = Open;

  160.         LCDInit();
  161.         Init_Timer();
  162.         DS18B20Init(0x7f,0x64,0x8a);
  163.         DS18B20StartConvert();

  164.         dat = ReadEEPROM(SelfCheckAddr+1);                          //读系统自检标饰位
  165.         dat = (dat<<8) | ReadEEPROM(SelfCheckAddr);

  166.         if(dat == 0xAA55)                //系统已经初始化过
  167.         {
  168.                 CardNum = ReadEEPROM(CardNumAddr);
  169.             CipherNum = ReadEEPROM(CipherNumAddr);
  170.                 Mode = ReadEEPROM(ModeAddr);
  171.         }
  172.         else                                        //系统第一次使用
  173.         {                                               
  174.                 WriteEEPROM(SelfCheckAddr,0xAA55);        //标记系统已设置过
  175.                 WriteEEPROM(CipherAddr,0x5678);                //初始化系统管理员密码为 12345678
  176.                 WriteEEPROM(CipherAddr,0x1234);

  177.                 CardNum = 0;
  178.                 WriteEEPROM(CardNumAddr,CardNum);
  179.                 CipherNum = 0;
  180.                 WriteEEPROM(CipherNumAddr,CipherNum);
  181.                 Mode = 0x88;
  182.                 WriteEEPROM(ModeAddr,Mode);                   //默认为密码模式
  183.         }

  184.         TimeInterface();

  185.         EA  = 1;
  186. }


  187. /*******************************************************
  188. * 函数名称:
  189. * 函数功能:        
  190. * 入口参数:无
  191. * 出口参数:无
  192. * 函数说明:
  193. ********************************************************/
  194. void Beep(uchar num,bit state)
  195. {
  196.         while(BITbeepwait);

  197.         BeepNum = (num<<1)-1;                 //蜂鸣器响的次数

  198.         if(state)                                         //鸣叫时间=BeepCopyTime*20ms
  199.                 BeepCopyTime = 10;             //短鸣
  200.         else
  201.                 BeepCopyTime = 30;                 //长鸣

  202.         BeepTimeCon = BeepCopyTime;
  203.         BITbeep = 1;
  204.         BITbeepwait = 1;
  205. }



  206. /*******************************************************
  207. * 函数名称:
  208. * 函数功能:        
  209. * 入口参数:无
  210. * 出口参数:无
  211. * 函数说明:
  212. ********************************************************/
  213. uchar CheckKey(void)
  214. {
  215.         uchar key;

  216.         KeyPort = 0x0f;
  217.         key = KeyPort;
  218.         if(key&0x0f != 0x0f)
  219.         {
  220.                 DelayMS(10);
  221.                 if(KeyPort == key)
  222.                 {
  223.                         KeyPort = 0xf0;
  224.                         key = (key&0x0f) | (KeyPort&0xf0);

  225. //                        Beep(1,Short);

  226.                         switch(key)
  227.                         {
  228.                                 case 0x00: return 1;  break;
  229.                                 case 0x01: return 2;  break;
  230.                                 case 0x02: return 3;  break;
  231.                                 case 0x03: return 4;  break;
  232.                                 case 0x04: return 5;  break;
  233.                                 case 0x05: return 6;  break;
  234.                                 case 0x06: return 7;  break;
  235.                                 case 0x07: return 8;  break;
  236.                                 case 0x08: return 9;  break;
  237.                                 case 0x09: return 10; break;
  238.                                 case 0x0a: return 11; break;
  239.                                 case 0x0b: return 12; break;
  240.                                 case 0x0c: return 13; break;
  241.                                 case 0x0d: return 14; break;
  242.                                 case 0x0e: return 15; break;
  243.                                 case 0x0f: return 16; break;
  244.                                 default  : return 0;  break;
  245.                         }
  246.                 }               
  247.                 return 0;       
  248.         }       
  249.         return 0;
  250. }


  251. /*******************************************************
  252. * 函数名称:
  253. * 函数功能:        
  254. * 入口参数:无
  255. * 出口参数:无
  256. * 函数说明:
  257. ********************************************************/
  258. void KeyWork(uchar key)
  259. {
  260.         if(key)
  261.         {
  262.                 if(key == 1)
  263.                         SetupMode();
  264.                
  265.                 else if(key>=0 && key<=9)
  266.                         InputCipher(key);

  267.                 else if(key==12 || key==13)
  268.                 {
  269.                         if(BITinputing)
  270.                                  ValidateCipher(key);       
  271.                 }
  272.         }
  273. }



  274. /*******************************************************
  275. * 函数名称:
  276. * 函数功能:输入密码        
  277. * 入口参数:无
  278. * 出口参数:无
  279. * 函数说明:
  280. ********************************************************/
  281. void InputCipher(cph)
  282. {
  283.         InputCipherInterface();                  //输入密码的界面

  284.         if(BITinputing == 0)
  285.                 BITinputing = 1;

  286.         Cipher = Cipher<<4 | cph;

  287.         if(CipherBitNum < 8)                  
  288.                 CipherBitNum += 1;

  289.         KeyTimeCon = 0x400;     //8S回主界面
  290. }


  291. /*******************************************************
  292. * 函数名称:
  293. * 函数功能:确认或重输密码        
  294. * 入口参数:无
  295. * 出口参数:无
  296. * 函数说明:将输入的密码和已设定的多个密码相比较
  297. ********************************************************/
  298. void ValidateCipher(uchar key)
  299. {
  300.         uchar i,j;
  301.         uint cphaddr=CipherAddr;
  302.         ulong cph=0;

  303.         if(key == 12)                 //确定键
  304.         {
  305.                 InputCipherNum += 1;
  306.                 for(j=0;j<CipherNum+1;j++)
  307.                 {
  308.                          for(i=0;i<4;i++)
  309.                                 cph = cph<<8 + ReadEEPROM(cphaddr+i);

  310.                         if(cph == Cipher)                         //密码正确
  311.                         {
  312.                                 Lock = Open;
  313.                                 Cipher = 0;
  314.                                 CipherBitNum = 0;
  315.                                 InputCipherNum = 0;
  316.                                 BITinputnum0ut = 0;
  317.                                 BITinputing = 0;
  318.                                 KeyTimeCon = 0;
  319.                                 BITcipherOK = 1;
  320.                                 TimeInterface();
  321.                                 Beep(1,Long);
  322.                                 return;
  323.                         }

  324.                         cphaddr += 0x100;
  325.                 }
  326.                
  327.                 if(j > CipherNum)                //密码不正确
  328.                 {
  329.                         Cipher = 0;
  330.                         CipherBitNum = 0;
  331.                         DisplayMoreWord(3,28,mimacuowu,16,0,5);     //密码错误!
  332.                         KeyTimeCon = 200;
  333.                         if(InputCipherNum == 3)
  334.                         {
  335.                                 BITinputnum0ut = 1;       
  336.                                 InputCipherNum = 0;
  337.                         }
  338.                         Beep(2,Short);       
  339.                 }
  340.         }
  341.         else                                 //取消键
  342.         {
  343.                 if(CipherBitNum == 0)          //连按两次取消键则返回主界面
  344.                 {
  345.                         Cipher = 0;
  346.                          BITinputing = 0;
  347.                         KeyTimeCon = 0;
  348.                         TimeInterface();
  349.                         Beep(1,Long);       
  350.                 }
  351.                 else
  352.                 {
  353.                         CipherBitNum = 0;
  354.                         Cipher = 0;
  355.                         ClearRect(5,32,6,96);
  356.                 }
  357.         }       
  358. }


  359. /*******************************************************
  360. * 函数名称:
  361. * 函数功能:        
  362. * 入口参数:无
  363. * 出口参数:无
  364. * 函数说明:
  365. ********************************************************/
  366. uchar ValidateIDCard(void)
  367. {
  368.         uchar i,j,idH,idM,idL,RidH,RidM,RidL;
  369.         uint idaddr=CaedAddr;

  370.         EX1 = 0;

  371.         for(i=0;i<6;i++)
  372.         {
  373.                  Card[i+5] -= 0x30;
  374.                 if(Card[i+5]>0x10)
  375.                         Card[i+5] -= 0x07;
  376.         }

  377.         idH = (Card[5]<<4) | Card[6];
  378.         idM = (Card[7]<<4) | Card[8];
  379.         idL = (Card[9]<<4) | Card[10];

  380.         for(j=0;j<CardNum;j++)
  381.         {
  382.                  RidL = ReadEEPROM(idaddr);
  383.                 RidM = ReadEEPROM(idaddr+1);
  384.                 RidH = ReadEEPROM(idaddr+2);

  385.                 if((idL == RidL) && (idM == RidM) && (idH == RidH))
  386.                 {
  387.                         Lock = Open;
  388.                         Beep(1,Long);
  389.                         EX1 = 1;
  390.                         return 1;       
  391.                 }

  392.                 idaddr + 0x100;
  393.         }

  394.         if(j == CardNum)
  395.                 Beep(2,Short);

  396.         EX1 = 1;
  397.        
  398.         return 0;       
  399. }


  400. /*******************************************************
  401. * 函数名称:
  402. * 函数功能:密码模式主循环        
  403. * 入口参数:无
  404. * 出口参数:无
  405. * 函数说明:
  406. ********************************************************/
  407. void CipherMode(void)
  408. {
  409.         EX1 = 0;     //关闭刷卡功能

  410.         while(Mode == 0x88)
  411.         {
  412.                 KeyWork(CheckKey());

  413.                 if(_testbit_(BITdispsec) && !BITinputing)
  414.                         RefurbishTime();
  415.        
  416.                 if(_testbit_(BITtemp) && !BITinputing)
  417.                         RefurbishTemp();

  418.                 if(_testbit_(BITkeyout))
  419.                 {
  420.                         Cipher = 0;
  421.                          BITinputing = 0;
  422.                         CipherBitNum = 0;
  423.                         TimeInterface();
  424.                 }               
  425.         }
  426. }


  427. /*******************************************************
  428. * 函数名称:
  429. * 函数功能:ID卡模式主循环        
  430. * 入口参数:无
  431. * 出口参数:无
  432. * 函数说明:
  433. ********************************************************/
  434. void IDCardMode(void)
  435. {
  436.         EX1 = 1;

  437.         while(Mode == 0x44)
  438.         {
  439.                 if(_testbit_(BITdispsec))
  440.                         RefurbishTime();
  441.        
  442.                 if(_testbit_(BITtemp))
  443.                         RefurbishTemp();

  444.                 if(_testbit_(GetOneCard))
  445.                         ValidateIDCard();

  446.                 if(CheckKey() == 1)
  447.                         SetupMode();
  448.         }
  449. }


  450. /*******************************************************
  451. * 函数名称:
  452. * 函数功能:双保险模式        
  453. * 入口参数:无
  454. * 出口参数:无
  455. * 函数说明:先刷卡在输密码
  456. ********************************************************/
  457. void TwoSafetyMode(void)
  458. {
  459.         EX1 = 1;

  460.         while(Mode == 0x22)
  461.         {
  462.                    if(_testbit_(BITdispsec))
  463.                         RefurbishTime();
  464.        
  465.                 if(_testbit_(BITtemp))
  466.                         RefurbishTemp();

  467.                 if(_testbit_(GetOneCard))
  468.                 {
  469.                         if(ValidateIDCard() == 1)
  470.                         {
  471.                                 KeyTimeCon = 500;                 //10S
  472.                                 while(!BITkeyout && !BITinputnum0ut)
  473.                                          KeyWork(CheckKey());

  474.                                 if(!_testbit_(BITcipherOK))
  475.                                 {
  476.                                         BITinputnum0ut = 0;
  477.                                         BITkeyout = 0;
  478.                                         KeyTimeCon = 0;
  479.                                         Cipher = 0;
  480.                                          BITinputing = 0;
  481.                                         CipherBitNum = 0;
  482.                                         TimeInterface();
  483.                                 }
  484.                         }
  485.                 }

  486.                 if(CheckKey() == 1)
  487.                         SetupMode();
  488.         }
  489. }


  490. /*******************************************************
  491. * 函数名称:
  492. * 函数功能:系统设置模式        
  493. * 入口参数:无
  494. * 出口参数:无
  495. * 函数说明:
  496. ********************************************************/
  497. void SetupMode(void)
  498. {
  499.        
  500. }
复制代码


门禁系统电路图及C 源码.zip

2.43 MB, 下载次数: 265, 下载积分: 黑币 -5

评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

回复

使用道具 举报

ID:128134 发表于 2016-6-27 22:15 | 显示全部楼层
很好的样子,有时间自己研究研究
回复

使用道具 举报

ID:146632 发表于 2016-12-26 09:37 | 显示全部楼层
看样子不错,谢谢啦!
回复

使用道具 举报

ID:182843 发表于 2017-3-24 10:58 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:183400 发表于 2017-3-25 21:03 | 显示全部楼层
好东西 感谢楼主分享
回复

使用道具 举报

ID:198664 发表于 2017-5-9 15:49 | 显示全部楼层
感谢楼主分享
回复

使用道具 举报

ID:245426 发表于 2017-11-2 19:25 | 显示全部楼层
好资料,51黑有你更精彩!!!
回复

使用道具 举报

ID:101726 发表于 2017-11-3 23:35 | 显示全部楼层
谢谢 分享  谢谢
回复

使用道具 举报

ID:246166 发表于 2017-11-8 18:33 | 显示全部楼层
楼主太强了,学习学习
回复

使用道具 举报

ID:183751 发表于 2017-12-4 12:11 | 显示全部楼层
楼主太强了,学习学习
回复

使用道具 举报

ID:241242 发表于 2017-12-8 08:52 | 显示全部楼层
看起来不错,下载学习一下。
回复

使用道具 举报

ID:260938 发表于 2017-12-13 08:46 | 显示全部楼层
很厉害,666666666666666
回复

使用道具 举报

ID:263943 发表于 2017-12-19 21:02 | 显示全部楼层
感谢楼主!!   厉害
回复

使用道具 举报

ID:336488 发表于 2018-5-23 14:39 | 显示全部楼层
感谢楼主
回复

使用道具 举报

ID:336690 发表于 2018-5-23 17:47 | 显示全部楼层
感谢你!!!!
回复

使用道具 举报

ID:32627 发表于 2018-6-7 07:32 | 显示全部楼层
下载学习一下。感谢你!
回复

使用道具 举报

ID:206230 发表于 2020-3-16 20:20 | 显示全部楼层
下载学习一下。感谢你
回复

使用道具 举报

ID:782249 发表于 2020-6-17 22:38 | 显示全部楼层

感谢楼主分享!!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表