方案:单片机处于低功耗模式,MS522处于软掉电模式。单片机用RTC定时(比如每隔1s)唤醒,单片机唤醒后唤醒MS522寻卡。寻到卡则做进一步处理,否则MS522继续进入软掉电模式,单片机进入低功耗模式。
if (1 == flag_rtc_wakeup) { flag_rtc_wakeup = 0; pcd_soft_powerup(); if (pcd_fast_detect() == true) { //todo } pcd_soft_powerdown(); }
void pcd_soft_powerdown(void) { write_rawrc(CommandReg, PCD_NOCMDCHANGE | (1 << 4)); } void pcd_soft_powerup(void) { uint8_t count = 0; write_rawrc(CommandReg, PCD_NOCMDCHANGE & (~(1 << 4))); do { // Wait for the PowerDown bit in CommandReg to be cleared (max 3x10us) delay_10us(1); } while ((read_rawrc(CommandReg) & (1 << 4)) && (++count) < 3); }
手册相关内容
//快速寻卡函数 bool pcd_fast_detect(void) { uint8_t valid_bits = 7; uint8_t command = PICC_REQIDL; uint8_t wait_irq = 0x30; // RxIRq and IdleIRq uint8_t n; uint16_t i; clear_bit_mask(CollReg, 0x80); // valuesAfterColl=1 => Bits received after collision are cleared. // write_rawrc(CommandReg, PCD_IDLE); // Stop any active command. write_rawrc(ComIrqReg, 0x7F); // Clear all seven interrupt request bits set_bit_mask(FIFOLevelReg, 0x80); // FlushBuffer = 1, FIFO initialization write_rawrc(FIFODataReg, command); // Write sendData to the FIFO write_rawrc(BitFramingReg, valid_bits); // Bit adjustments write_rawrc(CommandReg, PCD_TRANSCEIVE); // Execute the command set_bit_mask(BitFramingReg, 0x80); // StartSend=1, transmission of data starts i = 3; //根据时钟频率修改 while (1) { n = read_rawrc(ComIrqReg); // ComIrqReg[7..0] bits are: Set1 TxIRq RxIRq IdleIRq HiAlertIRq LoAlertIRq ErrIRq TimerIRq if (n & wait_irq) { // One of the interrupts that signal success has been set. //debug("t\r\n"); break; } if (n & 0x01) { // Timer interrupt - nothing received in time //debug("f\r\n"); return false; } if (--i == 0) { // The emergency break. If all other conditions fail we will eventually terminate on this one. Communication with the MFRC522 might be down. //debug("i\r\n"); return false; } } return true; }
void pcd_reset(void) { RC522_RST_ON; write_rawrc(CommandReg, PCD_RESETPHASE); uint8_t count = 0; do { // Wait for the PowerDown bit in CommandReg to be cleared (max 3x40us) delay_10us(4); } while ((read_rawrc(CommandReg) & (1 << 4)) && (++count) < 3); // TAuto=1; timer starts automatically at the end of the transmission in all communication modes at all speeds.10us write_rawrc(TModeReg, 0x80); write_rawrc(TPrescalerReg, 0x43); // 10μs // Reload timer with 0x1e = 30, ie 0.3ms before timeout. write_rawrc(TReloadRegH, 0x00); write_rawrc(TReloadRegL, 0x1e); write_rawrc(TxAutoReg, 0x40); write_rawrc(ModeReg, 0x3d); //和Mifare卡通讯,CRC初始值0x6363 pcd_antenna_on(); }
参考资料:
MS522手册:https://wenku.baidu.com/view/6f5bcabcbcd126fff6050b77
关于软掉电模式的讨论:https://github.com/miguelbalboa/rfid/issues/269
软掉电模式DEMO:https://github.com/akellai/rfid-music/blob/master/RFID_ProMini/RFID_ProMini.ino