riid的代码 cv520的, 亲自测试通过
单片机源程序如下:
- #include "main.h"
- #include "stm32f10x_Init.h"
- #include "PCD.h"
- #include "string.h"
- #include "ISO14443.h"
- #include "History.h"
- #include <stdio.h>
- //------------------------------------------------------------------------------
- // Functions Define
- //------------------------------------------------------------------------------
- tpd_Card g_sCard; // 射频卡
- unsigned char g_cBuffer[256]; // 通信缓存
- unsigned short g_iLength;
- unsigned char g_cATS[32];
- unsigned char g_cATSLen;
- const unsigned char g_cM1KEY[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- //系统需要读取卡片的全球唯一码(卡片的第0块)和会员编号数据(卡片的第1块),读卡密钥类型为A型(0x60) , 密钥数据 6 Byte(0x16,0x81,0x68,0x16,0x81,0x68) 。
- //******************************************************************************
- // PARAM: void
- // RETURN: void
- // COMMENT: 主程序
- //
- //******************************************************************************
- int main(void)
- {
- unsigned char cStatus;
-
- // 初始化系统
- InitializeSystem();
-
- // 初始化设备
- InitDevice();
-
- do
- {
- PiccReset();
-
- // ISO14443A寻卡 判断卡类型
- // 选卡,防碰撞,
- cStatus = PiccRequest(PICC_REQALL, g_sCard.cATQ);
-
- if (cStatus == ERR_SUCCESS)
- {
- cStatus = PiccAnticollSelect(&g_sCard.cLen, g_sCard.cUID, &g_sCard.cSAK);
-
- cStatus = PiccHaltA(); // 正常返回 00
-
- cStatus = PiccRequest(PICC_REQIDL, g_sCard.cATQ); // 正常返回 FF 无卡
-
- cStatus = PiccRequest(PICC_REQALL, g_sCard.cATQ); // 正常返回 00
- if (cStatus == ERR_SUCCESS)
- {
- cStatus = PiccAnticollSelect(&g_sCard.cLen, g_sCard.cUID, &g_sCard.cSAK);
- }
-
- LED_GRE(ON);
- }
- else
- {
- LED_GRE(OFF);
-
- }
-
- if (cStatus == ERR_SUCCESS)
- {
- printf("card ok !\n");
- // Mifare卡 测试流程
- // 认证扇区
- // 读取数据块1
- // 写入数据块1 清空
- // 读取数据块1
- // 写入数据块1 写值
-
- cStatus = PiccAuthState(3, 0x60, 0x00, (unsigned char *)g_cM1KEY);
- if (cStatus == ERR_SUCCESS)
- {
- printf("auth ok !\n");
- }
- if (cStatus == ERR_SUCCESS)
- {
- cStatus = PiccRead(1, g_cBuffer);
- }
-
- if (cStatus == ERR_SUCCESS)
- {
- printf("read: ");
- for (int i = 0; i < 16; i++) {
- printf("%02X ", g_cBuffer[i]&0xFF);
- }
- printf("\n");
- }
- if (cStatus == ERR_SUCCESS)
- {
- memset(g_cBuffer, 0xA5, 16);
- cStatus = PiccWrite(1, g_cBuffer);
- }
-
- if (cStatus == ERR_SUCCESS)
- {
- cStatus = PiccRead(1, g_cBuffer);
- }
-
- if (cStatus == ERR_SUCCESS)
- {
- printf("read: ");
- for (int i = 0; i < 16; i++) {
- printf("%02X ", g_cBuffer[i]&0xFF);
- }
- printf("\n");
- }
- if (cStatus == ERR_SUCCESS)
- {
- memset(g_cBuffer, 0x00, 16);
- cStatus = PiccWrite(1, g_cBuffer);
- }
-
- printf("\n");
- }
- else
- {
- LED_YEL(OFF);
- LED_BLU(OFF);
- }
- } while (1);
- }
- //******************************************************************************
- // IN: void
- // RETURN: void
- // Comment:
- //
- //******************************************************************************
- void InitDevice(void)
- {
- unsigned char cStatus;
- LED_RED(ON);
- LED_GRE(ON);
- LED_YEL(ON);
- LED_BLU(ON);
- Delay100uS(2000); // 延时200ms
- LED_RED(OFF);
- LED_GRE(OFF);
- LED_YEL(OFF);
- LED_BLU(OFF);
- BEEP(OFF);
-
- // LED_RED: 射频正常启动亮
- // LED_GRE: 有卡指示灯,单独亮为M1卡
- // LED_YEL: Type A CPU Card
- // LED_BLU: Type B CPU Card
-
- RC_SEL0(OFF);
- RC_SEL1(ON); // 设置射频芯片为SPI模式
-
- cStatus = PcdReset();
- if (cStatus == ERR_SUCCESS)
- {
- PcdConfig(PCD_TYPE_14443A);
- LED_RED(ON); // 射频初始化成功,RED LED 点亮。
- }
- return;
- }
- //******************************************************************************
- // IN: unsigned int iTimes
- // RETURN: void
- // Comment: 使用Timer4做等待延时,单位时间100us
- //
- //******************************************************************************
- void Delay100uS(unsigned int iTimes)
- {
- Timer4_Init(TIME_UNIT_100US);
-
- Timer4_Wait(iTimes);
-
- Timer4_Stop();
- return;
- }
- void Timer4_Init(unsigned char cMode)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- uint16_t PrescalerValue;
- uint16_t PeriodValue;
-
- switch (cMode)
- {
- case TIME_UNIT_100US:
- PrescalerValue = 0; // 系统时钟1分频
- PeriodValue = SystemCoreClock/10000;
- break;
-
- case TIME_UNIT_1MS:
- PrescalerValue = 1; // 系统时钟2分频
- PeriodValue = SystemCoreClock/2000;
- break;
-
- case TIME_UNIT_200MS:
- PrescalerValue = 575; // 系统时钟576分频
- PeriodValue = SystemCoreClock/2880;
- break;
-
- default:
- break;
- }
- TIM_TimeBaseStructure.TIM_Period = PeriodValue;
- ……………………
- …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码
所有资料51hei提供下载:
CV520 SourceCode.7z
(196.13 KB, 下载次数: 42)
|