#include "reg52.h"
#include <string.h>
#include <intrins.h>
#include "main.h"
#include "slrc1702.h"
#include "iso14443a.h"
//#include "stdio.h"
bit g_bReceOk; //正确接收到上位机指令标志
bit g_bRc1702Ok; //RC1702复位正常标志
unsigned int g_cReceNum,tmp; //接收到上位机的字节数
unsigned int data g_cCommand; //接收到的命令码
unsigned char data g_cSNR[4]; //M1卡序列号
unsigned char g_cIcdevH; //设备标记
unsigned char g_cIcdevL; //设备标记
unsigned char g_cFWI; //
unsigned char g_cCidNad,flag; //
unsigned char idata g_cReceBuf[64]; //和上位机通讯时的缓冲区
unsigned char tt1[2];
unsigned char blockdata[16];
unsigned char Password[6]={0x77,0x77,0x77,0x77,0x77,0x77};
struct TranSciveBuffer{unsigned char MfCommand;
unsigned int MfLength;
unsigned char MfData[64];
};
char M500PcdMfOutSelect(unsigned char type)
{
WriteRawRC(RegMfOutSelect,type&0x7);
return MI_OK;
}
char M500PiccRequest(unsigned char req_code, // request code ALL = 0x52
// or IDLE = 0x26
unsigned char *atq) // answer to request
{
return PcdRequest(req_code,atq);
}
char M500PiccAnticoll (unsigned char *snr)
{
return PcdAnticoll(snr); // first cascade level
}
char M500PiccSelect(unsigned char *snr,
unsigned char *sak)
{
return PcdSelect(snr,sak); // first cascade level
}
void main( )
{
unsigned counter,i,status1,counter1;
unsigned char mfout=2;
unsigned char cardserialno[4];
unsigned char *sak1,*pkeys;
pkeys=&Password[6];
InitializeSystem( );
Rc1702Ready( );
PcdConfigISOType( 'A' );
PcdReadE2(8,4,g_cSNR);
M500PcdMfOutSelect(mfout);
while(1)
{
/* status1 = M500PiccRequest(PICC_REQALL, tt1);//寻卡
if (status1==MI_OK)//如果找到卡
{
status1=M500PiccAnticoll(cardserialno);
}
if (status1==MI_OK)
{
status1=M500PiccSelect(cardserialno,sak1);//选择卡
}
Password[0]=0x77;
Password[1]=0x77;
Password[2]=0x77;
Password[3]=0x77;
Password[4]=0x77;
Password[5]=0x77;
//授权
if (status1==MI_OK)
// {
// if(MI_OK==ChangeCodeKey(&Password,pkeys))
// {
// if(MI_OK==PcdAuthKey(Password))
{
status1=PcdAuthState(PICC_AUTHENT1A,0x0c,&Password,sak1);
}
*/
// } }
/*if(status1==MI_OK)
{
status1=PcdLoadKeyE2(PICC_AUTHENT1A,0X0C);
if(status1==MI_OK)
{
status1 = PcdAuthState(PICC_AUTHENT1A, 0X0C, sak1);
}
}*/
/* if(status1==MI_OK)
{
status1=PcdRead(0x0c,blockdata); //读数据
// sond=~sond;
for(counter1=0;counter1<16;counter1++)
{
i = blockdata[counter1]<<4>>4;//因为printf(%x) 只可以16进制显示整数,所以必须把uchar左移4位再右移4位,下同
}
}
if(status1==MI_OK)
{
}
} */
status1=PcdRequest(PICC_REQALL, tt1);
if(status1==MI_OK)
{
status1=PcdRequest(PICC_REQALL, tt1);
}
else
{
continue;//提前终止此轮循环, 并跳到while入口处
}
if(status1==MI_OK)
{
status1=PcdAnticoll(g_cSNR); //防冲撞
}
else
{
continue;//提前终止此轮循环, 并跳到while入口处
}
if(status1==MI_OK)
{
status1=PcdSelect(g_cSNR,sak1);
}
else
{
continue;//提前终止此轮循环, 并跳到while入口处
}
if(status1==MI_OK) //下载密码 把7777777777下载下去
{
status1=M500PcdLoadKeyE2(PICC_AUTHENT1A,0,&pkeys); // Key loading into the fm1702sl EEPROM
}
if(status1==MI_OK) //校检密码
{
status1=PcdLoadKeyE2(PICC_AUTHENT1A,0); //将存在fm1702sl的EEPROM中的密钥匙调入fm1702sl的FIFO
}
if(status1==MI_OK)
{
status1 = PcdAuthState(PICC_AUTHENT1A, 0X0C, g_cSNR); //功能:用存放fm1702sl的FIFO中的密钥和卡上的密钥进行验证
}
else
{
continue;//提前终止此轮循环, 并跳到while入口处
}
if(status1==MI_OK)
{
status1=PcdRead(0x0c,blockdata); sond = 0; //蜂鸣器响一声,表示fm1702复位成功
DelayMs(200);
DelayMs(200);
sond = 1;
}
}
while(1){};
}
/////////////////////////////////////////////////////////////////////
//系统初始化
/////////////////////////////////////////////////////////////////////
void InitializeSystem()
{
TMOD &= 0x0F;
TMOD |= 0x21;
//PCON |= 0x80;
//SCON = 0x50;
//TH1 = TL1 = BOUND9600;
TR1=1;
P0 = P1 = P2 = P3 = 0xFF;
IE |= 0x90;
}
void time0() interrupt 1
{
TH0=(65536-50)/256;
TL0=(65536-50)%256;
tmp++;
}
/////////////////////////////////////////////////////////////////////
//初始化fm1702
/////////////////////////////////////////////////////////////////////
void Rc1702Ready()
{
char status;
DelayMs(100);
DelayMs(100);
DelayMs(100);
DelayMs(100);
DelayMs(100);
DelayMs(100);
DelayMs(100);
status = PcdReset();
if(status != MI_OK)
{
DelayMs(10);
status = PcdReset();
}
if(status != MI_OK)
{
DelayMs(10);
status = PcdReset();
}
if(status == MI_OK)
{
g_bRc1702Ok = 1;
sond = 0; //蜂鸣器响一声,表示fm1702复位成功
DelayMs(200);
DelayMs(200);
sond = 1;
}
}
/////////////////////////////////////////////////////////////////////
//用T2做延时子程序
/////////////////////////////////////////////////////////////////////
void DelayMs(unsigned int _MS)
{
RCAP2LH = RCAP2_1ms;
T2LH = RCAP2_1ms;
ET2 = 0; // Disable timer2 interrupt
T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
TR2 = 1;
while (_MS--)
{ while (!TF2);TF2 = 0; }
TR2 = 0;
}
void Delay_50us(unsigned char _50us)
{
RCAP2LH = RCAP2_50us;
T2LH = RCAP2_50us;
ET2 = 0; // Disable timer2 interrupt
T2CON = 0x04; // 16-bit auto-reload, clear TF2, start timer
while (_50us--)
{ while (!TF2);TF2 = 0; }
TR2 = 0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
想请教一下,我知道协议中command的数据帧,如何确定需要通过串口发送的指令呢?
一周热门 更多>