void delay_ns(unsigned int ns) { unsigned int i; for(i=0;i //------------------------------------------ // 读SPI数据 //------------------------------------------ unsigned char SPIReadByte(void) { unsigned char SPICount; clock out the data unsigned char SPIData; SPIData = 0; for (SPICount = 0; SPICount < 8; SPICount++) the data to be read { SPIData <<=1; data CLR_SPI_CK; //nop();//nop(); Raise the clock to clock the data out of the MAX7456 if(STU_SPI_MISO) { SPIData|=0x01; } SET_SPI_CK; // Drop the clock ready for the next bit } return (SPIData); } //------------------------------------------ // 写SPI数据 //------------------------------------------ void SPIWriteByte(unsigned char SPIData) { unsigned char SPICount; 39 // Counter used to // Prepare to clock in // Rotate the // //nop();//nop(); // and loop back // Finally return the read data // Counter used to clock out the data for (SPICount = 0; SPICount < 8; SPICount++) { if (SPIData & 0x80) { SET_SPI_MOSI; } else { CLR_SPI_MOSI; } nop();nop(); CLR_SPI_CK;nop();nop(); SET_SPI_CK;nop();nop(); 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; 40 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); 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); 41 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]:返回数据的位长度 ///////////////////////////////////////////////////////////////////// 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; } 42