⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 menjing.c

📁 门禁系统源码。实现只识别一个或几个号码!
💻 C
字号:
//#include <AT89X52.h>
#include "STC12C2052.h"
#include <intrins.h>


#define SDA P1_0
#define SCL P3_7
#define uchar unsigned char
#define uint unsigned int
#define green_led P1_1
#define open_door P1_3
#define sda_mokuai P1_5
#define scl_mokuai P3_3
#define WAITFORKEYBOARDPULSE  while(!scl_mokuai); while(scl_mokuai)

//uchar code ID_code[15]={0x7C,0x1C,0x00,0x00,0x00,0x60,0x1C,0x60,0x1C,0x80,0x60,0x60,0x1C,};
//7C 1C 00 00 00 60 9C 60 1C 80 60 60 1C 
//7C 1C 00 00 00 60 1C 60 1C 80 60 60 1C 
//AB 30 44 30 30 34 41 41 35 31 35 46 37 0D 0A 54
//AB 30 44 30 30 34 41 41 35 31 35 46 37 0D 0A 54 
//AB 30 44 30 30 34 41 41 35 31 35 46 37 0D 0A 54 
//30 44 30 30 34 41 41 35 31 35 46 37 0D 0A 0F AB 
uchar code ID_code[16]={0xAB,0x30,0x44,0x30,0x30,0x34,0x41,0x41,0x35,0x31,0x35,0x46,0x37,0x0D,0x0A,0x54,}; 
//AB 30 44 30 30 34 41 41 35 31 35 46 37 0D 0A 54 
//uchar code ID_code[16]={0x30,0x44,0x30,0x30,0x34,0x41,0x41,0x35,0x31,0x35,0x46,0x37,0x0D,0x0A,0x0F,0xAB }; 
//char code ID_code[16]={0x30,0x44,0x0f,0x30,0x34,0x41,0x41,0x35,0x31,0x35,0x46,0x37,0x0D,0x0A,0x54,0xAB }; 
//30 44 0F 30 34 41 41 35 31 35 46 37 0D 0A 54 AB
//30 44 30 30 34 41 0F 35 31 35 46 37 0D 0A 54 AB 
//30 44 0F 30 34 41 41 35 31 35 46 37 0D 0A 54 AB 
 uchar receive_buf[16]=0x00;

//78 38 00 00 00 40 18 40 38 00 40 40 38 

uchar count=0x00;//pass_flag=0x00;
/*
void Delay_52us_NOP(void)
{
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
}

void Delay_26us_NOP(void)
{
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
        _nop_();_nop_();_nop_();
}
*/
void iniMCU(void)
{
P1M1=0x08;
open_door=0;
//green_led=0;
P0=0xff;
//SMOD=1;
//green_led=0;
TMOD=0x22; // 定时器1 工作于方式2
//20
SCON=0x40; // 串口工作于方式1
TH1=0xfa; //装入初值,11.0592MHZ 的晶振,9600 的波特率
TL1=0xfa;
PCON=0x00; //波特率无倍增
TR1=1; //开启定时器1 开始工作
//TR0=1; //开启定时器0 开始工作
EA=1;
REN=1;
//WDT_CONTR = 0x3f;   //开硬件狗
ES=1;	

    //  IT0 = 0;      //低电平引起中断

        ES = 1;       //开串口中断
       // EX0 = 1;      //开外部0中断
	
}
	
/*********************************************************/
//函数:uart
//功能:串口中断
//输入:无
//输出:无
//描述:
/*********************************************************/
void uart() interrupt 4 using 2
{
//ES=0;
if(RI == 1)       //接收中断
{
 
receive_buf[count]=SBUF;

SBUF=receive_buf[count];
count++;
  if(SBUF==0x54){count=0x00;}//
  //pass_not();
RI= 0;
}
else if(TI == 1)  //发送中断
{
 
  TI = 0;
  //SBUF=0x77;
 // Delay_52us_NOP();
}
//ES=1;
}
void delay (uint us)   //delay time 
{ 
  while(us--); 
} 
void delay1 (uint ms) 
{ 
  uint i,j; 
  for(i=0;i<ms;i++) 
  for(j=0;j<15;j++) 
  delay(1); 
} 

void pass_not(void)
{
uchar temp=0x00,pass_flag=0x00;

for(temp=0x00;temp<16;temp++)
{		
	
	if(receive_buf[temp]==ID_code[temp])
		{  
			pass_flag++;
			if(pass_flag==15)
			{
				open_door=1;
				green_led=0;
				
				delay1(8000);
				delay1(8000);
				for(pass_flag=0x00;pass_flag<16;pass_flag++)
				{
				receive_buf[pass_flag]=0x00;	
				}
				pass_flag=0x00;
 				open_door=0;
				green_led=0xff;
			}
		}

}
}

/*
void ex1() interrupt 2 using 3
{        //时钟线变低
    unsigned char bitCount;
         unsigned char KeyCode = 0;
       //  if(KeyCodeSum < MAXBUFFER) KeyCodeBuff[KeyCodeSum] = 0;
	  // WAITFORKEYBOARDPULSE; 
while(scl_mokuai);
Delay_52us_NOP();
Delay_52us_NOP();
Delay_52us_NOP();
Delay_52us_NOP();
	  
        for(bitCount = 0; bitCount <8 ; bitCount ++)   // 把起始位算入
   {
  //  WAITFORKEYBOARDPULSE;     // 等待一个有效的下跳沿
  
    if(scl_mokuai == 1)
  {
      KeyCode |= 0x80; // 得到有效的数据位
     }
	  KeyCode  >>= 1;      // 按照PS2格式,数据低位在前
    scl_mokuai  = 1;
    sda_mokuai  = 1;
	Delay_52us_NOP();
	Delay_26us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
	Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();Delay_52us_NOP();
   }
//   WAITFORKEYBOARDPULSE;     // 等待按键发送效验位
   WAITFORKEYBOARDPULSE;     // 等待按键发送终止位
   while(!scl_mokuai);         // 等待键盘把时钟线拉高
      //  switch(KeyCode)

receive_buf[count]=KeyCode;
SBUF=receive_buf[count]; 
if(receive_buf[count]==ID_code[count]) pass_flag++; 
  count++;
  if(pass_flag>6){open_door=1;green_led=0;}
  if(count>12){count=0x00;pass_flag=0x00;}//
  //pass_not();
}

*/
/**********************************************************/
//#include "24C64.c"

void main(void)
{

iniMCU();
while(1)
{
//RI=1;
pass_not();
/*
SBUF=receive_buf[0]; 
Delay_52us_NOP();
SBUF=receive_buf[1];
Delay_52us_NOP(); 
SBUF=receive_buf[2];
Delay_52us_NOP(); 
SBUF=receive_buf[3]; 
Delay_52us_NOP();
SBUF=receive_buf[4]; 
Delay_52us_NOP();
SBUF=receive_buf[5];
Delay_52us_NOP(); 
*/
}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -