實(shí)驗(yàn)?zāi)康?/strong> 1. 最基礎(chǔ)實(shí)現(xiàn)刷卡后顯示沖值/扣款說明 2. 實(shí)現(xiàn)芯片內(nèi)部寄存器的讀寫,實(shí)現(xiàn)可記憶性。 設(shè)計(jì)思路
根據(jù)M1射頻卡與讀寫器的通訊過程,程序設(shè)計(jì)基本流程如下: 主函數(shù)架構(gòu): - void main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // 關(guān)看門狗
- int i = 0;
- unsigned char status;
- BoardConfig(0xf0);
- Init_Port();
- InitUART();
- Port_init(); //系統(tǒng)初始化,設(shè)置IO口屬性
- delay_ms(100); //延時100ms
- LCD_init(); //液晶參數(shù)初始化設(shè)置
- LCD_clear(); //清屏
- LCD_Desk1();
- _EINT();
- PcdReset(); //復(fù)位RC522
- PcdAntennaOn(); //開啟天線發(fā)射
- while (1)
- {
- status = PcdRequest(PICC_REQIDL, Temp); ////尋卡,輸出為卡類型
- if (status == MI_OK)
- status = PcdAnticoll(UD); //防沖撞處理,輸出卡片序列號,4字節(jié)
- if (status == MI_OK)
- status = PcdSelect(UD); //選擇卡片,輸入卡片序列號,4字節(jié)
- if (status == MI_OK)
- status = PcdAuthState(PICC_AUTHENT1A, 1, Password_Buffer, UD); //在進(jìn)行讀寫操作之前需要先進(jìn)行認(rèn)證
- if (status == MI_OK)
- status == incharg();//讀
- if (status == MI_OK)
- {
- status = PcdWrite(1,money_ok);//充錢
- memset(money_ok,0,sizeof(money_ok));
- break;
- }
- }
- LCD_clear(); //清屏
- LCD_Desk3();
- led();
- }
復(fù)制代碼其中 status == incharg();//讀 incharg 函數(shù) 這一部分是自己編寫用于讀寫存儲器內(nèi)容,并且進(jìn)行數(shù)據(jù)類型轉(zhuǎn)換功能的函數(shù)。 - char incharg(void)
- {
- unsigned char g_ucTempbuf[20];
- unsigned char num;
- unsigned char temp1[10];
- unsigned char temp2[5];
- unsigned char status2;
- long int sum = 0; //充值后整數(shù)部分的額度
- long int re;
- temp1[0] = '\0';
- temp2[0] = '\0';
- memset(money, 0, sizeof(money)); //清空錢包臨時數(shù)組
- status2 = PcdRead(1, g_ucTempbuf);//讀塊2部分
- if (status2 == MI_OK)
- {
- for (num = 0; num < 2; num++)
- {
- money[num] = g_ucTempbuf[1 - num];//存儲芯片寄存器讀出來的內(nèi)容,并且調(diào)換高低位(因?yàn)榇鎯Φ臅r候,按照低位在前的順序)
- }
- itoa(money[0], temp1, 16); //十六進(jìn)制數(shù)值轉(zhuǎn)十六進(jìn)制字符串(字符串無法進(jìn)行計(jì)算)
- itoa(money[1], temp2, 16); //十六進(jìn)制數(shù)值轉(zhuǎn)十六進(jìn)制字符串(字符串無法進(jìn)行計(jì)算)
- strcat(temp1, temp2); //將兩個char類型連接。輸出temp1=temp1temp2
- inteDec = str_dec(temp1); //余額整數(shù) 十六進(jìn)制字符串轉(zhuǎn)十進(jìn)制數(shù)
- temp1[0] = '\0'; // 字符常量占一個字節(jié)的內(nèi)存空間
- temp2[0] = '\0'; // 字符常量占一個字節(jié)的內(nèi)存空間
- memset(money, 0, sizeof(money)); //將money中當(dāng)前位置后面清零 。
- sum = inputnum + inteDec; //充值后余額整數(shù)部分之和
- re = sum;
- Int_char(re); //十進(jìn)制轉(zhuǎn)字符串
- led(); //led燈亮
- LCD_clear(); //清屏
- LCD_Desk2(); //lcd顯示正在充值
- while (Result[i] != '\0')
- {
- LCD_write_str(Result); //lcd顯示10進(jìn)制字符串
- i++;
- }
- delay_ms(1800);
- }
- if(sum<65536) //將充值完畢的余額總數(shù)再次存儲到寄存器內(nèi)。
- {
- itoa(sum,money_inch_hex,16); //10進(jìn)制數(shù)值轉(zhuǎn)為16進(jìn)制字符串,整數(shù)部分
- i=strlen(money_inch_hex);
- switch(i)
- {
- case 0:
- for(num=0;num<4;num++)
- {
- money_inch_hex[num]='0';
- }
- money_inch_hex[4]='\0';
- break;
- case 1:
- money_inch_hex[4]='\0';
- money_inch_hex[3]=money_inch_hex[0];
- money_inch_hex[2]='0';
- money_inch_hex[1]='0';
- money_inch_hex[0]='0';
- break;
- case 2:
- money_inch_hex[4]='\0';
- money_inch_hex[3]=money_inch_hex[1];
- money_inch_hex[2]=money_inch_hex[0];
- money_inch_hex[1]='0';
- money_inch_hex[0]='0';
- break;
- case 3:
- money_inch_hex[4]='\0';
- money_inch_hex[3]=money_inch_hex[2];
- money_inch_hex[2]=money_inch_hex[1];
- money_inch_hex[1]=money_inch_hex[0];
- money_inch_hex[0]='0';
- break;
- }
- StringToHex(money_i, money_inch_hex); //16進(jìn)制字符串轉(zhuǎn)為16進(jìn)制字節(jié)數(shù)組
- for(num=0;num<2;num++) //置高位在前
- {
- money_ok[num]=money_i[1-num];
- }
- }
- return status2; //返回判斷值
復(fù)制代碼函數(shù)功能基本實(shí)現(xiàn),因?yàn)槭菍?shí)現(xiàn)充值功能,所以為了防止連續(xù)多次充值,在主函數(shù)最后設(shè)置了break環(huán)節(jié)。 在實(shí)際操作過程中,在刷卡充值的時候,不能過早的移開卡片,否則會導(dǎo)致充卡失敗。 
|