RC500读卡单片机源程序

发布者:RoboPilot最新更新时间:2022-12-26 来源: zhihu关键字:RC500  读卡  单片机  源程序 手机看文章 扫描二维码
随时随地手机看文章

#define __MAIN_H

#include "main.h"

#undef __MAIN_H


#include

#include

#include

#include

#include "AT89S52.h"

#include "RICReg.H"

#include "m500a.H"

#include "PICCCmdConst.h"

#include "MfErrNo.h"


#define uchar unsigned char

#define uint unsigned int

//#define DataPort P0 // 并行数据接口

//pin define mcu 引脚定义

sbit RC500RST = P1^0; //复位 rc500

sbit RC500CS = P1^1; //rc500片选

sbit CARD_LED = P1^2; //系统LED灯

sbit SPEAKER = P1^3; //扬声器


bit bcard;


///////////////////////////////////////////////////////////////////////

// main

///////////////////////////////////////////////////////////////////////

void main(void)

{ //波特率

uchar baud;

InitSystem(); //系统初始化

delay_10ms(5); //延时

while (1)

{ delay_10ms(5);

if (bCmd) //判断执行命令标志位

{ bCmd = FALSE; //命令执行标志位清零

if(RevBuffer[0]==0x10) //RevBuffer[0]为0x10

{

RevBuffer[2]=RevBuffer[1];

cmd_ok();

SPEAKER=1;

CARD_LED=ON;

delay_10ms(RevBuffer[2]); //闪灯并鸣笛

SPEAKER=0;

CARD_LED=OFF;

}

else if(RevBuffer[0]==0x11) //RevBuffer[0]为0x11

{ switch(RevBuffer[1])

{ case 0:

baud=BAUD_9600;

break;

case 1:

baud=BAUD_14400;

break;

case 2:

baud=BAUD_19200;

break;

case 3:

baud=BAUD_28800;

break;

case 4:

baud=BAUD_38400;

break;

case 5:

baud=BAUD_57600;

break;

case 6:

baud=BAUD_115200;

break;

default:

baud=BAUD_19200;

break;

}

cmd_ok();

delay_10ms(5);

TR1 = 0;

TH1 = baud;

TL1 = TH1;

delay_10ms(2);

TR1 = TRUE;

}

else

uart_process(); //调用串口处理函数

}

}

}


///////////////////////////////////////////////////////////////////////

// 系统初始化

///////////////////////////////////////////////////////////////////////

void InitSystem(void)

{

RC500CS=0; //选通RC500

RC500RST=0; //复位


ET2 = FALSE; // 定时器2关中断

T2CON = 0x04; //TR2=1,TIMER2,auto reload

PCON = 0x80;

SCON = 0x70; // SMOD = 1;

TMOD = 0x21; //TMOD = 0x22;


TH1 = BAUD_19200; //定时器1产生波特率

TL1 = TH1;

TR1 = TRUE; // HT1

ET1=FALSE;

IT0 =TRUE; // Config ext0 as edge trigger for RC500

EX0 =TRUE; // Enable ext0 interrupt for RC500

EA = TRUE; // Enable all interrupts

TR2=FALSE; //Close T2

IP=0x10; //interrupt priority

ES = TRUE;

bCmd=FALSE; //

beep(1); //

splash(1);

MfConfig(); //配置RC500

}


///////////////////////////////////////////////////////////////////////

// uart 中断服务程序

///////////////////////////////////////////////////////////////////////

void Uart_ISR(void) interrupt 4 using 1

