陕西理工学院毕业设计
SPIData <<= 1; } }
///////////////////////////////////////////////////////////////////// //功 能:读RC632寄存器
//参数说明:Address[IN]:寄存器地址 //返 回:读出的值
///////////////////////////////////////////////////////////////////// unsigned char ReadRawRC(unsigned char Address) {
unsigned char ucAddr; unsigned char ucResult=0; CLR_SPI_CS;
ucAddr = ((Address<<1)&0x7E)|0x80; SPIWriteByte(ucAddr); ucResult=SPIReadByte(); SET_SPI_CS;
return ucResult; }
///////////////////////////////////////////////////////////////////// //功 能:写RC632寄存器
//参数说明:Address[IN]:寄存器地址 // value[IN]:写入的值
///////////////////////////////////////////////////////////////////// void WriteRawRC(unsigned char Address, unsigned char value) {
unsigned char ucAddr;
CLR_SPI_CS;
ucAddr = ((Address<<1)&0x7E); SPIWriteByte(ucAddr); SPIWriteByte(value); SET_SPI_CS; }
///////////////////////////////////////////////////////////////////// //功 能:清RC522寄存器位 //参数说明:reg[IN]:寄存器地址 // mask[IN]:清位值
///////////////////////////////////////////////////////////////////// void ClearBitMask(unsigned char reg,unsigned char mask) {
char tmp = 0x00;
tmp = ReadRawRC(reg);
第 40 页 共 61 页
陕西理工学院毕业设计
WriteRawRC(reg, tmp & ~mask); // clear bit mask }
///////////////////////////////////////////////////////////////////// //功 能:置RC522寄存器位 //参数说明:reg[IN]:寄存器地址 // mask[IN]:置位值
///////////////////////////////////////////////////////////////////// void SetBitMask(unsigned char reg,unsigned char mask) {
char tmp = 0x00;
tmp = ReadRawRC(reg);
WriteRawRC(reg,tmp | mask); // set bit mask }
///////////////////////////////////////////////////////////////////// //用MF522计算CRC16函数
/////////////////////////////////////////////////////////////////////
void CalulateCRC(unsigned char *pIndata,unsigned char len,unsigned char *pOutData) {
unsigned char i,n;
ClearBitMask(DivIrqReg,0x04); WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; i { WriteRawRC(FIFODataReg, *(pIndata+i)); } WriteRawRC(CommandReg, PCD_CALCCRC); i = 0xFF; do { n = ReadRawRC(DivIrqReg); i--; } while ((i!=0) && !(n&0x04)); pOutData[0] = ReadRawRC(CRCResultRegL); pOutData[1] = ReadRawRC(CRCResultRegM); } ///////////////////////////////////////////////////////////////////// //功 能:通过RC522和ISO14443卡通讯 //参数说明:Command[IN]:RC522命令字 // pInData[IN]:通过RC522发送到卡片的数据 // InLenByte[IN]:发送数据的字节长度 // pOutData[OUT]:接收到的卡片返回数据 // *pOutLenBit[OUT]:返回数据的位长度 ///////////////////////////////////////////////////////////////////// 第 41 页 共 61 页 陕西理工学院毕业设计 char PcdComMF522(unsigned char Command, unsigned char *pInData, unsigned char InLenByte, unsigned char *pOutData, unsigned int *pOutLenBit) { char status = MI_ERR; unsigned char irqEn = 0x00; unsigned char waitFor = 0x00; unsigned char lastBits; unsigned char n; unsigned int i; switch (Command) { case PCD_AUTHENT: irqEn = 0x12; waitFor = 0x10; break; case PCD_TRANSCEIVE: irqEn = 0x77; waitFor = 0x30; break; default: break; } WriteRawRC(ComIEnReg,irqEn|0x80); ClearBitMask(ComIrqReg,0x80); WriteRawRC(CommandReg,PCD_IDLE); SetBitMask(FIFOLevelReg,0x80); for (i=0; i WriteRawRC(FIFODataReg, pInData[i]); } WriteRawRC(CommandReg, Command); if (Command == PCD_TRANSCEIVE) { SetBitMask(BitFramingReg,0x80); } //i = 600;//根据时钟频率调整,操作M1卡最大等待时间25ms i = 2000; do 第 42 页 共 61 页 陕西理工学院毕业设计 { n = ReadRawRC(ComIrqReg); i--; } while ((i!=0) && !(n&0x01) && !(n&waitFor)); ClearBitMask(BitFramingReg,0x80); if (i!=0) { if(!(ReadRawRC(ErrorReg)&0x1B)) { status = MI_OK; if (n & irqEn & 0x01) { status = MI_NOTAGERR; } if (Command == PCD_TRANSCEIVE) { n = ReadRawRC(FIFOLevelReg); lastBits = ReadRawRC(ControlReg) & 0x07; if (lastBits) { *pOutLenBit = (n-1)*8 + lastBits; } else { *pOutLenBit = n*8; } if (n == 0) { n = 1; } if (n > MAXRLEN) { n = MAXRLEN; } for (i=0; i pOutData[i] = ReadRawRC(FIFODataReg); } } } else { status = MI_ERR; } 第 43 页 共 61 页