欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MS522低功耗寻卡

程序员文章站 2022-12-28 17:13:12
方案:单片机处于低功耗模式,MS522处于软掉电模式。单片机用RTC定时(比如每隔1s)唤醒,单片机唤醒后唤醒MS522寻卡。寻到卡则做进一步处理,否则MS522继续进入软掉电模式,单片机进入低功耗模式。 手册相关内容 参考资料: MS522手册:https://wenku.baidu.com/vi ......

方案:单片机处于低功耗模式,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);
}

手册相关内容

MS522低功耗寻卡

MS522低功耗寻卡

MS522低功耗寻卡

//快速寻卡函数
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

关于软掉电模式的讨论:

软掉电模式demo:https://github.com/akellai/rfid-music/blob/master/rfid_promini/rfid_promini.ino