#include "LCD1602.h"
#include "rc522.h"
#include "uart.h"
#include "motor_pwm.h"
uchar data1[16] = {0x03,0x55,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x00,0x00,0x01,0xFE,0x01,0xFE};
uchar g_ucTempbuf[16];
uchar code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
void yuyin(uchar dizhi)
{
Send_Data(0x7E);
Send_Data(0x04);
Send_Data(0x41);
Send_Data(0x00);
Send_Data(dizhi);
Send_Data(0xEF);
}
void main(void)
{
uchar time1s;
uint jishi1=0,jishi2=0,jishi3=0;
uint feiyong;
bit as1=0,as2=0,as3=0;
uchar bs1=0,bs2=0,bs3=0;
char i=0;
uchar status;
uchar disp1[16]={" Stop Car "};
uchar disp2[16]={" 1: 2: 3: "};
LCD_Init(); //LCD1602初始化
PcdReset();
PcdAntennaOff();
PcdAntennaOn();
M500PcdConfigISOType( 'A' );
LCD_Init();//LCD初始化
InitUART();//串口初始化
pwm_init();
WriteChar(1,0,16,disp1);
WriteChar(2,0,16,disp2);
stom=5;
TR2=1;
delayms(500);
TR2=0;
while(1)
{
if(++time1s>30)//车辆入库开始计费
{
time1s=0;
if(as1)jishi1++;
else jishi1=0;
if(as2)jishi2++;
else jishi2=0;
if(as3)jishi3++;
else jishi3=0;
}
if(1)
{
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡,进入磁场后可多次读写PICC_REQALL,单次读写PICC_REQIDL
if (status == MI_OK)
{
status = PcdAnticoll(g_ucTempbuf);//防冲撞
if (status != MI_OK)
{ continue; }
status = PcdSelect(g_ucTempbuf);//选定卡片
if (status != MI_OK)
{ continue; }
status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, g_ucTempbuf);//验证卡片密码
if (status != MI_OK)
{ continue; }
status = PcdRead(1, g_ucTempbuf);//读块
if (status != MI_OK)
{ continue; }
//车辆A
if((0x01==g_ucTempbuf[0]) && (data1[1]==g_ucTempbuf[1]) && (data1[2]==g_ucTempbuf[2]) &&
(data1[3]==g_ucTempbuf[3]) && (data1[4]==g_ucTempbuf[4]) && (data1[5]==g_ucTempbuf[5]) )
{
if(!as1)//入库
{
as1=1;
WriteChar(1,0,16,"A in storage ");
if(disp2[3]==' ')
{
disp2[3]='*';disp2[4]='A';
yuyin(1);
bs1=1;
}
else if(disp2[8]==' ')
{
disp2[8]='*';disp2[9]='A';
yuyin(2);
bs1=2;
}
else if(disp2[13]==' ')
{
disp2[13]='*';disp2[14]='A';
yuyin(3);
bs1=3;
}
}
else//出库
{
as1=0;
WriteChar(1,0,16,"A out storage ");
WriteChar(2,0,16,"Amount: ");
feiyong=(jishi1/60)*1;//以分钟为单位
WriteCOMDATA(0xc7,0);
WriteCOMDATA(feiyong/100%10+0x30,1);
WriteCOMDATA(feiyong/10%10+0x30,1);
WriteCOMDATA(feiyong%10+0x30,1);
if(bs1==1)
{
disp2[3]=' ';disp2[4]=' ';
}
else if(bs1==2)
{
disp2[8]=' ';disp2[9]=' ';
}
else if(bs1==3)
{
disp2[13]=' ';disp2[14]=' ';
}
yuyin(4);
bs1=0;
}
//打开杆子
stom=11;
TR2=1;
delayms(500);
TR2=0;
delayms(4000);//延时
//放下杆子
stom=5;
TR2=1;
delayms(500);
TR2=0;
WriteChar(1,0,16,disp1);
WriteChar(2,0,16,disp2);
}
//车辆B
else if((0x02==g_ucTempbuf[0]) && (data1[1]==g_ucTempbuf[1]) && (data1[2]==g_ucTempbuf[2]) &&
(data1[3]==g_ucTempbuf[3]) && (data1[4]==g_ucTempbuf[4]) && (data1[5]==g_ucTempbuf[5]) )
{
if(!as2)//入库
{
as2=1;
WriteChar(1,0,16,"B in storage ");
if(disp2[3]==' ')
{
disp2[3]='*';disp2[4]='B';
yuyin(1);
bs2=1;
}
else if(disp2[8]==' ')
{
disp2[8]='*';disp2[9]='B';
yuyin(2);
bs2=2;
}
else if(disp2[13]==' ')
{
disp2[13]='*';disp2[14]='B';
yuyin(3);
bs2=3;
}
}
else//出库
{
as2=0;
WriteChar(1,0,16,"B out storage ");
WriteChar(2,0,16,"Amount: ");
feiyong=(jishi2/60)*1;//以分钟为单位
WriteCOMDATA(0xc7,0);
WriteCOMDATA(feiyong/100%10+0x30,1);
WriteCOMDATA(feiyong/10%10+0x30,1);
WriteCOMDATA(feiyong%10+0x30,1);
if(bs2==1)
{
disp2[3]=' ';disp2[4]=' ';
}
else if(bs2==2)
{
disp2[8]=' ';disp2[9]=' ';
}
else if(bs2==3)
{
disp2[13]=' ';disp2[14]=' ';
}
yuyin(4);
bs2=0;
}
//打开杆子
stom=11;
TR2=1;
delayms(500);
TR2=0;
delayms(4000);//延时
//放下杆子
stom=5;
TR2=1;
delayms(500);
TR2=0;
WriteChar(1,0,16,disp1);
WriteChar(2,0,16,disp2);
}
//车辆C
else if((0x03==g_ucTempbuf[0]) && (data1[1]==g_ucTempbuf[1]) && (data1[2]==g_ucTempbuf[2]) &&
(data1[3]==g_ucTempbuf[3]) && (data1[4]==g_ucTempbuf[4]) && (data1[5]==g_ucTempbuf[5]) )
{
if(!as3)//入库
{
as3=1;
WriteChar(1,0,16,"C in storage ");
if(disp2[3]==' ')
{
disp2[3]='*';disp2[4]='C';
yuyin(1);
bs3=1;
}
else if(disp2[8]==' ')
{
disp2[8]='*';disp2[9]='C';
yuyin(2);
bs3=2;
}
else if(disp2[13]==' ')
{
disp2[13]='*';disp2[14]='C';
yuyin(3);
bs3=3;
}
}
else//出库
{
as3=0;
WriteChar(1,0,16,"C out storage ");
WriteChar(2,0,16,"Amount: ");
feiyong=(jishi3/60)*1;//以分钟为单位
WriteCOMDATA(0xc7,0);
WriteCOMDATA(feiyong/100%10+0x30,1);
WriteCOMDATA(feiyong/10%10+0x30,1);
WriteCOMDATA(feiyong%10+0x30,1);
if(bs3==1)
{
disp2[3]=' ';disp2[4]=' ';
}
else if(bs3==2)
{
disp2[8]=' ';disp2[9]=' ';
}
else if(bs3==3)
{
disp2[13]=' ';disp2[14]=' ';
}
yuyin(4);
bs3=0;
}
//打开杆子
stom=11;
TR2=1;
delayms(500);
TR2=0;
delayms(4000);//延时
//放下杆子
stom=5;
TR2=1;
delayms(500);
TR2=0;
WriteChar(1,0,16,disp1);
WriteChar(2,0,16,disp2);
}
}
}
if(0)//IC卡注册
{
WriteChar(1,0,16," IC ZhuCe ");
WriteChar(2,0,16," ");
status = PcdRequest(PICC_REQALL, g_ucTempbuf);//寻卡,进入磁场后可多次读写PICC_REQALL,单次读写PICC_REQIDL
if (status == MI_OK)
{
status = PcdAnticoll(g_ucTempbuf);//防冲撞
if (status != MI_OK)
{ continue; }
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; }
yuyin(4);
WriteChar(2,0,16," OK!!! ");
delayms(4000);
}
}
}
}