通过RC522装置,能够给卡片充值金额,记录地铁站位置以及抵达的地铁站位置,将地铁站的位置进行计算,将充值的金额减去,如果不能大于等于0则无法出站,显示屏会出现请充值字样。
单片机源程序如下:
- #include "common.h"
- #include "RC522.h"
- #include "uart.h"
- #include "juzhenanjian.h"
- #include "12864.h"
- #define DEBUG
- unsigned char code DefaultKey[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- //unsigned char writeBuf[16] = {0x01, 0x00, 0x03, 0x00, 0x00, 0x00};
- int num,flag,i,fig,mode=0,fig1=0;
- static volatile int s=0;
- /* 充值
- ** 金额数
- **
- */
- void Value()
- { num =keyscan();
- switch (num)
- {
- case 1:fig=0x01;fig1=1;
- Lcd_Num_Display1(1,6,fig1);
- break;
- case 2:fig=0x02;fig1=2;
- Lcd_Num_Display1(1,6,fig1);
- break;
- case 3:fig=0x03;fig1=3;
- Lcd_Num_Display1(1,6,fig1);
- break;
- case 4:mode=1 ;
- Lcd_Write_Com(0x01);
- Lcd_Display(1,1,"充值金额: 元");
- break;
- case 5:fig=0x04;fig1=4;
- Lcd_Num_Display1(1,6,fig1);
- break;
- case 8:mode=2 ;
- Lcd_Write_Com(0x01);
- Lcd_Display(1,1,"刷卡模式:");
- break;
- }
- }
- /* 金额充值
- ** value 金额数
- ** *sernum 序列号
- */
- void ChangeValue(unsigned char *sernum, unsigned int value)
- {
- unsigned char status;
- unsigned char readBuf[16];
- unsigned char writeBuf[16]=0;
-
- #ifdef DEBUG
- UART_TC("You will be select!\n");
- #endif
- status = PcdSelect(sernum);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Select card ok!\n");
- #endif
- status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey, sernum);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Authent A success!\n");
- #endif
- status = PcdRead(1, readBuf);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Read OK\n");
- for(i=0; i<16; i++)
- {tochar(readBuf[i]);}
- #endif
- if(status == MI_OK)
- {
- writeBuf[2] = value;
- PcdWrite(1,writeBuf);
- UART_TC("Write OK\n");
- }
- if(readBuf[2]==1 || readBuf[2]==2 || readBuf[2]==3)
- {
- Lcd_Write_Com(0x01);
- Lcd_Display(1,1,"充值成功: 元");
- Lcd_Num_Display1(1,6,fig1);
- }
- }
- }
- }
- }
- void ShowCardType(unsigned char *type)
- {
- UART_TC("Card Type: ");
-
- if(type[0]==0x04 && type[1]==0x00)
- UART_TC("MFone-S50\n");
- else if(type[0]==0x02 && type[1]==0x00)
- UART_TC("MFone-S70\n");
- else if(type[0]==0x44 && type[1]==0x00)
- UART_TC("MF-UltraLight\n");
- else if(type[0]==0x08 && type[1]==0x00)
- UART_TC("MF-Pro\n");
- else if(type[0]==0x44 && type[1]==0x03)
- UART_TC("MF Desire\n");
- else
- UART_TC("Unknow Card Type\n");
- }
- void ShowCardId(unsigned char *id)
- {
- int IdLen = 4;
- int i;
-
- for(i=0; i<IdLen; i++){
- tochar(id[i]);
- }
- UART_TC("\n");
- }
- void InoutHandle(const unsigned char *sernum)
- {
- unsigned char status;
- unsigned char readBuf[16];
- unsigned char writeBuf[16];
-
- #ifdef DEBUG
- UART_TC("You will be select!\n");
- #endif
- status = PcdSelect(sernum);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Select card ok!\n");
- #endif
- status = PcdAuthState(PICC_AUTHENT1A, 1, DefaultKey , sernum);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Authent A success!\n");
- #endif
- status = PcdRead(1, readBuf);
- if(status == MI_OK){
- #ifdef DEBUG
- UART_TC("Read OK\n");
- for(i=0; i<16; i++)
- {tochar(readBuf[i]);
- }
- #endif
- writeBuf[2] = readBuf[2];
- switch(flag)
- {
- case 0x0A :tochar(flag);
- if(readBuf[1]==0) // 第一位 0进站1出站 第0位记录进站位置 第2位金额 {
- {
- writeBuf[0]=0x0A;
- writeBuf[1]=1;
- PcdWrite(1,writeBuf);
- UART_TC("Int success!\n");
- Lcd_Display(1,6,"进站");
- }
- else
- {
- if(readBuf[0]==0x0B)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"Success Thank you!");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");
- }
- }
- else if(readBuf[0]==0x0C)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0D)
- {
- if(readBuf[2]>=0x03)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0E)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- }
- break;
- case 0x0B :tochar(flag);
- if(readBuf[1]==0)
- {
- writeBuf[0]=0x0B;
- writeBuf[1]=1;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"进站");
-
- }
- else
- {
- if(readBuf[0]==0x0A)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0C)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0D)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0E)
- {
- if(readBuf[2]>=0x04)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值 ");}
- }
- }
- break;
- case 0x0C :tochar(flag);
- if(readBuf[1]==0)
- {
- writeBuf[0]=0x0C;
- writeBuf[1]=1;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"进站");
- }
- else
- {
- if(readBuf[0]==0x0B)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0A)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0D)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0E)
- {
- if(readBuf[2]>=0x03)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- }
- break;
- case 0x0D :tochar(flag);
- if(readBuf[1]==0)
- {
- writeBuf[0]=0x0D;
- writeBuf[1]=1;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"进站");
-
- }
- else
- {
- if(readBuf[0]==0x0B)
- {
- if(readBuf[2]>=2)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0C)
- {
- if(readBuf[2]>=0x01)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0A)
- {
- if(readBuf[2]>=0x03)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0E)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- }
- break;
- case 0x0E :tochar(flag);
- if(readBuf[1]==0)
- {
- writeBuf[0]=0x0E;
- writeBuf[1]=1;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"进站");
-
- }
- else
- {
- if(readBuf[0]==0x0B)
- {
- if(readBuf[2]>=0x03)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0C)
- {
- if(readBuf[2]>=0x03)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0D)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,1,"Success Thanks");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- else if(readBuf[0]==0x0A)
- {
- if(readBuf[2]>=0x02)
- {LED_OK=0;
- writeBuf[0]=0;
- writeBuf[1]=0;
- writeBuf[2]=0;
- PcdWrite(1,writeBuf);
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"Success Thank you!");
- LED_FAILED=1;
- ALARM=1;}
- else
- {LED_FAILED=0;
- ALARM=0;
- Lcd_Display(1,6,"出站");
- Lcd_Display(2,2,"失败,请充值");}
- }
- }
- break;
- default:
- LED_FAILED=0;
- ALARM=0;
- Lcd_Display(2,2,"error");
- }
- }
- }
- }
- }
复制代码
所有资料51hei提供下载:
RFID 最终程序.zip
(185.06 KB, 下载次数: 27)
|