|
riid的代碼 cv520的, 親自測試通過
0.png (49.87 KB, 下載次數(shù): 33)
下載附件
2018-9-12 04:40 上傳
單片機源程序如下:
- #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};
- //系統(tǒng)需要讀取卡片的全球唯一碼(卡片的第0塊)和會員編號數(shù)據(jù)(卡片的第1塊),讀卡密鑰類型為A型(0x60) , 密鑰數(shù)據(jù) 6 Byte(0x16,0x81,0x68,0x16,0x81,0x68) 。
- //******************************************************************************
- // PARAM: void
- // RETURN: void
- // COMMENT: 主程序
- //
- //******************************************************************************
- int main(void)
- {
- unsigned char cStatus;
-
- // 初始化系統(tǒng)
- InitializeSystem();
-
- // 初始化設(shè)備
- 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卡 測試流程
- // 認(rèn)證扇區(qū)
- // 讀取數(shù)據(jù)塊1
- // 寫入數(shù)據(jù)塊1 清空
- // 讀取數(shù)據(jù)塊1
- // 寫入數(shù)據(jù)塊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); // 設(shè)置射頻芯片為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; // 系統(tǒng)時鐘1分頻
- PeriodValue = SystemCoreClock/10000;
- break;
-
- case TIME_UNIT_1MS:
- PrescalerValue = 1; // 系統(tǒng)時鐘2分頻
- PeriodValue = SystemCoreClock/2000;
- break;
-
- case TIME_UNIT_200MS:
- PrescalerValue = 575; // 系統(tǒng)時鐘576分頻
- PeriodValue = SystemCoreClock/2880;
- break;
-
- default:
- break;
- }
- TIM_TimeBaseStructure.TIM_Period = PeriodValue;
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
CV520 SourceCode.7z
(196.13 KB, 下載次數(shù): 42)
2024-6-17 20:29 上傳
點擊文件名下載附件
rfid cv520代碼,讀寫非接觸ic卡 下載積分: 黑幣 -5
|
|