這是一個使用stm32使用mtr4048 讀取身份證UID的代碼,有點亂,但是肯定沒問題
單片機源程序如下:
- //頭文件調用
- #include "usually.h"
- #include "usart.h"
- #include "rtc.h"
- #include "stm32f10x_spi.h"
- //#include "define.h"
- #include "iso14443_4.h"
- #include "iso14443a.h"
- #include "mh523.h"
- #include "stdio.h"
- #include "mfrc522.h"
- //聲明變量
- extern struct Data_Time timer;
- void delay_t(int time)
- {
- volatile int d = time * 10;
- int i = 0;
- for(;i < time; i++)
- d = d+1;
- }
- //函數申明
- void Init_LED(void);
- void Init_NVIC(void);
- void Delay_Ms(uint16_t time);
- void Delay_Us(uint16_t time);
- #define SPI_FLASH_CS_HIGH() GPIO_SetBits(GPIOA, GPIO_Pin_4)
- #define SPI_FLASH_CS_LOW() GPIO_ResetBits(GPIOA, GPIO_Pin_4)
- void SPI_FLASH_Init(void)
- {
- SPI_InitTypeDef SPI_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
- /* SCK */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* MISO */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* MOS */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* CS */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- SPI_FLASH_CS_HIGH();
- SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
- SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
- SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
- SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStructure.SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, &SPI_InitStructure);
- SPI_Cmd(SPI1, ENABLE);
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- ** 函數名稱: main
- ** 功能
- 描述: 主函數入口
- ** 參數描述:無
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
- int main(void)
- {
- uint8_t t=0;
- int ret = 0;
- int fd;
- u8 status = 0;
- int block = 0;
- u8 preaddata[16];
- int j = 0, i = block - block % 4;
- u8 pkey[] = {0x20, 0x10, 0x03, 0x22, 0x00, 0x00};
- u8 psak;
- u8 tag_type[16] = {0};
- SystemInit(); //系統時鐘配置
- Init_NVIC(); //中斷向量表注冊函數
- //Init_Usart(); //串口引腳配置
- //Usart_Configure(115200); //串口配置 設置波特率為115200
- Init_LED(); //LED初始化
- SPI_FLASH_Init();
- #if 1
- //Init_RTC(); //內部RTC初始化
- //Time_Update(2013,6,26,0,0,0);
- // pcd_lpcd_start();
- //sleep(1);
- i = 0;
- //for(;;)
- //{
- // ret = ReadRawRC (0x0 + i++);
- // status = ret;
- //}
- PcdReset();
- //PcdAntennaOff();
- //PcdAntennaOn();
- __loop_mian:
- M500PcdConfigISOType('B');
- ///////////////////////////////////////
- //status = PcdRequest(0x52, tag_type);
- status = pcd_request_b(0x52, tag_type);
- if (status != 0)
- {
- delay_t(100);
- PcdAntennaOff();
- goto __loop_mian;
- }
- #if 1
- status = pcd_attri_b(0x52, tag_type);
- if (status != 0)
- {
- delay_t(100);
- goto __loop_mian;
- }
- status = get_idcard_num(0x52, tag_type);
- if (status != 0)
- {
- delay_t(100);
- goto __loop_mian;
- }
- goto __loop_mian;
- /////////////////////////////////
- status = PcdAnticoll(tag_type);
- if(status != 0)
- {
- delay_t(100);
- goto __loop_mian;
- }
- #if 1
- // printf("ID %02X%02X%02X%02X\n", tag_type[0], tag_type[1], tag_type[2], tag_type[3]);
- ////////////////////////////
- status = PcdSelect(tag_type);
- if(status != 0)
- {
- delay_t(100);
- goto __loop_mian;
- }
- // printf("psak %02X\n", psak);
- status = PcdAuthState(0x60, 7, pkey,tag_type );
- if(status != 0)
- {
-
- delay_t(100);
- goto __loop_mian;
- }
-
- PcdRead(4,preaddata);
- if(status != 0)
- {
-
- delay_t(100);
- goto __loop_mian;
- }
- LED1 = ~LED1;
- delay_t(10000);
- LED1 = ~LED1;
- #endif
- goto __loop_mian;
- #endif
- #endif
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- ** 函數名稱: LED_Init
- ** 功能描述: LED IO引腳配置
- ** 參數描述:無
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
- void Init_LED(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //定義一個GPIO結構體變量
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //使能各個端口時鐘,重要!!!
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ; //配置LED端口掛接到6、12、13端口
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //通用輸出推挽
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; //配置端口速度為50M
- GPIO_Init(GPIOA, &GPIO_InitStructure); //將端口GPIOD進行初始化配置
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- ** 函數名稱: NVIC_Configuration
- ** 功能描述: 系統中斷配置
- ** 參數描述:無
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
- void Init_NVIC(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef VECT_TAB_RAM //向量表基地址選擇
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); //將0x20000000地址作為向量表基地址(RAM)
- #else
- NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); //將0x08000000地址作為向量表基地址(FLASH)
- #endif
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //先占優先級1位,從優先級3位
- NVIC_InitStructure.NVIC_IRQChannel = RTC_IRQn; //RTC全局中斷
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占優先級1位,從優先級3位
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //先占優先級0位,從優先級4位
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能該通道中斷
- NVIC_Init(&NVIC_InitStructure); //根據NVIC_InitStruct中指定的參數初始化外設NVIC寄存器
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- ** 函數名稱: Delay_Ms_Ms
- ** 功能描述: 延時1MS (可通過仿真來判斷他的準確度)
- ** 參數描述:time (ms) 注意time<65535
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
- void Delay_Ms(uint16_t time) //延時函數
- {
- uint16_t i,j;
- for(i=0;i<time;i++)
- for(j=0;j<10260;j++);
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- ** 函數名稱: Delay_Ms_Us
- ** 功能描述: 延時1us (可通過仿真來判斷他的準確度)
- ** 參數描述:time (us) 注意time<65535
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
- void Delay_Us(uint16_t time) //延時函數
- {
- uint16_t i,j;
- for(i=0;i<time;i++)
- for(j=0;j<9;j++);
- }
- /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
- End:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D:-D
- :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
復制代碼
所有資料51hei提供下載:
測試程序:RTC萬年歷實驗(串口接收 波特率115200).7z
(209.35 KB, 下載次數: 7)
2019-9-30 17:03 上傳
點擊文件名下載附件
mtr4048 下載積分: 黑幣 -5
|