上面是我的微信和QQ群,欢迎新朋友的加入。
目录
1.硬件平台
1.STM32F103+FM17550
2.卡片类型:身份证、TYPEA/B卡、银行卡
2.驱动实现
1.硬件SPI,配置如下
/* SPI1 init function */
static void MX_SPI1_Init(void)
{
/* SPI1 parameter configuration*/
hspi1.Instance = SPI1;
hspi1.Init.Mode = SPI_MODE_MASTER;
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
hspi1.Init.CLKPolarity = SPI_POLARITY_HIGH;
hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
hspi1.Init.NSS = SPI_NSS_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 10;
if (HAL_SPI_Init(&hspi1) != HAL_OK)
{
Error_Handler();
}
}
3.循环扫卡测试程序
/*********************************************
** 函数描述:循环寻卡
** 输入参数:无
** 返回值 :无
** 说明:
*********************************************/
void Find_Card(void)
{
volatile unsigned char statues;
unsigned int Rec_len; //卡片返回数据长度
statues=TypeA_CardActive(picc_atqa,picc_uid,picc_sak);
if(statues != TRUE)
{
FM175X_SoftReset();
Rec_len = 0;
Pcd_ConfigISOType(1); //设置TypeB
Set_Rf(3); //turn on radio
statues=TypeB_WUP(&Rec_len,Send_Buff+Data,PUPI);//寻卡 Standard send request command Standard mode
if(statues == TRUE)statues=TypeB_Select(PUPI,&Rec_len,Send_Buff+Data);
}
if(statues == TRUE)
{
HAL_Delay(10);
}
HAL_Delay(1000);
Set_Rf(0); //turn off radio
}
4.TYPEB射频卡扫描
/*************************************************
** 函数描述:测试TYPEB卡
** 输入参数:无
** 返回值 :无
** 说明:
*************************************************/
void Test_CardB(void)
{
uint8_t status;
unsigned int Rec_len; //卡片返回数据长度
HAL_Delay(1000);
FM175X_SoftReset();
Rec_len = 0;
Pcd_ConfigISOType(1); //设置TypeB
Set_Rf(3); //turn on radio
status=TypeB_WUP(&Rec_len,Send_Buff,PUPI);//寻卡 Standard send request command Standard mode
if(Rec_len == 0x60)
{
status=TypeB_Select(PUPI,&Rec_len,Send_Buff);
if(status == TRUE)
{
status=TypeB_GetUID(&Rec_len,Send_Buff);
if(status == TRUE)
{
junValue++;
}
}
}
}
5.TYPEA射频卡扫描
/***********************************************************
** 函数描述:测试TYPEA卡
** 输入参数:无
** 返回值 :无
** 说明:
***********************************************************/
void Test_CardA(void)
{
uint8_t status;
//激活卡(寻卡,防冲突,选卡)
status=TypeA_CardActive(picc_atqa,picc_uid,picc_sak);
if (status != TRUE)
{
//激活失败
return;
}
status=Mifare_Auth(0x00,0x01,DefualtKey,picc_uid); //card authentication
if (status != TRUE)
{
//认证失败
return;
}
//测试的时候避免反复写数据
if(junValue)
{
status=Mifare_Blockwrite(0x04,DefualtData); //block write function
if (status != TRUE)
{
//写数据失败
return;
}
junValue = 0;
}
status=Mifare_Blockread(0x04,Send_Buff); //bolck read function(读04块的数据)
if (status != TRUE)
{
//读数据失败
return;
}
}
6.身份证UID读取
//检测TYPEB 射频卡
FM175X_SoftReset();
Rec_len = 0;
Pcd_ConfigISOType(1); //设置TypeB
Set_Rf(3); //turn on radio
status=TypeB_WUP(&Rec_len,Send_Buff,PUPI);//寻卡 Standard send request command Standard mode
if(Rec_len == 0x60)
{
status=TypeB_Select(PUPI,&Rec_len,Send_Buff);
if(status == TRUE)
{
status=TypeB_GetUID(&Rec_len,Send_Buff);
if(status == TRUE)
{
for(uint8_t i=0;i<12;i++)
{
HID_SendHex(Send_Buff[i]);
}
HID_Enter();
}
}
}