利用串口發送卡號
單片機源程序如下:
- #include "sys.h"
- #include "delay.h"
- #include "usart.h"
- #include "led.h"
- #include "lcd.h"
- #include "spi2.h"
- #include "rc522.h"
- #include "usb_lib.h"
- #include "hw_config.h"
- #include "usb_pwr.h"
- unsigned char SN[4]; //卡號
- extern u8 IDA[10];
- extern u8 IDB[10];
-
- int main(void)
- {
- u8 cmdb[2];
- u8 status;
- // u16 t;
- // u16 len;
- // u16 times=0;
- u8 usbstatus=0;
- Stm32_Clock_Init(9); //系統時鐘設置
- uart_init(72,115200); //串口初始化為115200
- delay_init(72); //延時初始化
- OUTPUT_Init(); //輸出模塊初始化
- LED_Init(); //初始化與LED連接的硬件接口
- LCD_Init(); //初始化LCD
- SPI1_Init();
- SPI1_SpeedSet(6); //APB1的256分頻
- POINT_COLOR=RED; //設置字體為紅色
- LCD_ShowString(30,50,200,16,16,"WarShip STM32");
- LCD_ShowString(30,70,200,16,16,"USB Virtual USART TEST");
- LCD_ShowString(30,90,200,16,16,"ATOM@ALIENTEK");
- LCD_ShowString(30,110,200,16,16,"2015/1/28");
- LCD_ShowString(30,130,200,16,16,"USB Connecting...");//提示USB開始連接
- delay_ms(1800);
- USB_Port_Set(0); //USB先斷開
- delay_ms(700);
- USB_Port_Set(1); //USB再次連接
- Set_USBClock();
- USB_Interrupts_Config();
- USB_Init();
- CLR_NFC_RST;
- delay_ms(10);
- SET_NFC_RST;
- delay_ms(1);
- PcdConfig('A');
- delay_ms(1);
- while(1)
- {
- if(usbstatus!=bDeviceState)//USB連接狀態發生了改變.
- {
- usbstatus=bDeviceState;//記錄新的狀態
- if(usbstatus==CONFIGURED)
- {
- POINT_COLOR=BLUE;
- LCD_ShowString(30,130,200,16,16,"USB Connected ");//提示USB連接成功
- LED1=0;//DS1亮
- }else
- {
- POINT_COLOR=RED;
- LCD_ShowString(30,130,200,16,16,"USB disConnected ");//提示USB斷開
- LED1=1;//DS1滅
- }
- }
- #if 1
-
- PcdConfig('A');
- delay_ms(1);
- cmdb[1] = 0x52;
- delay_ms(1);
-
- status = ComReqA(cmdb);
- if(status == MI_OK)
- {
- LED0 = 0;
- usb_printf("ID:%02x %02x %02x %02x\r\n",IDA[0],IDA[1],IDA[2],IDA[3]);//發送卡號
- //printf("ID:%02x %02x %02x %02x\r\n",IDA[0],IDA[1],IDA[2],IDA[3]);//發送卡號
- }
- else LED0 = 1;
- delay_ms(10);
-
- #endif
- #if 1
-
- PcdConfig('B');
- delay_ms(1);
-
- cmdb[1] = 0x08;
- status = ComReqB(cmdb);
- if(status == MI_OK)
- {
- LED1 = 0;
- usb_printf("ID:%02x %02x %02x %02x %02x\r\n",IDB[0],IDB[1],IDB[2],IDB[3],IDB[4]);
- //printf("ID:%02x %02x %02x %02x %02x\r\n",IDB[0],IDB[1],IDB[2],IDB[3],IDB[4]);//發送卡號
- }
- #endif
- CLR_NFC_RST;
- delay_ms(250);
- LED1 = 1;
- LED0 = 1;
-
- #if 0
- if(USB_USART_RX_STA&0x8000)
- {
- len=USB_USART_RX_STA&0x3FFF;//得到此次接收到的數據長度
- printf("\r\n您發送的消息為:%d\r\n\r\n",len);
- for(t=0;t<len;t++)
- {
- USB_USART_SendData(USB_USART_RX_BUF[t]);//以字節方式,發送給USB
- }
- printf("\r\n\r\n");//插入換行
- USB_USART_RX_STA=0;
- }
- else
- {
- times++;
- if(times%5000==0)
- {
- usb_printf("\r\n戰艦STM32開發板USB虛擬串口實驗\r\n");
- usb_printf("正點原子@ALIENTEK\r\n\r\n");
- }
- if(times%200==0)usb_printf("請輸入數據,以回車鍵結束\r\n");
- if(times%30==0)LED0=!LED0;//閃爍LED,提示系統正在運行.
- delay_ms(10);
- }
- #endif
-
- }
- }
復制代碼
所有資料51hei提供下載:
RC522_AB.7z
(182.62 KB, 下載次數: 68)
2019-7-20 04:02 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|