#define M500_GLOBALS
#define songti 0x00l
#define heiti 0x40650l
#define kaiti 0x804A0l
#define yuanti 0xC02F0l
//#include <absacc.h>
#include "INTRINS.H"
#include "stdio.h"
#include "string.h"
#include "REG52.H"
#include "Mfreg500.h"
#include "MfErrNo.h"
#include "M500AuC.h"
//#include "Lcd_Driver.h"
//#include "W25X80_Flash.h"
#include "LCD_W25x80.c"
#include "STC12C_EEPROM.c"
#define GetRegPage(addr) (0x80 | (addr>>3))
#define nocard 0
#define mifare1 1
#define mifarepro 2
#define mifarelight 3
#define unknowncard 4
#define NO_TIMER2 1
#define uchar unsigned char
#define uint unsigned int
bit keyFlag=0,readFlag,S1_Flag=0,S2_Flag=0,LCD_Clr_Flag=0;
uchar xdata rev[96*2]={0};
//uchar xdata info[5][48]={0};
uchar *rev0=rev;
unsigned char flagcount=0,SectrNO=14,status1=1;
//图书信息
struct book
{
uchar size; //图书信息长度
uchar booknumsize; //图书编号长度
uchar booknum[7]; /*图书编号 */
uchar isbnnum; //图书号总字节数n1
uchar isbn[13]; /*图书号(ISBN号)*/
uchar authnum; //书作者总字节数n3
uchar author[30]; /*图书作者 */
uchar pubnum; //出版社总字节数n4
uchar publisher[30]; /*出版社*/
uchar namenum; //书名总字节数n2
uchar name[50]; /*图书名*/
};
struct book xdata bookinf;
//uchar xdata snd[30];
//uchar xdata *rev;
//sfr AUXR=0x8e; /*如果不需设置AUXR就不用声明AUXR*/
///////////////////////////////////////////////////////////////////////
// 往一个地址写一个数据
///////////////////////////////////////////////////////////////////////
void WriteRawIO(unsigned char Address,unsigned char value)
{
//_nop_();
//_nop_();
ALE=0;
RC500NCS=1;
RC500NWR=1;
ALE=1;
P0=Address;
ALE=0;
RC500NCS=0;
P0=value;
RC500NWR=0;
_nop_();
RC500NWR=1;
RC500NCS=1;
//_nop_();
//_nop_();
}
///////////////////////////////////////////////////////////////////////
// 从一个地址读出一个数据
///////////////////////////////////////////////////////////////////////
unsigned char ReadRawIO(unsigned char Address)
{
unsigned char value;
//_nop_();
//_nop_();
ALE=0;
RC500NCS=1;
RC500NRD=1;
ALE=1;
P0=Address;
ALE=0;
RC500NCS=0;
P0=0xFF;
_nop_();
RC500NRD=0;
value=P0;
RC500NRD=1;
RC500NCS=1;
//_nop_();
//_nop_();
return value;
}
///////////////////////////////////////////////////////////////////////
// 往一个地址写一个数据(EEPROM)
///////////////////////////////////////////////////////////////////////
void WriteIO(unsigned char Address, unsigned char value)
{
WriteRawIO(0x00,GetRegPage(Address));
WriteRawIO(Address,value);
}
///////////////////////////////////////////////////////////////////////
// 从一个地址读出一个数据(EEPROM)
///////////////////////////////////////////////////////////////////////
unsigned char ReadIO(unsigned char Address)
{
WriteRawIO(0x00,GetRegPage(Address));
return ReadRawIO(Address);
}
///////////////////////////////////////////////////////////////////////
// 设置定时时间
///////////////////////////////////////////////////////////////////////
void M500PcdSetTmo(unsigned char tmoLength)
{
switch(tmoLength)
{
case 1:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,0x6a);
break;
case 2:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,0xa0);
break;
case 3:
WriteIO(RegTimerClock,0x09);
WriteIO(RegTimerReload,0xa0);
break;
case 4:
WriteIO(RegTimerClock,0x09);
WriteIO(RegTimerReload,0xff);
break;
case 5:
WriteIO(RegTimerClock,0x0b);
WriteIO(RegTimerReload,0xff);
break;
case 6:
WriteIO(RegTimerClock,0x0d);
WriteIO(RegTimerReload,0xff);
break;
case 7:
WriteIO(RegTimerClock,0x0f);
WriteIO(RegTimerReload,0xff);
break;
default:
WriteIO(RegTimerClock,0x07);
WriteIO(RegTimerReload,tmoLength);
break;
}
}
///////////////////////////////////////////////////////////////////////
// Request Command defined in ISO14443(Mifare)
///////////////////////////////////////////////////////////////////////
char M500PcdCmd(unsigned char cmd,volatile unsigned char data *rcv,MfCmdInfo idata *info)
{
char idata status = MI_OK;
char idata tmpStatus ;
unsigned char idata lastBits;
unsigned int idata timecnt = 0;
unsigned char idata irqEn = 0x00;
unsigned char idata waitFor = 0x00;
unsigned char idata timerCtl = 0x00;
WriteIO(RegInterruptEn,0x7F);
WriteIO(RegInterruptRq,0x7F);
WriteIO(RegCommand,PCD_IDLE);
FlushFIFO();
MpIsrInfo = info;
MpIsrOut = rcv;
info->irqSource = 0x00;
switch(cmd)
{
case PCD_IDLE:
irqEn = 0x00;
waitFor = 0x00;
break;
case PCD_WRITEE2:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_READE2:
irqEn = 0x07;
waitFor = 0x04;
break;
case PCD_LOADCONFIG:
case PCD_LOADKEYE2:
case PCD_AUTHENT1:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_CALCCRC:
irqEn = 0x11;
waitFor = 0x10;
break;
case PCD_AUTHENT2:
irqEn = 0x04;
waitFor = 0x04;
break;
case PCD_RECEIVE:
info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
irqEn = 0x06;
waitFor = 0x04;
break;
case PCD_LOADKEY:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSMIT:
irqEn = 0x05;
waitFor = 0x04;
break;
case PCD_TRANSCEIVE:
info->nBitsReceived = -(ReadIO(RegBitFraming) >> 4);
irqEn = 0x3D;
waitFor = 0x04;
break;
default:
status = MI_UNKNOWN_COMMAND;
}
if (status == MI_OK)
{
irqEn |= 0x20;
waitFor |= 0x20;
timecnt=2300;
WriteIO(RegInterruptEn,irqEn | 0x80);
WriteIO(RegCommand,cmd);
while (!(MpIsrInfo->irqSource & waitFor||!(timecnt--)));
WriteIO(RegInterruptEn,0x7F);
WriteIO(RegInterruptRq,0x7F);
SetBitMask(RegControl,0x04);
WriteIO(RegCommand,PCD_IDLE);
if (!(MpIsrInfo->irqSource & waitFor))
{
status = MI_ACCESSTIMEOUT;
}
else
{
status = MpIsrInfo->status;
}
if (status == MI_OK)
{
if (tmpStatus = (ReadIO(RegErrorFlag) & 0x17))
{
if (tmpStatus & 0x01)
{
info->collPos = ReadIO(RegCollPos);
status = MI_COLLERR;
}
else
{
info->collPos = 0;
if (tmpStatus & 0x02)
{
status = MI_PARITYERR;
}
}
if (tmpStatus & 0x04)
{
status = MI_FRAMINGERR;
}
if (tmpStatus & 0x10)
{
评论5