RC522读卡好通过串口传到PC机显示卡号
每次都会有一个字符不一样
请问是什么问题?
代码:
- #include "reg52.h"
- #include "lcd.h"
- #include<stdio.h>
- #include <intrins.h>
- #include "rc522.h"
- unsigned char code lzdID[4]={0xE0,0x23,0xD0,0xA4};
- unsigned char code lhjID[4]={0x1E,0x1B,0x06,0xA3};
- unsigned char code lqcID[4]={0x80,0x2C,0x04,0x7D};
- unsigned char UID[5];
- unsigned char Temp[4] ;
- unsigned long time_20ms;
- char dis0[16];
- char dis1[16];
- char dis2[16];
- unsigned char lzdFlag ='N';
- unsigned char lhjFlag ='N';
- unsigned char lslFlag ='N';
- unsigned char disFlag =0;
- unsigned char manNum=0;
- unsigned char a;
- void Init_Timer0(void);
- void UsartInit(void);
- void uartSendStr(unsigned char *s,unsigned char length);
- void uartSendByte(unsigned char dat);
- void UsartInit()
- {
- SCON=0X50;
- TMOD=0X20;
- PCON=0X80;
- TH1=0XF9;
- TL1=0XF9;
- ES=1;
- EA=1;
- TR1=1;
- }
- void delay(unsigned char i)
- {
- while(i--);
- }
- void main()
- {
- unsigned char disPlace=0;
- Init_Timer0();
- delay(100);
- UsartInit();
- delay(100);
- LcdInit();
- delay(100);
- LCD_Clear();
- delay(100);
- PcdReset();
- PcdAntennaOn();
- while(1)
- {
- if(PcdRequest(0x52,Temp)==MI_OK)
- {
- if(PcdAnticoll(UID)==MI_OK)
- {
- uartSendByte(0x00);
- uartSendStr(UID,4);
- uartSendByte(0x00);
- if((UID[0]==lzdID[0])&&(UID[1]==lzdID[1]))
- {
- lzdFlag ='Y';
- sprintf(dis0,"aaaaaaaa");
- LCD_Write_String(3,0,dis0);
- }
- else if((UID[0]==lhjID[0])&&(UID[1]==lhjID[1]))
- {
- lhjFlag ='Y';
- sprintf(dis0,"bbbbbbbb");
- LCD_Write_String(3,0,dis0);
- }
- else if((UID[0]==lqcID[0])&&(UID[1]==lqcID[1]))
- {
- lslFlag ='Y';
- sprintf(dis0,"cccccccc");
- LCD_Write_String(3,0,dis0);
- }
- }
-
- }
- if(disFlag == 1)
- {
- manNum=0;
- if(lzdFlag=='Y')
- {manNum=manNum+1;}
- if(lhjFlag=='Y')
- {manNum=manNum+1;}
- if(lslFlag=='Y')
- {manNum=manNum+1;}
- sprintf(dis2,"ID:");
- LCD_Write_String(0,0,dis2);
- sprintf(dis1,"Num:%d ",(int)manNum);
- LCD_Write_String(0,1,dis1);
- disFlag =0;
- }
- }
- }
- void Usart() interrupt 4
- {
- if(RI)
- {
- RI=0;
- }
- if(TI)
- TI=0;
- }
- void Init_Timer0(void)
- {
- TMOD |= 0x01;
- TH0=(65536-20000)/256;
- TL0=(65536-20000)%256;
- EA=1;
- ET0=1;
- TR0=1;
- }
- void Timer0_isr(void) interrupt 1
- {
- TH0=(65536-20000)/256;
- TL0=(65536-20000)%256;
- time_20ms++;
- if(time_20ms%10==0)
- {disFlag = 1 ;}
- }
- void uartSendByte(unsigned char dat)
- {
- unsigned char time_out;
- time_out=0x00;
- SBUF = dat;
- while((!TI)&&(time_out<100))
- { time_out++;delay(100);}
- TI = 0;
- }
- void uartSendStr(unsigned char *s,unsigned char length)
- {
- unsigned char NUM;
- NUM=0x00;
- while(NUM<length)
- { uartSendByte(*s);
- s++;
- NUM++;
- }
- }
复制代码
|