#include <reg52.h>
#include <STDIO.H>
#include <string.h>
#include "main.h"
#include "mfrc522.h"
sbit dula=P2^6;
sbit wela=P2^7;
int num,num1,flag;
unsigned char code table[]={
0x3f,0x06,0x5b,0x4f,0x66,
0x6d,0x7d,0x07,0x7f,0x6f,0x77,
0x7c,0x39,0x5e,0x79,0x71};
unsigned char code data1[16] = {0x12,0x34,0x56,0x78,0xED,0xCB,0xA9,0x87,0x12,0x34,0x56,0x78,0x01,0xFE,0x01,0xFE};
//M1卡的某一塊寫為如下格式,則該塊為錢包,可接收扣款和充值命令
//4字節金額(低字節在前)+4字節金額取反+4字節金額+1字節塊地址+1字節塊地址取反+1字節塊地址+1字節塊地址取反
unsigned char code data2[4] = {0,0,0,0x01};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char g_ucTempbuf[20];
void Delay1000ms() //@11.0592MHz
{
unsigned char i, j, k;
i = 8;
j = 1;
k = 243;
do{
do{
while (--k);
} while (--j);
} while (--i);
}
void display(unsigned char num)
{
dula=1;
P0=table[num];
dula=0;
wela=1;
P0=0xfe;
wela=0;
}
void main( ) {
unsigned char status,i;
unsigned int temp;
InitializeSystem( );
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
while (1)
{
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//尋卡
if (status != MI_OK) {
InitializeSystem( );
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
continue;
}
printf("卡的類型:");
for(i=0; i<2; i++) {
temp=g_ucTempbuf[i];
printf("%X",temp);
}
status = PcdAnticoll(g_ucTempbuf);//防沖撞
if (status != MI_OK) {
continue;
}
////////以下為超級終端打印出的內容////////////////////////
printf("卡序列號:"); //超級終端顯示,
for(i=0; i<4; i++) {
temp=g_ucTempbuf[i];
printf("%X",temp);
}
///////////////////////////////////////////////////////////
status = PcdSelect(g_ucTempbuf);//選定卡片
if (status != MI_OK) {
continue;
}
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//驗證卡片密碼
if (status != MI_OK) {
continue;
}
status = PcdWrite(1, data1);//寫塊
if (status != MI_OK) {
continue;
}
while(1) {
display(num);
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//尋卡
if (status != MI_OK) {
InitializeSystem( );
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
continue;
}
status = PcdAnticoll(g_ucTempbuf);//防沖撞
if (status != MI_OK) {
printf("防沖撞失敗\n");
continue;
}
printf("卡序列號:"); //超級終端顯示,
for(i=0; i<4; i++) {
temp=g_ucTempbuf[i];
printf("%X",temp);
}
printf("\n");
status = PcdSelect(g_ucTempbuf);//選定卡片
if (status != MI_OK) {
printf("選卡失敗\n");
continue;
}
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//驗證卡片密碼
if (status != MI_OK) {
printf("密碼驗證失敗\n");
continue;
}
status = PcdValue(PICC_DECREMENT,1,data2);//扣款
if (status != MI_OK) {
printf("扣款失敗\n");
continue;
}
status = PcdBakValue(1, 2);//塊備份
if (status != MI_OK) {
printf("塊備份失敗\n");
continue;
}
status = PcdRead(2, g_ucTempbuf);//讀塊
if (status != MI_OK) {
printf("讀塊失敗\n");
continue;
}
printf("卡讀塊:"); //超級終端顯示,
for(i=0; i<16; i++) {
temp=g_ucTempbuf[i];
printf("%X",temp);
}
printf("\n");
if(PcdHalt() != MI_OK){
printf("休眠卡片失敗\n");
}
Delay1000ms();
num=num+3;
flag=1;
}
}
}
/////////////////////////////////////////////////////////////////////
//系統初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem() {
P0 = 0xFF;
P1 = 0xFF;
P2 = 0xFF;
P3 = 0xFF;
TMOD=0x21; //設T0為方式1,GATE=1;
SCON=0x50;
TH1=0xF4; //波特率為2400bps
TL1=0xF4;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=1;
ET0=1; //允許T0中斷
TR1=1; //開啟定時器
TI=1; //發送標志位
EA=1; //開啟總中斷
// ES = 1; //允許串口中斷
// RI = 1; //接收標志位
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
if(flag==1)
{
num1++;
if(num1==20)
{
num1=0;
num=num-1;
if(num<=0)
num=9;
}
}
}
/*
void Uart(void) interrupt 4
{
if(RI)
{
if(SBUF == 0)
printf("接收到\n");
RI = 0;
}
}
*/
|