这是一个使用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)
|