{

uchar len, i;

uint j=0;

if(RI) //如果是接收中断

{ len=SBUF; //读取接收到的数据

RI=0; //清除接收中断标志

for(i=0;i{

while(!RI)

{

j++;

if(j>1000)

{break; //超时退出

}

}

if(j<1000)

{ RevBuffer[i]=SBUF; //读取接收到的数据

RI=0;

j=0;

}

else

{break; }//超时退出

}

if(i==len)

{ REN=0; // 循环缓冲区环回

bCmd=TRUE; //命令标志位bCmd置位

}

}

else if(!RI && TI) //如果 RI=0,TI=1

{

TI=0; //清除发送中断标志位

len=RevBuffer[0]; //发送RevBuffer长度个数据

for(i=0;i{ SBUF=RevBuffer[i];

while(!TI)

;

TI=0;

}

REN=1;

}

}



///////////////////////////////////////////////////////////////////////

// uart process routine

///////////////////////////////////////////////////////////////////////

void uart_process(void)

{

uchar cmd;

uchar status;

cmd = RevBuffer[0];

switch(cmd)

{

case 0x20: // Request ,?

status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]);

if(status!=0)

{

status=M500PiccCommonRequest(RevBuffer[1],&RevBuffer[2]);

if(status!=0)

{

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

}

}

if(RevBuffer[2]==2)

cardtype=mifare1S70; // Mifare1 S70 

else if(RevBuffer[2]==4)

cardtype=mifare1S50; // Mifare1 S50 

else if(RevBuffer[2]==16)

cardtype=mifarelight; // Mifare Light 

else

cardtype=unknowncard;

RevBuffer[0]=3; // 

RevBuffer[1]=status;

break;

case 0x21: //CardSnr

status = M500PiccCascAnticoll(0,&RevBuffer[2]);

if(status!=0) //ア?

{

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

}

memcpy(CardSnr,&RevBuffer[2],4);

RevBuffer[0]=5;

RevBuffer[1]=status;

break;

case 0x22: // Select Card

status=M500PiccCascSelect(CardSnr,&RevBuffer[2]);

if(status!=MI_OK)

{

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

}


RevBuffer[0]=3;

RevBuffer[1]=status;

break;

case 0x23: // Key loading into the MF RC500's EEPROM

status=M500PiccAuthE2(RevBuffer[1],CardSnr,RevBuffer[2],RevBuffer[3]);

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

case 0x24: // Key loading into the MF RC500's EEPROM

status=M500PcdLoadKeyE2(RevBuffer[1],RevBuffer[2],&RevBuffer[3]);

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

case 0x25: // Read the mifare card

status=M500PiccRead(RevBuffer[1],&RevBuffer[2]);

if(status==0)

{

if(cardtype==mifare1S50||cardtype==mifare1S70)

RevBuffer[0]=17;

else if(cardtype==1)

RevBuffer[0]=9;

else

RevBuffer[0]=16;

}

else

{

RevBuffer[0]=1;

}

RevBuffer[1]=status;

break;

case 0x26: // Write the mifare card

status=M500PiccWrite(RevBuffer[1],&RevBuffer[2]);

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

case 0x27:

status = M500PiccValue(RevBuffer[1],RevBuffer[2],&RevBuffer[3],RevBuffer[7]);

RevBuffer[0]=1;

RevBuffer[1]=status;

break;

case 0x28: //

status=M500PiccHalt();

RevBuffer[0]=1; //

RevBuffer[1]=status;

break;

case 0x29: //power down

SetBitMask(RegControl,0x10); // PowerDown = 1

cmd_ok();

break;

case 0x30: //

ClearBitMask(RegControl,0x10); // PowerDown = 0

cmd_ok();

break;

default: //

RevBuffer[0]=1;

RevBuffer[1]=1;

break;

}

}


///////////////////////////////////////////////////////////////////////

//命令执行标志置1

///////////////////////////////////////////////////////////////////////

void cmd_ok(void)

{

RevBuffer[0]=1;

RevBuffer[1]=0;

}

///////////////////////////////////////////////////////////////////////


///////////////////////////////////////////////////////////////////////

// write address

///////////////////////////////////////////////////////////////////////

void WriteRawIO(uchar Address,uchar value)

{

XBYTE[Address]=value;

}


///////////////////////////////////////////////////////////////////////

// read address

///////////////////////////////////////////////////////////////////////

uchar ReadRawIO(uchar Address)

{

return XBYTE[Address];

}


///////////////////////////////////////////////////////////////////////

// G E N E R I C W R I T E

///////////////////////////////////////////////////////////////////////

void WriteIO(uchar Address, uchar value)

{

WriteRawIO(0x00,GetRegPage(Address));

WriteRawIO(Address,value);

}


///////////////////////////////////////////////////////////////////////

// G E N E R I C R E A D

// (EEPROM)

///////////////////////////////////////////////////////////////////////

uchar ReadIO(uchar Address)

{

WriteRawIO(0x00,GetRegPage(Address));

return ReadRawIO(Address);

}


///////////////////////////////////////////////////////////////////////

// set RC500 timeout

///////////////////////////////////////////////////////////////////////

void M500PcdSetTmo(uchar 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(uchar cmd,

volatile uchar data *rcv,

MfCmdInfo idata *info)

{

char idata status = MI_OK;

char idata tmpStatus ;

uchar idata lastBits;

unsigned int idata timecnt = 0;

uchar idata irqEn = 0x00;

uchar idata waitFor = 0x00;

uchar idata timerCtl = 0x00;


WriteIO(RegInterruptEn,0x7F); //Clear Int

WriteIO(RegInterruptRq,0x7F);

WriteIO(RegCommand,PCD_IDLE); // (0x00) No action: cancel current command


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;

}


///////////////////////////////////////////////////////////////////////

// set bit mask

///////////////////////////////////////////////////////////////////////

char SetBitMask(uchar reg,uchar mask)

{

char idata tmp = 0x00;


tmp = ReadIO(reg);

WriteIO(reg,tmp | mask); // set bit mask

return 0x00;

}


///////////////////////////////////////////////////////////////////////

//清除位掩码

///////////////////////////////////////////////////////////////////////

char ClearBitMask(uchar reg,uchar mask)

{

char idata tmp = 0x00;

[1] [2] [3]
关键字:RC500  读卡  单片机  源程序 引用地址:RC500读卡单片机源程序

上一篇:串行7段LED显示应用程序
下一篇:用51单片机控制的LED流水灯设计

热门资源推荐
热门放大器推荐
小广播
设计资源 培训 开发板 精华推荐

最新单片机文章
何立民专栏 单片机及嵌入式宝典

北京航空航天大学教授,20余年来致力于单片机与嵌入式系统推广工作。

换一换 更多 相关热搜器件

 
EEWorld订阅号

 
EEWorld服务号

 
汽车开发圈

电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号 Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved