#define __MAIN_H
#include "main.h"
#undef __MAIN_H
#include #include #include #include #include "AT89S52.h" #include "RICReg.H" #include "m500a.H" #include "PICCCmdConst.h" #include "MfErrNo.h" #define uchar unsigned char #define uint unsigned int //#define DataPort P0 // 并行数据接口 //pin define mcu 引脚定义 sbit RC500RST = P1^0; //复位 rc500 sbit RC500CS = P1^1; //rc500片选 sbit CARD_LED = P1^2; //系统LED灯 sbit SPEAKER = P1^3; //扬声器 bit bcard; /////////////////////////////////////////////////////////////////////// // main /////////////////////////////////////////////////////////////////////// void main(void) { //波特率 uchar baud; InitSystem(); //系统初始化 delay_10ms(5); //延时 while (1) { delay_10ms(5); if (bCmd) //判断执行命令标志位 { bCmd = FALSE; //命令执行标志位清零 if(RevBuffer[0]==0x10) //RevBuffer[0]为0x10 { RevBuffer[2]=RevBuffer[1]; cmd_ok(); SPEAKER=1; CARD_LED=ON; delay_10ms(RevBuffer[2]); //闪灯并鸣笛 SPEAKER=0; CARD_LED=OFF; } else if(RevBuffer[0]==0x11) //RevBuffer[0]为0x11 { switch(RevBuffer[1]) { case 0: baud=BAUD_9600; break; case 1: baud=BAUD_14400; break; case 2: baud=BAUD_19200; break; case 3: baud=BAUD_28800; break; case 4: baud=BAUD_38400; break; case 5: baud=BAUD_57600; break; case 6: baud=BAUD_115200; break; default: baud=BAUD_19200; break; } cmd_ok(); delay_10ms(5); TR1 = 0; TH1 = baud; TL1 = TH1; delay_10ms(2); TR1 = TRUE; } else uart_process(); //调用串口处理函数 } } } /////////////////////////////////////////////////////////////////////// // 系统初始化 /////////////////////////////////////////////////////////////////////// void InitSystem(void) { RC500CS=0; //选通RC500 RC500RST=0; //复位 ET2 = FALSE; // 定时器2关中断 T2CON = 0x04; //TR2=1,TIMER2,auto reload PCON = 0x80; SCON = 0x70; // SMOD = 1; TMOD = 0x21; //TMOD = 0x22; TH1 = BAUD_19200; //定时器1产生波特率 TL1 = TH1; TR1 = TRUE; // HT1 ET1=FALSE; IT0 =TRUE; // Config ext0 as edge trigger for RC500 EX0 =TRUE; // Enable ext0 interrupt for RC500 EA = TRUE; // Enable all interrupts TR2=FALSE; //Close T2 IP=0x10; //interrupt priority ES = TRUE; bCmd=FALSE; // beep(1); // splash(1); MfConfig(); //配置RC500 } /////////////////////////////////////////////////////////////////////// // uart 中断服务程序 /////////////////////////////////////////////////////////////////////// void Uart_ISR(void) interrupt 4 using 1 { uchar len, i; uint j=0; if(RI) //如果是接收中断 { len=SBUF; //读取接收到的数据 RI=0; //清除接收中断标志 for(i=0;i while(!RI) { j++; if(j>1000) {break; //超时退出 } } if(j<1000) { RevBuffer[i]=SBUF; //读取接收到的数据 RI=0; j=0; } else {break; }//超时退出 } if(i==len) { REN=0; // 循环缓冲区环回 bCmd=TRUE; //命令标志位bCmd置位 } } else if(!RI && TI) //如果 RI=0,TI=1 { TI=0; //清除发送中断标志位 len=RevBuffer[0]; //发送RevBuffer长度个数据 for(i=0;i while(!TI) ; TI=0; } REN=1; } } /////////////////////////////////////////////////////////////////////// // uart process routine /////////////////////////////////////////////////////////////////////// void uart_process(void) { uchar cmd; uchar status; cmd = RevBuffer[0]; switch(cmd) { case 0x20: // Request ,? status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]); if(status!=0) { status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]); if(status!=0) { RevBuffer[0]=1; RevBuffer[1]=status; break; } } if(RevBuffer[2]==2) cardtype=mifare1S70; // Mifare1 S70 else if(RevBuffer[2]==4) cardtype=mifare1S50; // Mifare1 S50 else if(RevBuffer[2]==16) cardtype=mifarelight; // Mifare Light else cardtype=unknowncard; RevBuffer[0]=3; // RevBuffer[1]=status; break; case 0x21: //CardSnr status = M500PiccCascAnticoll(0,&RevBuffer[2]); if(status!=0) //ア? { RevBuffer[0]=1; RevBuffer[1]=status; break; } memcpy(CardSnr,&RevBuffer[2],4); RevBuffer[0]=5; RevBuffer[1]=status; break; case 0x22: // Select Card status=M500PiccCascSelect(CardSnr,&RevBuffer[2]); if(status!=MI_OK) { RevBuffer[0]=1; RevBuffer[1]=status; break; } RevBuffer[0]=3; RevBuffer[1]=status; break; case 0x23: // Key loading into the MF RC500's EEPROM status=M500PiccAuthE2(RevBuffer[1],CardSnr,RevBuffer[2],RevBuffer[3]); RevBuffer[0]=1; RevBuffer[1]=status; break; case 0x24: // Key loading into the MF RC500's EEPROM status=M500PcdLoadKeyE2(RevBuffer[1],RevBuffer[2],&RevBuffer[3]); RevBuffer[0]=1; RevBuffer[1]=status; break; case 0x25: // Read the mifare card status=M500PiccRead(RevBuffer[1],&RevBuffer[2]); if(status==0) { if(cardtype==mifare1S50||cardtype==mifare1S70) RevBuffer[0]=17; else if(cardtype==1) RevBuffer[0]=9; else RevBuffer[0]=16; } else
相关文章