?? t2.c
字號:
#include <AT89X52.h>
#include <INTRINS.H>
#include <string.h>
unsigned char INBUF_LEN; //數據長度
unsigned char inbuf1[32];
unsigned char count3;
unsigned int i;
unsigned char c0[9]={0x00,0x06,0x00,0x00,0x05,0x03,0x01,0x03,0x01}; //初始相對卡號
unsigned char temp[9]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char pass[6]={0xFF,0xFF,0xFF,0xFF,0xFF,0xFF};
bit flag=0; //是否進行下一步的標志
//*********************************************************************************//
unsigned char ledcode[] ={0xee, //0
0x28, //1
0xcd, //2
0x6d, //3
0x2b, //4
0x67, //5
0xe7, //6
0x2c, //7
0xef, //8
0x6f, //9
0xaf, //A
0xe3, //B
0xc6, //C
0xe9, //D
0xc7, //E
0x87}; //F
//數碼管字符表
//*********************************************************************************//
void delay(int j) // 延時
{
int a;
while (--j){a=1000;while(--a){;}}
}
/***********************************************************************************/
void init_com(void) //初始化串口
{
SCON=0x50;
TMOD=0x20;//t0 timer
TH1=0xfd;//9600bps
TR1=1;
ET1=0;
ES=1;
EA=1;
IT0 =0;
count3=0; //串口計數清零
INBUF_LEN=0x21; //數據長度設為最大
}
//向串口發送一個字符串,strlen為該字符串長度,連續發送
void send_string_com(unsigned char strlen)
{
unsigned int t=0;
RI=0;
EA=0;
do
{
SBUF=inbuf1[t];
while(TI==0);
TI=0;
t++;
} while(t < strlen);
EA=1;
}
//串口接收中斷函數 ,連續接收
void serial () interrupt 4 using 3
{
unsigned int k=0;
EA=0;
if(RI)
{
while(inbuf1[count3]!=0x0D)
{
RI = 0;
if (inbuf1[count3]==0x3C )
{count3=0;inbuf1[count3]=0x3C;count3++;}
else count3++;
inbuf1[count3]=SBUF;
while (RI==0&&k<6000){k++;}
}
}
EA=1;
//INBUF_LEN=count3+1;
count3=0;
}
/***************************************************************/
unsigned char BCC(unsigned char *str,unsigned int strlen) //數據校驗
{
unsigned char bcc1=0x00;
i=0;
while(i<strlen)
{
bcc1^=str[i];
i++;
}
return bcc1;
}
/***************************************************************/
void searchcard() //這一步能正常通過了!
{
flag=0;
inbuf1[0]=0x3C; //下傳命令:3C 04 01 70 00(尋卡模式00(IDLE)或01(ALL)) 00 BCC(49) 0D
inbuf1[1]=0x04;
inbuf1[2]=0x01;
inbuf1[3]=0x70;
inbuf1[4]=0x01;
inbuf1[5]=0x00;
inbuf1[6]=BCC(inbuf1,6); //校驗
inbuf1[7]=0x0D;
INBUF_LEN=8; count3=0;
send_string_com(INBUF_LEN); //向模塊發送命令
delay(100); //等待模塊回傳信息
// send_string_com(INBUF_LEN);
if ( inbuf1[1]==0x05)
{
temp[0]=inbuf1[2]; //獲取相對卡號
temp[1]=inbuf1[3];
temp[2]=inbuf1[4];
temp[3]=inbuf1[5];
delay(50);
flag=1; //尋卡成功,flag置位
} else
flag=0;
count3=0;
return;
}
/**************************************************************/
void passcheck() //這一步好像有問題!不知道為什么!可能是延時太少么?
{
unsigned int i;
flag=0;
inbuf1[0]=0x3C; // 下傳命令:3C 0E 01 6C 六字節密碼 認證模式(00-A/01-B) 絕對塊號 四字節卡號 校驗位 0D
inbuf1[1]=0x0E; //eg:3C 0E 01 6C ff ff ff ff ff ff 00 02 74 09 49 C4 AD 0D
inbuf1[2]=0x01;
inbuf1[3]=0x6C;
for (i=0;i<6;i++){inbuf1[i+4]=pass[i];}
inbuf1[10]=0x00; //認證模式(00-A/01-B) 使用A模式 只讀不寫
inbuf1[11]=0x01; //讀塊號1
for (i=0;i<4;i++) {inbuf1[i+12]=temp[i];}
inbuf1[16]=BCC(inbuf1,16);
inbuf1[17]=0x0D;
INBUF_LEN=18; count3=0;
send_string_com(INBUF_LEN); //向模塊發送命令
delay(100); //等待模塊回傳信息
// send_string_com(INBUF_LEN); //測試點
if (inbuf1[1]==0x02) //這里只是根據返回命令的特征進行簡單驗證,為了程序更加健壯,應該要進行bcc校驗
{
delay(100);
flag=1; //密碼驗證成功,flag置位
}
return;
}
/**************************************************************/
void readcard()
{
flag=0;
inbuf1[0]=0x3C; // 下傳命令:3C 04 01 66 00(絕對塊號) 00 BCC 0D
inbuf1[1]=0x04;
inbuf1[2]=0x01;
inbuf1[3]=0x66;
inbuf1[4]=0x01; //絕對塊號
inbuf1[5]=0x00;
inbuf1[6]=BCC(inbuf1,6);
inbuf1[7]=0x0D;
INBUF_LEN=8;
count3=0;
send_string_com(INBUF_LEN); //向模塊發送命令
delay(100); //等待模塊回傳信息
if (inbuf1[1]==0x12) //這里只是根據返回命令的特征進行簡單驗證,為了程序更加健壯,應該要進行bcc校驗
{
for (i=0;i<10;i++)
{temp[i]=inbuf1[i+3];
inbuf1[i]=temp[i];}
delay(20);
flag=1; //尋卡成功,flag置位
}
// INBUF_LEN=0x08; count3=0;
// send_string_com(INBUF_LEN);
delay(20);
return;
}
/**************************************************************/
void cardcheck() //驗證相對卡號
{
flag=1;
for(i=0;i<8;i++)
{if (temp[i]!=c0[i])
flag=0;}
// if (temp[8]>=c0[8])
// c0[8]=temp[8]; //卡號的最后一個字節是識別字節,用于用戶換自己的卡,大數有效
// else flag=0;
}
/**************************************************************/
void closecard() // 關閉卡片:3C 04 01 68 00 00 51 0D
{
inbuf1[0]=0x3C;
inbuf1[1]=0x04;
inbuf1[2]=0x01;
inbuf1[3]=0x68;
inbuf1[4]=0x00;
inbuf1[5]=0x00;
inbuf1[6]=0x51;
inbuf1[7]=0x0D;
INBUF_LEN=0x08;
count3=0;
send_string_com(INBUF_LEN);
}
/**************************************************************/
void main(){
unsigned int state;
init_com(); //初始化串口和中斷
state=1;
P1_7=1;
P0=ledcode[0];
delay(50);
while(1)
{
// P1_2=0; P0=ledcode[0];delay(50);
switch(state){
case 1:
P0=ledcode[state];delay(50);
searchcard();
if (flag) state++;else state=6;break;
//尋卡
case 2:
P0=ledcode[state];delay(50);
passcheck();
if (flag) state++;else state=6;break;
//如果尋卡成功,獲取絕對卡號,進行密碼驗證
case 3:
P0=ledcode[state];delay(50);
readcard();
if (flag) state++;else state=6;break;
//如果密碼驗證成功,讀卡獲取,獲取相對卡號
case 4: P0=ledcode[state];delay(50);
cardcheck();
if (flag) state++;else state=6;break;
// 進行驗證
case 5:
P0=ledcode[state];delay(500);
P1_7=0;delay(1000);P1_7=1;state++;break;
// p1.7指示電動門鎖執行開門動作 ,
case 6: P0=ledcode[state];delay(50);
closecard();state=1;break;
// 模擬時接led顯示 及接在外部中斷上,用以模擬正常開門時門磁動作
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -