⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rc500.c

📁 RC500的全部操作,读卡
💻 C
📖 第 1 页 / 共 3 页
字号:
#include <REGX52.H>

#include <absacc.h>
#include <string.h>

#include <RC500Err.h>
#include <RC500Reg.h>
#include <RC500.h>

typedef struct 
         {
            unsigned char  cmd;           
            char           status;        
            unsigned char  nBytesSent;   
            unsigned char  nBytesToSend; 
            unsigned char  nBytesReceived;
            unsigned short nBitsReceived; 
            unsigned char  irqSource;     
            unsigned char  collPos;       
                                          
         } MfCmdInfo;
#define ResetInfo(info)    \
            info.cmd            = 0;\
            info.status         = MI_OK;\
            info.irqSource      = 0;\
            info.nBytesSent     = 0;\
            info.nBytesToSend   = 0;\
            info.nBytesReceived = 0;\
            info.nBitsReceived  = 0;\
            info.collPos        = 0;

// 往一个地址写一个数据
//void WriteRawIO(unsigned char Address,unsigned char value);
// 从一个地址读出一个数据
//unsigned char ReadRawIO(unsigned char Address);
// 往一个地址写一个数据(EEPROM)
//void WriteIO(unsigned char Address, unsigned char value);
// 从一个地址读出一个数据(EEPROM)
// unsigned char ReadIO(unsigned char Address);
// 设置定时时间
void M500PcdSetTmo(unsigned char tmoLength);
// Request Command defined in ISO14443(Mifare)
char  M500PcdCmd(unsigned char cmd,
               volatile unsigned char data *rcv,
               MfCmdInfo idata *info);
// 置一个bit
char SetBitMask(unsigned char reg,unsigned char mask);
// 清一个bit
char ClearBitMask(unsigned char reg,unsigned char mask);
//
void FlushFIFO(void);
// Value format operations for Mifare Standard card ICs
char M500PiccValue(unsigned char dd_mode, 
                   unsigned char addr, 
                   unsigned char *value,
                   unsigned char trans_addr);
// Set card in HALT-state
// 终止卡的操作
char M500PiccHalt(void);
// Reset the MF RC500 
//char M500PcdReset(void);
// Configures the MF RC500
char M500PcdConfig(void);
// Key loading into the MF RC500's EEPROM
char M500PcdLoadKeyE2(unsigned char key_type,
                       unsigned char sector,
                       unsigned char *uncoded_keys);
// Write the MF RC500's EEPROM
char PcdWriteE2(unsigned int startaddr,
                unsigned char length,
                unsigned char* _data);
// Select Command defined in ISO14443(MIFARE)
char M500PcdMfOutSelect(unsigned char type);
// Request Command defined in ISO14443(MIFARE)
// Request,Anticoll,Select,return CardType(2 bytes)+CardSerialNo(4 bytes)    
// 寻卡,防冲突,选择卡    返回卡类型(2 bytes)+ 卡系列号(4 bytes)
char M500PiccCommonRequest(unsigned char req_code,unsigned char *atq);
// Cascaded Anti-Collision Command defined in ISO14443(MIFARE)
// 防冲突 读卡的系列号 MLastSelectedSnr
char M500PiccCascAnticoll (unsigned char bcnt,unsigned char *snr);
// Cascaded Select command defined in ISO14443(MIFARE)
// 选择卡 Select Card
char M500PiccCascSelect(unsigned char *snr,unsigned char *sak);
// Key loading into the MF RC500's EEPROM
// 校验卡密码(E2)
char M500PiccAuthE2( unsigned char auth_mode,unsigned char *snr,      
                     unsigned char keynr,unsigned char block);
