#include "stm32f10x.h"
#include "bsp_SysTick.h"
#include "usart1.h"
#include "rc522_config.h"
#include "rc522_function.h"
#include <stdbool.h>
#include "can.h"
#include "led.h"
#include "stdio.h"
u8 ucArray_ID [ 4 ];                                                   //先后存放IC卡的类型和UID(IC卡序列号)
char cStr [ 30 ]; 
void IC_test ( void )
{                                                                                       
	u8 ucStatusReturn;                                                   //返回状态                                       
	
  while ( 1 )
  { 
		if ( ( ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID ) ) != MI_OK )                                    //寻卡
			ucStatusReturn = PcdRequest ( PICC_REQALL, ucArray_ID );		                                                 //若失败再次寻卡

		if ( ucStatusReturn == MI_OK  )
		{
			if ( PcdAnticoll ( ucArray_ID ) == MI_OK )                                                                   //防冲撞（当有多张卡进入读写器操作范围时，防冲突机制会从其中选择一张进行操作）
			{
				sprintf ( cStr, "The Card ID is: %02X%02X%02X%02X", ucArray_ID [ 0 ], ucArray_ID [ 1 ], ucArray_ID [ 2 ], ucArray_ID [ 3 ] );				
				USART1_printf (USART1, "%s\r\n",cStr ); 
			}
			
		}
		
  }
	
}


/**
  * @brief  主函数
  * @param  无
  * @retval 无
  */
int main ( void )
{
	LED_GPIO_Config();
	SysTick_Init ();     //滴答时钟初始化
	USART1_Config();     //USART1 配置模式为 115200 8-N-1
	RC522_Init ();     //RC522模块所需外设的初始化配置
	CAN_Mode_Init(CAN_SJW_2tq,CAN_BS2_8tq,CAN_BS1_8tq,4,CAN_Mode_Normal);//CAN初始化普通模式,波特率500Kbps    	
	USART1_printf(USART1, "\r\n Welcome!!!\r\n");
	USART1_printf(USART1, "Please put the IC card on equipment ...\r\n" );		
	PcdReset();
	M500PcdConfigISOType ( 'A' );//设置工作方式	
  while ( 1 )
  {
    IC_test ();//IC卡检测	
  }
		
}


/****************************END OF FILE**********************/

