#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 { RevBuffer[0]=1; } RevBuffer[1]=status; break; case 0x26: // Write the mifare card status=M500PiccWrite(RevBuffer[1],&RevBuffer[2]); RevBuffer[0]=1; RevBuffer[1]=status; break; case 0x27: status = M500PiccValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3],RevBuffer[7]); RevBuffer[0]=1; RevBuffer[1]=status; break; case 0x28: // status=M500PiccHalt(); RevBuffer[0]=1; // RevBuffer[1]=status; break; case 0x29: //power down SetBitMask(RegControl,0x10); // PowerDown = 1 cmd_ok(); break; case 0x30: // ClearBitMask(RegControl,0x10); // PowerDown = 0 cmd_ok(); break; default: // RevBuffer[0]=1; RevBuffer[1]=1; break; } } /////////////////////////////////////////////////////////////////////// //命令执行标志置1 /////////////////////////////////////////////////////////////////////// void cmd_ok(void) { RevBuffer[0]=1; RevBuffer[1]=0; } /////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////// // write address /////////////////////////////////////////////////////////////////////// void WriteRawIO(uchar Address,uchar value) { XBYTE[Address]=value; } /////////////////////////////////////////////////////////////////////// // read address /////////////////////////////////////////////////////////////////////// uchar ReadRawIO(uchar Address) { return XBYTE[Address]; } /////////////////////////////////////////////////////////////////////// // G E N E R I C W R I T E /////////////////////////////////////////////////////////////////////// void WriteIO(uchar Address, uchar value) { WriteRawIO(0x00,GetRegPage(Address)); WriteRawIO(Address,value); } /////////////////////////////////////////////////////////////////////// // G E N E R I C R E A D // (EEPROM) /////////////////////////////////////////////////////////////////////// uchar ReadIO(uchar Address) { WriteRawIO(0x00,GetRegPage(Address)); return ReadRawIO(Address); } /////////////////////////////////////////////////////////////////////// // set RC500 timeout /////////////////////////////////////////////////////////////////////// void M500PcdSetTmo(uchar tmoLength) { switch(tmoLength) { case 1: WriteIO(RegTimerClock,0x07); WriteIO(RegTimerReload,0x6a); break; case 2: WriteIO(RegTimerClock,0x07); WriteIO(RegTimerReload,0xa0); break; case 3: WriteIO(RegTimerClock,0x09); WriteIO(RegTimerReload,0xa0); break; case 4: WriteIO(RegTimerClock,0x09); WriteIO(RegTimerReload,0xff); break; case 5: WriteIO(RegTimerClock,0x0b); WriteIO(RegTimerReload,0xff); break; case 6: WriteIO(RegTimerClock,0x0d); WriteIO(RegTimerReload,0xff); break; case 7: WriteIO(RegTimerClock,0x0f); WriteIO(RegTimerReload,0xff); break; default: WriteIO(RegTimerClock,0x07); WriteIO(RegTimerReload,tmoLength); break; } } /////////////////////////////////////////////////////////////////////// // Request Command defined in ISO14443(Mifare) /////////////////////////////////////////////////////////////////////// char M500PcdCmd(uchar cmd, volatile uchar data *rcv, MfCmdInfo idata *info) { char idata status = MI_OK; char idata tmpStatus ; uchar idata lastBits; unsigned int idata timecnt = 0; uchar idata irqEn = 0x00; uchar idata waitFor = 0x00; uchar idata timerCtl = 0x00; WriteIO(RegInterruptEn,0x7F); //Clear Int WriteIO(RegInterruptRq,0x7F); WriteIO(RegCommand,PCD_IDLE); // (0x00) No action: cancel current command FlushFIFO(); MpIsrInfo = info; MpIsrOut = rcv; info->irqSource = 0x00; switch(cmd) { case PCD_IDLE: irqEn = 0x00; waitFor = 0x00; break; case PCD_WRITEE2: irqEn = 0x11; waitFor = 0x10; break; case PCD_READE2: irqEn = 0x07; waitFor = 0x04; break; case PCD_LOADCONFIG: case PCD_LOADKEYE2: case PCD_AUTHENT1: irqEn = 0x05; waitFor = 0x04; break; case PCD_CALCCRC: irqEn = 0x11; waitFor = 0x10; break; case PCD_AUTHENT2: irqEn = 0x04; waitFor = 0x04; break; case PCD_RECEIVE: info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4); irqEn = 0x06; waitFor = 0x04; break; case PCD_LOADKEY: irqEn = 0x05; waitFor = 0x04; break; case PCD_TRANSMIT: irqEn = 0x05; waitFor = 0x04; break; case PCD_TRANSCEIVE: info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4); irqEn = 0x3D; waitFor = 0x04; break; default: status = MI_UNKNOWN_COMMAND; } if (status == MI_OK) { irqEn |= 0x20; waitFor |= 0x20; timecnt=1000; WriteIO(RegInterruptEn,irqEn | 0x80); WriteIO(RegCommand,cmd); while (!(MpIsrInfo->irqSource & waitFor||!(timecnt--))); WriteIO(RegInterruptEn,0x7F); WriteIO(RegInterruptRq,0x7F); SetBitMask(RegControl,0x04); WriteIO(RegCommand,PCD_IDLE); if (!(MpIsrInfo->irqSource & waitFor)) { status = MI_ACCESSTIMEOUT; } else { status = MpIsrInfo->status; } if (status == MI_OK) { if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17)) { if (tmpStatus & 0x01) { info->collPos = ReadIO(RegCollPos); status = MI_COLLERR; } else { info->collPos = 0; if (tmpStatus & 0x02) { status = MI_PARITYERR; } } if (tmpStatus & 0x04) { status = MI_FRAMINGERR; } if (tmpStatus & 0x10) { FlushFIFO(); status = MI_OVFLERR; } if (tmpStatus & 0x08) { status = MI_CRCERR; } if (status == MI_OK) status = MI_NY_IMPLEMENTED; } if (cmd == PCD_TRANSCEIVE) { lastBits = ReadIO(RegSecondaryStatus) & 0x07; if (lastBits) info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits; else info->nBitsReceived += info->nBytesReceived * 8; } } else { info->collPos = 0x00; } } MpIsrInfo = 0; MpIsrOut = 0; return status; } /////////////////////////////////////////////////////////////////////// // set bit mask /////////////////////////////////////////////////////////////////////// char SetBitMask(uchar reg,uchar mask) { char idata tmp = 0x00; tmp = ReadIO(reg); WriteIO(reg,tmp | mask); // set bit mask return 0x00; } /////////////////////////////////////////////////////////////////////// //清除位掩码 /////////////////////////////////////////////////////////////////////// char ClearBitMask(uchar reg,uchar mask) { char idata tmp = 0x00;
上一篇:串行7段LED显示应用程序
下一篇:用51单片机控制的LED流水灯设计
- 热门资源推荐
- 热门放大器推荐
设计资源 培训 开发板 精华推荐
- DC1466B-C,演示板 LTC2636 八路 12 位 SPI VOUT DAC,具有 10ppm/摄氏度内部基准
- LTC3630AEDHC 12.5V 至 76V 输入至 12V 输出、500mA 降压转换器的典型应用电路
- DC1316A,用于 LT1913EDD 36V、3.5A、2.4MHz 降压型开关稳压器的演示板
- TAR5S50 点稳压器(低压降稳压器)的典型应用
- DC244A,LT1339、LT1373、LT1431 演示板,15A 时 32Vin 至 12Vout,100mA 时 -5Vout
- SiP2801低功耗电流模式控制器典型应用电路
- HDMI分屏器 TS3DV642A0RUAR
- 使用 Diodes Incorporated 的 PAM2701JER 的参考设计
- 使用 ON Semiconductor 的 KA7809E 的参考设计
- STEVAL-ILL061V1,基于 LED1642GW 和 STM32 高亮度 RGB LED 阵列驱动器的评估板,具有局部调光和诊断功能