学前班
最后登录1970-1-1
在线时间 小时
注册时间2016-2-22
|
硬件平台: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;
}
|
|