1、SL RC400程序设计#include #include #include #include main.h#include slrc400.h#include iso15693.hunsigned char xdata ini _at_ 0x7f00; / move base address to 0x7f00unsigned char xdata *GpBase = &ini; / redirect pointer to base address#define ReadRawIO(addr) (*(GpBase + addr)#define WriteRawIO(addr,value) *
2、(GpBase + addr) = value;extern struct TranSciveBufferunsigned char MfCommand; unsigned int MfLength; unsigned char MfData64; ;/复位并初始化RC400/注意:RC400上电后应延时500ms才能可靠初始化/char PcdReset() char status = MI_OK; char n = 0; unsigned int i = 2000; RC400_RST=0; DelayMs(50); RC400_RST=1; DelayMs(5); RC400_RST=0
3、; while (i!=0 & n!=PCD_RESETPHASE) n = ReadRawRC(RegCommand); i-; while (i!=0 & n=PCD_RESETPHASE) n = ReadRawRC(RegCommand); i-; if (i != 0) WriteRawRC(RegPage,0x80); n = 0x80; while ( (i!=0) & (n&0x80) ) n = ReadRawRC(RegCommand); i-; if (i=0 | (n&0xFF) status = MI_RESETERR ; else status = MI_RESET
4、ERR; if (status = MI_OK) WriteRawRC(RegPage,0x0); return status;/设置RC400的工作方式 /char PcdConfigISOType()ClearBitMask(RegControl,0x08);/ WriteRawRC(RegTxControl,0x58);/100%ASK WriteRawRC(RegCwConductance,0x3f);/输出功率和操作距离调整 WriteRawRC(RegModConductance,0x05); / must be measured for 15% Modulation Index
5、WriteRawRC(RegCoderControl,0x2f); /2e fast 2f / 52.97kHz, 1-256 coding WriteRawRC(RegModWidth,0x3F); /设置调制脉冲宽度: 9.44us WriteRawRC(RegModWidthSOF,0x3F); /设置SOF脉冲宽度: 9.44us/ WriteRawRC(RegTypeBFraming,0x00); WriteRawRC(RegRxControl1,0x8b);/控制接收:电压增益42db WriteRawRC(RegDecoderControl,0x34);/c14); WriteR
6、awRC(RegBitPhase,0xad);/0xcd);/0xad);/?? WriteRawRC(RegRxThreshold,0x68);/c68);/ WriteRawRC(RegBPSKDemControl,0); WriteRawRC(RegRxControl2,0x81);/c41);I时钟用作接收器时钟 WriteRawRC(RegClockQControl,0x00); WriteRawRC(RegRxWait,0x08); WriteRawRC(RegChannelRedundancy,0x2C); / Enable CRC TX/RX WriteRawRC(RegCRC
7、PresetLSB,0xFF); WriteRawRC(RegCRCPresetMSB,0xFF); WriteRawRC(RegTimeSlotPeriod,0x00); WriteRawRC(RegSIGOUTSelect,0x00);/?0X02 / enable SIGOUT = envelope/ WriteRawRC(RFU27,0x00); WriteRawRC(RegFIFOLevel,0x38); WriteRawRC(RegTimerClock,0x0B); WriteRawRC(RegTimerReload,0x00); WriteRawRC(RegTimerContro
8、l,0x02); / TStopRxEnd=0,TStopRxBeg=1,/ WriteRawRC(RFU2E,0x00);/ WriteRawRC(RFU2F,0x00); DelayMs(1); PcdAntennaOn(); return MI_OK;/读RC400寄存器位/input:Address=寄存器地址/ouput:读出的值/unsigned char ReadRawRC(unsigned char Address) static unsigned char c;c = ReadRawIO(Address); / read value at the specified / ad
9、dress return c; / 写RC400寄存器/input:Address=寄存器地址/ value=要写入的值/void WriteRawRC(unsigned char Address, unsigned char value) WriteRawIO(Address,value);/置RC400寄存器位/input:reg=寄存器地址/ mask=置位值/void SetBitMask(unsigned char reg,unsigned char mask) char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg,tmp | ma
10、sk); / set bit mask/清RC400寄存器位/input:reg=寄存器地址/ mask=清位值/void ClearBitMask(unsigned char reg,unsigned char mask) char tmp = 0x0; tmp = ReadRawRC(reg); WriteRawRC(reg, tmp & mask); / clear bit mask / 通过RC400和ISO15693卡通讯/input: pi-MfCommand = RC400命令字/ pi-MfLength = 发送的数据长度/ pi-MfData = 发送数据/output:st
11、atus = 错误字/ pi-MfLength = 接收的数据BIT长度/ pi-MfData = 接收数据/char ISO15693_Transceive(struct TranSciveBuffer *pi)bit recebyte = 0; char status = MI_COM_ERR; unsigned char n,waitFor,TimerReload; unsigned int i; switch (pi-MfCommand) case PCD_TRANSMIT: waitFor = 0x08;/0x10; break; default: waitFor = 0x28; r
12、ecebyte=1; break; /* switch (pi-MfData1) case ISO15693_STAY_QUIET: TimerReload = 0x04; / 2048/fc = 0x01 = 151 us break; case ISO15693_SELECT: case ISO15693_RESET_TO_READY: TimerReload = 0x0F; / 2048/fc = 0x01 = 151 us break; case ISO15693_LOCK_AFI: case ISO15693_LOCK_DSFID: case ISO15693_LOCK_BLOCK:
13、 case ISO15693_WRITE_SINGLE_BLOCK: case ISO15693_WRITE_MULTIPLE_BLOCKS: case ISO15693_WRITE_AFI: case ISO15693_WRITE_DSFID: TimerReload = 0x29; / 2048/fc = 0x01 = 151 us break; case ISO15693_READ_SINGLE_BLOCK: TimerReload = 0x17; / 2048/fc = 0x01 = 151 us break; case ISO15693_INVENTORY: TimerReload
14、= 0x1F; / 2048/fc = 0x01 = 151 us break; case ISO15693_GET_SYSTEM_INFO: TimerReload = 0x25; / 2048/fc = 0x01 = 151 us break; case ISO15693_GET_MULTIPLE_BLOCK_SECURITY: TimerReload = 0x40;/(0x04+(cmdcmdlen-1+0x01)*0x02 + 0x04 + 0x01; / 2048/fc = 0x01 = 151 us break; / (0x04 + cmdcmdlen - 1)*0x02 - Ti
15、me for all receivedBytes, 0x04 - Time bevore and after response, 0x01 additional case ISO15693_READ_MULTIPLE_BLOCKS: TimerReload = 0x40;/(0x04+0x04*(cmdcmdlen-1+0x01)*0x02 + 0x04 + 0x01; / 2048/fc = 0x01 = 151 us break; default: TimerReload = 0x86; / 2048/fc = 0x01 = 151 us break; WriteRawRC(RegPage
16、,0x00); WriteRawRC(RegFIFOLevel,0x1A); SetBitMask(RegChannelRedundancy, 0x04); WriteRawRC(RegTimerReload, TimerReload); WriteRawRC(RegTimerControl, 0x06);*/ SetBitMask(RegControl,0x01); WriteRawRC(RegCommand, 0x00); WriteRawRC(RegInterruptEn, 0x81); WriteRawRC(RegInterruptRq, 0x3F); WriteRawRC(RegIn
17、terruptEn, 0x38 | 0x80); for (i=0; iMfLength; i+) WriteRawRC(RegFIFOData,pi-MfDatai); _nop_();_nop_();_nop_();_nop_();_nop_(); WriteRawRC(RegCommand, PCD_TRANSCEIVE); / start to send command to label i = 0xa000; do n = ReadRawRC(RegInterruptRq); i-; while ( (i!=0) & !(n&waitFor) );/等待发送完毕 if (!receb
18、yte) if (n & 0x10) status = MI_OK; WriteRawRC(RegInterruptEn,0x10); WriteRawRC(RegTimerControl,0x00); WriteRawRC(RegCommand,PCD_IDLE); ClearBitMask(RegCoderControl, 0x80); return status; else if (i!=0) & (n&0x08) if ( !(ReadRawRC(RegErrorFlag) & 0x0C) ) n = ReadRawRC(RegFIFOLength); pi-MfLength = n*
19、8; for (i=0; iMfDatai = ReadRawRC(RegFIFOData); _nop_();_nop_();_nop_();_nop_();_nop_(); if (pi-MfData0 = 0)/没有错误标志 status = MI_OK; WriteRawRC(RegInterruptEn,0x10); WriteRawRC(RegTimerControl,0x00); WriteRawRC(RegCommand,PCD_IDLE); ClearBitMask(RegCoderControl, 0x80); return status; /开启天线 /每次启动或关闭天险
20、发射之间应至少有1ms的间隔/char PcdAntennaOn() unsigned char i; i = ReadRawRC(RegTxControl); if (i & 0x03) return MI_OK; else SetBitMask(RegTxControl, 0x03); return MI_OK; char ISO15693_Transceive1(struct TranSciveBuffer *pi)bit recebyte = 0; char status = MI_COM_ERR; unsigned char n,waitFor,TimerReload; unsign
21、ed int i; switch (pi-MfCommand) case PCD_TRANSMIT: waitFor = 0x10; break; default: waitFor = 0x28; recebyte=1; break; switch (pi-MfData1) case ISO15693_STAY_QUIET: TimerReload = 0x04; / 2048/fc = 0x01 = 151 us break; case ISO15693_SELECT: case ISO15693_RESET_TO_READY: TimerReload = 0x0F; / 2048/fc =
22、 0x01 = 151 us break; case ISO15693_LOCK_AFI: case ISO15693_LOCK_DSFID: case ISO15693_LOCK_BLOCK: case ISO15693_WRITE_SINGLE_BLOCK: case ISO15693_WRITE_MULTIPLE_BLOCKS: case ISO15693_WRITE_AFI: case ISO15693_WRITE_DSFID: TimerReload = 0x29; / 2048/fc = 0x01 = 151 us break; case ISO15693_READ_SINGLE_
23、BLOCK: TimerReload = 0x17; / 2048/fc = 0x01 = 151 us break; case ISO15693_INVENTORY: TimerReload = 0x1F; / 2048/fc = 0x01 = 151 us break; case ISO15693_GET_SYSTEM_INFO: TimerReload = 0x25; / 2048/fc = 0x01 = 151 us break; case ISO15693_GET_MULTIPLE_BLOCK_SECURITY: TimerReload = 0x40;/(0x04+(cmdcmdle
24、n-1+0x01)*0x02 + 0x04 + 0x01; / 2048/fc = 0x01 = 151 us break; / (0x04 + cmdcmdlen - 1)*0x02 - Time for all receivedBytes, 0x04 - Time bevore and after response, 0x01 additional case ISO15693_READ_MULTIPLE_BLOCKS: TimerReload = 0x40;/(0x04+0x04*(cmdcmdlen-1+0x01)*0x02 + 0x04 + 0x01; / 2048/fc = 0x01
25、 = 151 us break; default: TimerReload = 0x86; / 2048/fc = 0x01 = 151 us break; WriteRawRC(RegPage,0x00); WriteRawRC(RegFIFOLevel,0x1A); SetBitMask(RegChannelRedundancy, 0x04); WriteRawRC(RegTimerReload, TimerReload); WriteRawRC(RegTimerControl, 0x06); SetBitMask(RegControl,0x01); WriteRawRC(RegComma
26、nd, 0x00); WriteRawRC(RegInterruptEn, 0x81); WriteRawRC(RegInterruptRq, 0x3F); WriteRawRC(RegInterruptEn, 0x38 | 0x80); for (i=0; iMfLength; i+) WriteRawRC(RegFIFOData,pi-MfDatai); _nop_();_nop_();_nop_();_nop_();_nop_(); WriteRawRC(RegCommand, PCD_TRANSCEIVE); / start to send command to label i = 0
27、xa000; do n = ReadRawRC(RegInterruptRq); i-; while ( (i!=0) & !(n&waitFor) );/等待发送完毕 if (!recebyte) if (n & 0x10) status = MI_OK; WriteRawRC(RegInterruptEn,0x10); WriteRawRC(RegTimerControl,0x00); WriteRawRC(RegCommand,PCD_IDLE); ClearBitMask(RegCoderControl, 0x80); return status; else if (i!=0) & (
28、n&0x08) if ( !(ReadRawRC(RegErrorFlag) & 0x0C) ) n = ReadRawRC(RegFIFOLength); pi-MfLength = n*8; for (i=0; iMfDatai = ReadRawRC(RegFIFOData); _nop_();_nop_();_nop_();_nop_();_nop_(); /if (pi-MfData0 = 0)/没有错误标志 status = MI_OK; WriteRawRC(RegInterruptEn,0x10); WriteRawRC(RegTimerControl,0x00); WriteRawRC(RegCommand,PCD_IDLE); ClearBitMask(RegCoderControl, 0x80); return status;