萌新想问问大佬们在这个程序里UartProcess跟iccardcode两个方法的作用:
#include "reg52.h"
#include "main.h"
#include "mfrc522.h"
#include "LCD12864.h"
#include "delay.h"
#include <string.h>
#include <stdio.h>
sbit BEEP=P1^0 ;
//sbit KEY=P1^2 ;
sbit KEY1=P1^4;//撤销按键
sbit LED=P1^1;
#define SET_BEEP BEEP = 0
#define CLR_BEEP BEEP = 1
signed long dat=0;
unsigned char dat1=0,ii=0,iii=0,ii1=0,tt1=0;//ii充值消费标志;iii出入库标志;tt1电机控制
char string[]=" ";
//M1卡的某一块写为如下格式,则该块为钱包,可接收扣款和充值命令
//4字节金额(低字节在前)+4字节金额取反+4字节金额+1字节块地址+1字节块地址取反+1字节块地址+1字节块地址取反
unsigned char code data2[4] = {0x12,0,0,0};
unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
//unsigned char g_ucTempbuf[20];
unsigned char idata MLastSelectedSnr[4];
unsigned char idata RevBuffer[30];
//unsigned char data SerBuffer[20];
bit CmdValid;
signed long xiaofei=0;
signed long chongzhi=5000;
unsigned char code ASCII_NUM[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
unsigned char code Yankey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //原始密码
unsigned char code XiugaiYunKey[16]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFE,0xFF,0x07,0x80,0x69,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; //修改后的密码
unsigned char code Nowkey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}; //xianzia 密码
unsigned char code XiugaiNowKey[16]= {0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x07,0x80,0x69,0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
unsigned char idata Read_Data[16]={0x00};
//unsigned char idata RevBuffer[30];
//unsigned char idata MLastSelectedSnr[4];
unsigned char data OperationCard;
uchar data bWarn,bPass,ttt=0,tt=0;//tt键盘功能标志
#define REGCARD 0xa1
#define CONSUME 0xa2
#define READCARD 0xa3
#define ADDMONEY 0xa4
#define CHEXIAO 0xa5
void STC_send_bluetooth(unsigned char a)
{ //注意:若单片机TXD(P3.1)无上拉能力,必须在P3.1端接上拉电阻。
TI=0;
SBUF=a;
while(TI==0);
TI=0;
}
void KeyScan(void)
{
char string[]=" ";
if(ttt==0)
{
if(KEY1 == 0)//撤销
{
delay_10ms(1);
if(KEY1 == 0 )
{
OperationCard=CHEXIAO;
tt=4;}
}
if(REGCARDBTN == 0)//注册
{
delay_10ms(1);
if(REGCARDBTN == 0 )
OperationCard = REGCARD;
tt=3;
}
if(CONSUMEBTN == 0)//消费
{
delay_10ms(1);
if(CONSUMEBTN == 0 )
OperationCard = CONSUME; tt=1;
}
if(ADDMONEYBTN == 0)//充值
{
delay_10ms(1);
if(ADDMONEYBTN == 0 )
OperationCard = ADDMONEY; tt=2;
}
// if(READCARDBTN == 0)//读卡信息
// {
// delay_10ms(1);
// if(READCARDBTN == 0 )
// OperationCard = READCARD; tt=4;
// }
}
}
void Warn(void)
{
uchar data ii;
for(ii=0;ii<3;ii++)
{
SET_BEEP;//ShortBeep();如果是有源蜂鸣器就一句SET_BEEP就ok了
delay_10ms(5);
CLR_BEEP;
delay_10ms(5);
}
}
void WaitCardOff(void)
{
char status;
unsigned char TagType[2];
while(1)
{
status = PcdRequest(PICC_REQALL , TagType);
if(status)
{
status = PcdRequest(PICC_REQALL , TagType);
if(status)
{
status = PcdRequest(PICC_REQALL , TagType);
if(status)
{
return;
}
}
}
//delay_10ms(5);
}
}
void Pass(void)
{
SET_BEEP;//LongBeep();如果是有源蜂鸣器就一句SET_BEEP就ok了
delay_10ms(30);
CLR_BEEP;
}
void CtrlProcess(void)
{
unsigned char idata Write_Consume_Data[16];
signed long money_value;
float f_money_value;
unsigned char ii;
char status;
status=PcdRequest(PICC_REQIDL,&RevBuffer[0]);//寻天线区内未进入休眠状态的卡,返回卡片类型 2字节
if(status!=MI_OK)
return;
for(ii=0;ii<2;ii++)
{
string[2*ii] = ASCII_NUM[(RevBuffer[ii]>>4)&0x0f];
string[2*ii+1] = ASCII_NUM[RevBuffer[ii]&0x0f];
}
string[4] = '\0';
// LCD_PutString(0,1,"类型: IC卡 ");
LCD_PutString(0,2,"停车卡: ");
LCD_PutString(0,3," ");
//LCD_PutString(3,1,string);//LCD显示卡类型
status=PcdAnticoll(&RevBuffer[2]);//防冲撞,返回卡的序列号 4字节
if(status!=MI_OK)
return;
memcpy(MLastSelectedSnr,&RevBuffer[2],4);//4字节序列号 放到MLastSelectedSnr
for(ii=0;ii<4;ii++)
{
string[2*ii] = ASCII_NUM[(MLastSelectedSnr[ii]>>4)&0x0f];
string[2*ii+1] = ASCII_NUM[MLastSelectedSnr[ii]&0x0f];
}
string[9] = '\0';
LCD_PutString(3,2,string);// LCD显示序列号
status=PcdSelect(MLastSelectedSnr);//选卡
if(status!=MI_OK)
return;
if(OperationCard==REGCARD)//注册
{
while(1)
{
LCD_PutString(0,1,"类型: IC卡 ");
status=PcdAuthState(PICC_AUTHENT1A,7,Yankey,MLastSelectedSnr);//
//DelayNS(1);
if(status!=MI_OK)
{ LCD_PutString(0,3," 已注册: "); LCD_PutString(0,4," ");Warn(); WaitCardOff(); break; }
status=PcdWrite(7,XiugaiYunKey);
if(status!=MI_OK)
{
// bWarn=1;
break;
}
for(ii=0;ii<16;ii++)
{
Write_Consume_Data[ii]=0xaa;//Write_First_Data
}
memset( Write_Consume_Data, 0, sizeof(Write_Consume_Data) );//Write_First_Data Write_First_Data
money_value = 0000;
memcpy( Write_Consume_Data, ( uchar * )&money_value, 4 ); //Write_First_Data
status=PcdWrite(4,&Write_Consume_Data[0]);//Write_First_Data
if(status!=MI_OK)
{
bWarn=1;
break;
}
LCD_PutString(0,3,"注册: 成功 ");
LCD_PutString(0,4," ");
Pass();
PcdHalt();
}
}
if(OperationCard==CHEXIAO)//撤销
{
while(1)
{
LCD_PutString(0,1,"类型: IC卡 ");
//status=PcdAuthState(PICC_AUTHENT1A,7,LastKeyB,MLastSelectedSnr);//
// status = PcdAuthState(PICC_AUTHENT1A,7,NewKeyA, MLastSelectedSnr);//验证卡片密码(原始的密码)FE
status = PcdAuthState(PICC_AUTHENT1A,7,Nowkey,MLastSelectedSnr);
if(status!=MI_OK)
{ LCD_PutString(0,4," 已撤销: "); Warn(); WaitCardOff(); break; }
status=PcdWrite(7,XiugaiNowKey);
if(status!=MI_OK)
{
// bWarn=1;
break;
}
for(ii=0;ii<16;ii++)
{
Write_Consume_Data[ii]=0xaa;//Write_First_Data
}
memset( Write_Consume_Data, 0, sizeof(Write_Consume_Data) );//Write_First_Data Write_First_Data
money_value = 0000;
memcpy( Write_Consume_Data, ( uchar * )&money_value, 4 ); //Write_First_Data
status=PcdWrite(4,&Write_Consume_Data[0]);//Write_First_Data
if(status!=MI_OK)
{
bWarn=1;
break;
}
LCD_PutString(0,3,"注销: 成功 ");
Pass();
PcdHalt();
}
}
else if(OperationCard==CONSUME)//消费
{
while(1)
{
status=PcdAuthState(PICC_AUTHENT1A,7,Nowkey,MLastSelectedSnr);//
if(status!=MI_OK)
{ LCD_PutString(0,1,"类型: IC卡 "); LCD_PutString(0,3," 无效卡: ");LCD_PutString(0,4," "); Warn(); WaitCardOff(); break; }
if(ii1==3) {ii1=0;Warn();}
status=PcdRead(4,Read_Data);
if(status!=MI_OK)
{
break;
}
if(iii==0){ LCD_PutString(0,1,"类型: IC卡 ");}
memcpy( (uchar *)&money_value, Read_Data, 4 );
if(money_value>0)
{
STC_send_bluetooth('Y');
STC_send_bluetooth(string[0] );
STC_send_bluetooth(string[1] );
STC_send_bluetooth(string[2] );
STC_send_bluetooth(string[3] );
STC_send_bluetooth(string[4] );
STC_send_bluetooth(string[5] );
STC_send_bluetooth(string[6] );
STC_send_bluetooth(string[7] );
bPass=1;
delay_10ms(100);//延迟一秒,等待系统返回数据
//这里须修改为余额小于消费时不减
if(money_value-xiaofei>=0)
{
money_value=money_value-xiaofei;
if(iii==1){ LCD_PutString(0,1,"类型: IC卡 入库");iii=0;}
if(iii==2){ LCD_PutString(0,1,"类型: IC卡 出库");}
if(tt1==1) { LED=0; tt1=0;}
}
else { LCD_PutString(0,1," 消费金额不足!");Warn();iii=0;}
memcpy( Write_Consume_Data, (uchar *)&money_value, 4 );
status=PcdWrite(4,&Write_Consume_Data[0]);
if(status!=MI_OK)
{
//bWarn=1;
break;
}
if(iii==2){ LCD_PutString(0,3,"消费: ");}
LCD_PutString(0,4,"余额: ");
string[0]=xiaofei/10000+0x30;
string[1]=xiaofei%10000/1000+0x30;
string[2]=xiaofei%1000/100+0x30;
string[3]='.';
string[4]=xiaofei%100/10+0x30;
string[5] = '\0';
delay_10ms(1);
if(iii==2){ LCD_PutString(3,3,string );iii=0;}
//string[0]=xiaofei/10000+0x30;
f_money_value = money_value * 1.0/100;
sprintf(string,"%5.1f",f_money_value);
LCD_PutString(3,4,string);
delay_10ms(1);
STC_send_bluetooth('Z');
STC_send_bluetooth(string[0]);
STC_send_bluetooth(string[1]);
STC_send_bluetooth(string[2]);
STC_send_bluetooth(string[3]);
STC_send_bluetooth(string[4]);
STC_send_bluetooth(string[5]);
Pass();
}
else
{ LCD_PutString(0,3," 消费金额不足!");
LCD_PutString(0,4,"余额: ");
f_money_value = money_value * 1.0/100;
sprintf(string,"%5.2f",f_money_value);
LCD_PutString(3,4,string);
Warn(); }
PcdHalt();
}
}
else if(OperationCard==ADDMONEY)//充值
{
while(1)
{
LCD_PutString(0,1,"类型: IC卡 ");
status=PcdAuthState(PICC_AUTHENT1A,7,Nowkey,MLastSelectedSnr);//????KUAI4?
if(status!=MI_OK)
{ LCD_PutString(0,3," 无效卡: "); LCD_PutString(0,4," ");Warn(); WaitCardOff(); break; }
status=PcdRead(4,Read_Data);
if(status!=MI_OK)
{
break;
}
memcpy( (uchar *)&money_value, Read_Data, 4 );
money_value=money_value+chongzhi;
memcpy( Write_Consume_Data, (uchar *)&money_value, 4 );
status=PcdWrite(4,&Write_Consume_Data[0]);
if(status!=MI_OK)
{
// bWarn=1;
break;
}
string[8] = money_value/10000+0x30;
string[9] = money_value%10000/1000+0x30;
string[10] = money_value%1000/100+0x30;
string[11] = '.';
string[12] = money_value%100/10+0x30;
string[13] = '\0';
delay_10ms(1);
STC_send_bluetooth('Z');
STC_send_bluetooth(string[0]);
STC_send_bluetooth(string[1]);
STC_send_bluetooth(string[2]);
STC_send_bluetooth(string[3]);
STC_send_bluetooth(string[4]);
STC_send_bluetooth(string[5]);
STC_send_bluetooth(string[6]);
STC_send_bluetooth(string[7]);
STC_send_bluetooth(string[8]);
STC_send_bluetooth(string[9]);
STC_send_bluetooth(string[10]);
STC_send_bluetooth(string[11]);
STC_send_bluetooth(string[12]);
STC_send_bluetooth(string[13]);
delay_10ms(1);
LCD_PutString(0,3,"充值: ");
LCD_PutString(0,4,"余额: ");
string[0]=chongzhi/10000+0x30;
string[1]=chongzhi%10000/1000+0x30;
string[2]=chongzhi%1000/100+0x30;
string[3] = '\0';
delay_10ms(1);
LCD_PutString(3,3,string );
f_money_value = money_value * 1.0/100;
sprintf(string,"%5.1f",f_money_value);
LCD_PutString(3,4,string);
delay_10ms(1);
PcdHalt();
Pass();
}
}
delay_10ms(10);
LED=1;
WaitCardOff();
PcdHalt();//中止
BEEP=1;
}
void iccardcode()
{
unsigned char cmd;
unsigned char status;
cmd = RevBuffer[0];
switch(cmd)
{
case 1: // Halt the card //终止卡的操作
status= PcdHalt();;
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 2: // Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)
// 寻卡,防冲突,选择卡 返回卡类型(2 bytes)+ 卡系列号(4 bytes)
status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
status= PcdRequest(RevBuffer[1],&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
}
RevBuffer[0]=3;
RevBuffer[1]=status;
break;
case 3: // 防冲突 读卡的系列号 MLastSelectedSnr
status = PcdAnticoll(&RevBuffer[2]);
if(status!=0)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
memcpy(MLastSelectedSnr,&RevBuffer[2],4);
RevBuffer[0]=5;
RevBuffer[1]=status;
break;
case 4: // 选择卡 Select Card
status=PcdSelect(MLastSelectedSnr);
if(status!=MI_OK)
{
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
}
RevBuffer[0]=3;
RevBuffer[1]=status;
break;
case 5: // Key loading into the MF RC500's EEPROM
status = PcdAuthState(RevBuffer[1], RevBuffer[3], DefaultKey, MLastSelectedSnr);// 校验卡密码
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 6:
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 7:
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 8: // Read the mifare card
// 读卡
status=PcdRead(RevBuffer[1],&RevBuffer[2]);
if(status==0)
{RevBuffer[0]=17;}
else
{RevBuffer[0]=1;}
RevBuffer[1]=status;
break;
case 9: // Write the mifare card
// 写卡 下载密码
status=PcdWrite(RevBuffer[1],&RevBuffer[2]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 10:
PcdValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);
RevBuffer[0]=1;
RevBuffer[1]=status;
break;
case 12: // 参数设置
PcdBakValue(RevBuffer[1], RevBuffer[2]);
RevBuffer[0]=1; //contact
RevBuffer[1]=0;
break;
}
}
/////////////////////////////////////////////////////////////////////
//系统初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem()
{
CmdValid=0;
LED_GREEN = 1;
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
M500PcdConfigISOType( 'A' );
LED_GREEN = 0;
delay_10ms(10);
LED_GREEN = 1;
delay_10ms(10);
LED_GREEN = 0;
}
///////////////////////////////////////////////////////////////////////
// 接收和发送中断
///////////////////////////////////////////////////////////////////////
void isr_UART(void) interrupt 4 using 1
{
unsigned char len, i;
unsigned int j=0;
if(RI)
{
len=SBUF;
RI=0;
for(i=0;i<len;i++)
{
while(!RI)
{
j++;
if(j>1000)
{
break;
}
}
if(j<1000)
{
RevBuffer[i]=SBUF;
RI=0;
j=0;
}
else
{
break;
}
}
if(i==len)
{
REN=0;
CmdValid=1;
}
}
else if(!RI && TI)
{
TI=0;
len=RevBuffer[0];
for(i=0;i<len+1;i++)
{
SBUF=RevBuffer[i];
while(!TI);
TI=0;
}
REN=1;
}
}
void UartProcess(void)
{
unsigned char baud;
if(RevBuffer[0]==11) //为了加快响应速度 测试峰鸣器
{
RevBuffer[2]=RevBuffer[1];
RevBuffer[0]=1; //contact
RevBuffer[1]=0;
CALL_isr_UART();
LED_GREEN = 1;
delay_10ms(RevBuffer[2]);
LED_GREEN = 0;
}
else if(RevBuffer[0]==13) //设置通讯波特率
{
switch(RevBuffer[1])
{
case 0:
baud=BAUD_9600;
break;
}
RevBuffer[0]=1; //contact
RevBuffer[1]=0;
CALL_isr_UART();
delay_10ms(5);
TR1 = 0;
TH1 = baud;
TL1 = TH1;
delay_10ms(2);
TR1 = TRUE;
}
else
{
iccardcode();
CALL_isr_UART();
}
}
void InitializeSystem1()
{
P0 = 0xFF; P1 = 0xFF; P3 = 0xFF;P2 = 0xFF;
TMOD = 0x21; //T1,T0同时使用,T1方式2,T0方式1
TH1 = 0xFd; //11.0592M晶振,9600波特率
TL1 = 0xFd;
TH0 = 0xfe; //11.0592M晶振,9600波特率
TL0 = 0x33;
ET0 = 1; //开定时器中断
TR0 = 1;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;//允许串口接收
ES = 1;//开串口 中断
EA = 1;
}
void timer0() interrupt 1 /* T0中断服务程序 */
{
TH0=0xfe; //11.0592M晶振,9600波特率
TL0=0xee;
}
void sint() interrupt 4 using 2 //串口中断接收3个字节
{
if(RI) //是否接收中断
{
RI=0;
dat=SBUF;
dat1=SBUF;
if(ii==1) {xiaofei=dat*100;ii=0;}
if(ii==2) {chongzhi=dat*100;ii=0;}
if(dat1=='A') { ii=1;}
if(dat1=='G') { tt1=1;}
if(dat1=='B') { ii=2;}
if(dat1=='D') { ii1=3;}
if(dat1=='E') { iii=1;}
if(dat1=='F') { iii=2;}
}
}
void main( )
{
Init_ST7920(); //初始化
ClrScreen();
InitializeSystem( );//
InitializeSystem1( );//定时器初始化
OperationCard = 0;
RevBuffer[0]=2;RevBuffer[1]='a';RevBuffer[2]='b';
CALL_isr_UART();
STC_send_bluetooth('J');
tt=1;
OperationCard = CONSUME;
// P1=0X0F;
while (1)
{
if (CmdValid)//命令是否和合法
{
CmdValid = FALSE;
UartProcess();
}
KeyScan();//键盘扫描
if(ttt==0 && tt==1)
{
LCD_PutString(0,1," ");
LCD_PutString(0,2," 欢迎您进入某 ");
LCD_PutString(0,3," 某小区停车场 ");
LCD_PutString(0,4," ");
CtrlProcess();//控制处理}
}
if(tt==2)
{
LCD_PutString(0,1," ");
LCD_PutString(0,2," 充值界面 ");
LCD_PutString(0,3," ");
LCD_PutString(0,4," ");
CtrlProcess();//控制处理}
}
if(tt==3)
{
LCD_PutString(0,1," ");
LCD_PutString(0,2," 注册界面 ");
LCD_PutString(0,3," ");
LCD_PutString(0,4," ");
CtrlProcess();//控制处理}
}
if(tt==4)
{
LCD_PutString(0,1," ");
LCD_PutString(0,2," 撤销界面 ");
LCD_PutString(0,3," ");
LCD_PutString(0,4," ");}
CtrlProcess();//控制处理}
}
}
|