// Authentication key coding
char M500HostCodeKey(  unsigned char *uncoded, unsigned char *coded);
// Authentication with direct key loading from the uC
// 直接校验密码
//char M500PiccAuthKey(  unsigned char auth_mode,
//                       unsigned char *snr,       
//                       unsigned char *keys,      
//                       unsigned char block);
//
char M500PiccAuthState( unsigned char auth_mode,unsigned char *snr,unsigned char block);
// Read the mifare card
// 读卡
char M500PiccRead(unsigned char addr,unsigned char *_data);
// Write the mifare card
// 写卡  下载密码
char M500PiccWrite( unsigned char addr,unsigned char *_data);
// Reset Rf Card
char M500PcdRfReset(unsigned char ms);
// Config
char MfConfig(void);

static   volatile MfCmdInfo idata   MInfo; 
//static   volatile MfCmdInfo    *MpIsrInfo = 0; 
static   volatile MfCmdInfo    *MpIsrInfo; 
//static   volatile unsigned char  *MpIsrOut = 0; 
static   volatile unsigned char  *MpIsrOut;

unsigned char SerBuffer[20];

void delay_100us()
{
	unsigned char i=100;
	while(i--);
}

void delay_ms(unsigned int ms)
{
	unsigned char i;
	while(ms--)
	{
		i=112;
		while(i--);
	}

}

///////////////////////////////////////////////////////////////////////
// 往一个地址写一个数据
///////////////////////////////////////////////////////////////////////
void WriteRawIO(unsigned char Address,unsigned char value)
{
	XBYTE[Address]=value;
}

///////////////////////////////////////////////////////////////////////
// 从一个地址读出一个数据
///////////////////////////////////////////////////////////////////////
unsigned char ReadRawIO(unsigned char Address)
{
	return XBYTE[Address];
}

///////////////////////////////////////////////////////////////////////
// 往一个地址写一个数据(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=1000;
        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)   
                {
                    FlushFIFO();
                    status = MI_OVFLERR;
                }
 	            if (tmpStatus & 0x08) 
	            {
                    status = MI_CRCERR;
	            }	
                if (status == MI_OK)
                    status = MI_NY_IMPLEMENTED;
            }
            if (cmd == PCD_TRANSCEIVE)
            {
                lastBits = ReadIO(RegSecondaryStatus) & 0x07;
                if (lastBits)
                    info->nBitsReceived += (info->nBytesReceived-1) * 8 + lastBits;
                else
                    info->nBitsReceived += info->nBytesReceived * 8;
            }
        }
        else
        {
            info->collPos = 0x00;
        }
    }
    MpIsrInfo = 0;         
    MpIsrOut  = 0;
    return status;
}   

///////////////////////////////////////////////////////////////////////
// 置一个bit
///////////////////////////////////////////////////////////////////////
char SetBitMask(unsigned char reg,unsigned char mask)  
{
    char idata tmp = 0x00;

    tmp = ReadIO(reg);
    WriteIO(reg,tmp | mask);  // set bit mask
    return 0x00;
}

///////////////////////////////////////////////////////////////////////
// 清一个bit
///////////////////////////////////////////////////////////////////////
char ClearBitMask(unsigned char reg,unsigned char mask)  
{
    char idata tmp = 0x00;

    tmp = ReadIO(reg);
    WriteIO(reg,tmp & ~mask);  // clear bit mask
    return 0x00;
}

///////////////////////////////////////////////////////////////////////
//清除FIFO
///////////////////////////////////////////////////////////////////////
void FlushFIFO(void)
{  
    SetBitMask(RegControl,0x01);
}

///////////////////////////////////////////////////////////////////////
// Value format operations for Mifare Standard card ICs
///////////////////////////////////////////////////////////////////////
/*char M500PiccValue(unsigned char dd_mode, 
                   unsigned char addr, 
                   unsigned char *value,
                   unsigned char trans_addr)
{
    char status = MI_OK;

    M500PcdSetTmo(1); 
    ResetInfo(MInfo);   
    SerBuffer[0] = dd_mode;      
    SerBuffer[1] = addr;
    MInfo.nBytesToSend = 2;
    status = M500PcdCmd(PCD_TRANSCEIVE,
                       SerBuffer,
                       &MInfo);

    if (status != MI_NOTAGERR)   
    {
        if (MInfo.nBitsReceived != 4)  

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -