野火电子论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 11313|回复: 1

STM32外接RFID模块读写S50卡的块数据失败

[复制链接]
发表于 2016-2-22 11:29:53 | 显示全部楼层 |阅读模式
硬件平台:STM32野火ISO-V3开发板+野火RFID模块
软件平台:XP系统+MDK5
背景描述:单独测试野火RFID模块提供的Demo,能够正确读取S50卡的ID号
问题描述:由于野火RFID模块Demo没有测试读写S50卡的块数据,故手动在main函数中增加了测试的代码,测试结果有时候成功有时候失败。
现场还原:(备注:以写块数据为例)
        ①、失败的代码如下:
        void IC_test ( void )
        {
                u8 UserDataBuffer[16] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
                u8 DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                printf ( "S50 Card Block Write Command\n" );
                while ( 1 )
                {
                        if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )                                    //寻卡
                                ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );                                                            //若失败继续寻卡
                       
                        if ( ucStatusReturn != MI_OK )                                                                                                             //防止连续打卡
                                bFind = false;
                       
                        if ( ( ucStatusReturn == MI_OK ) && ( bFind == false ) )
                        {
                                if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                                    //防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)
                                {
                                        bFind = true;
                                       
                                        printf ( "S50 Card ID is: " );   
                                        printf ( "%02X%02X%02X%02X\n", ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] );  //打印IC卡的UID(IC卡序列号)
                                       
                                        if( (ucStatusReturn = PcdSelect(ucArray_ID) ) == MI_OK)                                                                                                          //选定卡片
                                        {
                                                if( (ucStatusReturn = PcdAuthState(0x60, 1, DefaultKey, ucArray_ID) ) == MI_OK)                                                  //验证卡片块号为1的密码
                                                {
                                                        if( (ucStatusReturn = PcdWrite(1, UserDataBuffer) ) == MI_OK)                                                                  //写数据到M1卡块号为1的位置
                                                        {
                                                                printf ( "S50 Card Block Write Success\n" );
                                                                Beep_On();
                                                                delay_ms(30);
                                                                Beep_Off();
                                                                break;
                                                        }
                                                        else
                                                        {
                                                                printf ( "S50 Card Block Write Error\n" );
                                                                break;
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }


        ②、成功的代码如下:
        void IC_test ( void )
        {
                u8 UserDataBuffer[16] = {0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF,0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF};
                u8 DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
                u8 WriteSuccessFlag = 0;
                printf ( "S50 Card Block Write Command\n" );
                while ( 1 )
                {
                        if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )                                    //寻卡
                                ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );                                                            //若失败继续寻卡
                       
                        if ( ucStatusReturn != MI_OK )                                                                                                             //防止连续打卡
                                bFind = false;
                       
                        if ( ( ucStatusReturn == MI_OK ) && ( bFind == false ) )
                        {
                                if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                                    //防冲撞(当有多张卡进入读写器操作范围时,防冲突机制会从其中选择一张进行操作)
                                {
                                        bFind = true;
                                       
                                        printf ( "S50 Card ID is: " );   
                                        printf ( "%02X%02X%02X%02X\n", ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] );  //打印IC卡的UID(IC卡序列号)
                                       
                                        if( (ucStatusReturn = PcdSelect(ucArray_ID) ) == MI_OK)                                                                                                          //选定卡片
                                        {
                                                if( (ucStatusReturn = PcdAuthState(0x60, 1, DefaultKey, ucArray_ID) ) == MI_OK)                                                  //验证卡片块号为1的密码
                                                {
                                                        if( (ucStatusReturn = PcdWrite(1, UserDataBuffer) ) == MI_OK)                                                                  //写数据到M1卡块号为1的位置
                                                        {
                                                                printf ( "S50 Card Block Write Success\n" );
                                                                WriteSuccessFlag = 1;
                                                                break;
                                                        }
                                                        else
                                                        {
                                                                printf ( "S50 Card Block Write Error\n" );
                                                                break;
                                                        }
                                                }
                                        }
                                }
                        }
                }
               
                if(WriteSuccessFlag == 1)
                {
                        Beep_On();
                        delay_ms(30);
                        Beep_Off();
                }
        }

个人分析:针对以上两种情况的代码进行比较,从软件程序的角度分析是无区别的。
尝试如下:①更换RFID模块;②更换开发板;③更换S50卡;
实验结果:结果不变

请各路大侠提供协助,一起探究其缘由,万分感谢!


Ps:
main函数代码如下:
int main ( void )
{
        SysTick_Init ();   //滴答时钟初始化
        USART1_Config ();  //USART1 配置模式为 115200 8-N-1,中断接收
        Beep_Init();                                                                 //初始化蜂鸣器端口
        RC522_Init ();     //RC522模块所需外设的初始化配置
        printf ( "WF-RC522 Test\n" );
        PcdReset ();
        M500PcdConfigISOType ( 'A' );//设置工作方式
        while ( 1 )
        {
              IC_test ();//IC卡检测       
        }       
}

蜂鸣器的初始化函数如下:
#define BEEP PBout(0)        // BEEP,蜂鸣器接口       
void Beep_Init(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);        //使能GPIOB端口时钟
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;                                         //BEEP-->PB.0 端口配置
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                          //推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;                         //速度为50MHz
        GPIO_Init(GPIOB, &GPIO_InitStructure);                                        //根据参数初始化GPIOB
        Beep_Off();                                                                                //输出0,关闭蜂鸣器输出
}
void Beep_On(void)
{
        BEEP=1;  
}
void Beep_Off(void)
{
        BEEP=0;       
}



回复

使用道具 举报

发表于 2016-2-22 14:37:34 | 显示全部楼层
两个代码的主要区别就是把蜂鸣器的代码移出来了吧?
确实很奇怪。
这样的话就在关键地方加多点延时看看。
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

联系站长|手机版|野火电子官网|野火淘宝店铺|野火电子论坛 ( 粤ICP备14069197号 ) 大学生ARM嵌入式2群

GMT+8, 2024-5-19 20:37 , Processed in 0.039428 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表