RFModuleFunc.C
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:10k
- #include <Reg52.H>
- #include <string.h>
- #include <INTRINS.H>
- #include "RfModuleFunc.H"
- //各延时定义
- #define DELAY_TIME0 100
- #define DELAY_TIME1 2500
- #define DELAY_TIME2 3000
- #define DELAY_TIME3 55000
- #define SPI_CMD_GET_VERSION 0x32 //读机内版本号
- #define SPI_CMD_GET_INFO 0x34 //读取RF模块固件信息序列号
- #define SPI_CMD_SET_CONTROL_BIT 0x50 //设置RC500中的控制位
- #define SPI_CMD_CLR_CONTROL_BIT 0x51 //清除RC500中的控制位
- #define SPI_CMD_BUZZER 0x60 //输出驱动无源蜂鸣器信号
- //Mifare模块基本命令部分
- #define SPI_CMD_MIFARE_CONFIG 0x52 //复位且配置RC500
- #define SPI_CMD_MIFARE_CLOSE 0x3F //关闭RC500,使读写模块进入掉电模式
- #define SPI_CMD_MIFARE_REQUEST 0x41 //寻卡
- #define SPI_CMD_MIFARE_ANTICOLL 0x42 //防冲突
- #define SPI_CMD_MIFARE_ANTICOLL2 0x71 //防冲突2,允许多张卡进入
- #define SPI_CMD_MIFARE_SELECT 0x43 //选择卡
- #define SPI_CMD_MIFARE_AUTH 0x44 //开始校验密钥
- #define SPI_CMD_MIFARE_AUTH2 0x72 //可选择密钥区验证
- #define SPI_CMD_MIFARE_AUTHKEY 0x73 //直接校验密钥
- #define SPI_CMD_MIFARE_HALT_CARD 0x45 //停止当前卡
- #define SPI_CMD_MIFARE_READ 0x46 //读卡
- #define SPI_CMD_MIFARE_WRITE 0x47 //写卡
- #define SPI_CMD_MIFARE_INC 0x48 //增值操作
- #define SPI_CMD_MIFARE_DEC 0x49 //减值操作
- #define SPI_CMD_MIFARE_RESORE 0x4A //将所访问单元块的字节数保存在卡的内部寄存器中
- #define SPI_CMD_MIFARE_TRANS 0x4B //将卡内部寄存器的内容转输到访问快的字节数
- #define SPI_CMD_MIFARE_VALUE 0x70 //增减值操作
- #define SPI_CMD_MIFARE_LOADKEY 0x4C //改变存贮在EEPROM 中的密钥
- #define SPI_CMD_MIFARE_RESET_CARD 0x4E //关闭天线输出数ms 使卡复位
- #define SPI_CMD_MIFARE_AUTHE2KEY 0x56 //从Mifare模块的E2校验密钥
- #define SPI_CMD_MIFARE_CHECK_WRITE 0x53 //将所传送的数据和上一次所写的数据进行比较
- #define SPI_CMD_MIFARE_READ_E2 0x61 //读RC500 内EEPROM 的内容
- #define SPI_CMD_MIFARE_WRITE_E2 0x62 //写数据到RC500 内EEPROM
- //卡操作命令部分
- #define SPI_CMD_SET_CARD_APP_SECTOR 0x80 //设置卡扇区应用参数
- #define SPI_CMD_GET_CARD_APP_SECTOR 0x81 //读取卡扇区应用参数
- #define SPI_CMD_SET_BASE_KEY 0x82 //设置基密钥
- #define SPI_CMD_GET_BASE_KEY 0x83 //读取基密钥
- #define SPI_CMD_GET_CARD_KEY 0x84 //取得卡密钥
- #define SPI_CMD_IS_CARD_IN 0x85 //是否有卡进入读卡区
- #define SPI_CMD_READ_COMPANY_INFO 0x86 //读卡上公司信息
- #define SPI_CMD_READ_CARDHOLDER_INFO 0x87 //读卡上持卡人信息
- #define SPI_CMD_READ_BUSINESS_INFO 0x88 //读卡上交易数据(含卡恢复流程)
- #define SPI_CMD_CONSUME 0x89 //消费
- #define SPI_CMD_FILL 0x8A //充值
- #define SPI_CMD_READ_ORDER_DINNER_INFO 0x8B //读卡上订餐信息
- #define SPI_CMD_WRITE_ORDER_DINNER_INFO 0x8C //写订餐信息到卡上
- #define SPI_CMD_READ_TIME_COUNT_INFO 0x8D //读卡上计时消费信息
- #define SPI_CMD_WRITE_TIME_COUNT_INFO 0x8E //写计时消费信息到卡上
- #define SPI_CMD_SET_CARD_STATE 0x8F //设置卡状态
- //============================管脚定义====================================
- sbit RF_RST = P2^6;
- sbit RF_CS = P3^2; //INT0
- sbit RF_SCLK = P3^5;
- sbit RF_SDAT = P3^4;
- #define RF_OPEN() IE0=0;EX0=1;
- #define RF_CLOSE() EX0=0;
- //============================常量定义================================
- #define RF_BUFFER_MAX_ZIAE 64 //不能超255,否则需加长度字节数
- unsigned char pdata Rf_Buffer[RF_BUFFER_MAX_ZIAE];
- bit newdata = 0;
- //BCC校验计算
- unsigned char GetBCC(unsigned char *Buffer, unsigned char Len)
- {
- unsigned char value=0;
- while(Len--)
- {
- value ^= *Buffer;
- Buffer++;
- }
- return ~value;
- }
- //外部提供延时1ms的子程序
- //extern Delay1ms(unsigned char _1ms);
- extern void do_nop(unsigned char Count);
- void Rf_Int0(void) interrupt 0 using 1 //rf_rcv(Rf_Buffer);
- {
- unsigned char Rf_count;
- unsigned char i,j,ch;
- RF_SCLK = 0;
- RF_SDAT = 1;
- for(i=10;i--;);//delay;
- Rf_count= 0;
- while(!RF_CS)
- {
- for(i=0;i<8;i++)
- {
- RF_SCLK = 1;ch <<=1;
- ch |= RF_SDAT;for(j=2;j--;);//delay;
- RF_SCLK = 0;for(j=2;j--;);//delay;
- }
- Rf_Buffer[Rf_count]= ch;
- if(++Rf_count>=RF_BUFFER_MAX_ZIAE)break;
- }
- RF_SDAT = 0;
- newdata = 1;
- }
- void Rf_Init(void)
- {
- EA = 0;
- IP &= ~0X01; //X0优先级0
- //IPH &= ~0X01;
- IT0 = 1; //INT1 is falling edg trig
- EA = 1;
- RF_CS = 1;
- RF_SCLK = 0;
- RF_SDAT = 0;
- // 模块复位(模块已由外部阻容电路复位)
- // RF_RST = 0; Delay1ms(45);
- // RF_RST = 1; Delay1ms(45);
- }
- unsigned char Rf_Send(unsigned char *buf,unsigned char Count)
- {
- unsigned char i,j,ch;
- RF_CLOSE();
- RF_SDAT = 1;
- RF_SCLK = 0;
- i=200;
- while(RF_SDAT) i--;
- if(!i) return(RF_ERR);
- RF_CS = 0;
- i=200;
- while(!RF_SDAT) i--;
- if(!i) return(RF_ERR);
- for(i=0;i<Count;i++)
- {
- ch=buf[i];
- for(j=0;j<8;j++)
- {
- RF_SDAT = (bit)(ch&0x80);do_nop(1);
- RF_SCLK = 1;do_nop(3);
- RF_SCLK = 0;ch<<=1;do_nop(1);
- }
- }
- RF_CS = 1;
- RF_SDAT = 0;
- //TR1 = 0;
- return(MI_OK);
- }
- unsigned char Rf_Command(unsigned int DelayTime)
- {
- unsigned char Len,BCC_Value;
- Len = Rf_Buffer[LENGTH] + 3; //计算总长度
- Rf_Buffer[Len]=GetBCC(Rf_Buffer,Len); //计算BCC
- if(Rf_Send(Rf_Buffer,Len+1)!=MI_OK) return RF_ERR;
- RF_OPEN();
- while(!newdata && DelayTime) DelayTime--;
- RF_CLOSE();
- if(newdata==0) return RF_ERR; //接收超时判断
- newdata=0;
- Len = Rf_Buffer[LENGTH] + 3; //计算总长度(不含BCC)
- BCC_Value=GetBCC(Rf_Buffer,Len); //计算BCC
- if(BCC_Value == Rf_Buffer[Len])
- return RF_OK;
- return MI_CRCERR;
- }
- //=========================卡操作基本函数=================================
- unsigned char mifs_request(unsigned char _Mode,unsigned char *_TagType)
- {
- Rf_Buffer[SEQNR]=1;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_REQUEST;
- Rf_Buffer[LENGTH]=0x01;
- Rf_Buffer[MODE]=_Mode;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- if(Rf_Buffer[STATUS]==MI_OK)
- {
- _TagType[0]=Rf_Buffer[TAGTYPE];
- _TagType[1]=Rf_Buffer[TAGTYPE+1];
- }
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_anticoll(unsigned char _Bcnt,unsigned char *_SNR)
- {
- Rf_Buffer[SEQNR]=1;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_ANTICOLL;
- Rf_Buffer[LENGTH]=0x01;
- Rf_Buffer[MODE]=_Bcnt;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
- if(Rf_Buffer[STATUS]==MI_OK)
- {
- memcpy(_SNR,&Rf_Buffer[SERNR],4);
- }
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_select(unsigned char *_SNR,unsigned char *_Size)
- {
- Rf_Buffer[SEQNR]=1;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_SELECT;
- Rf_Buffer[LENGTH]=0x04;
- memcpy(&Rf_Buffer[SERNR],_SNR,4);
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- if(Rf_Buffer[STATUS]==MI_OK)
- {
- *_Size=Rf_Buffer[SIZE];
- }
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_authentication(unsigned char _Mode,unsigned char _SecNr)
- {
- Rf_Buffer[SEQNR]=1;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_AUTH;
- Rf_Buffer[LENGTH]=0x02;
- Rf_Buffer[MODE]=_Mode;
- Rf_Buffer[SECNR]=_SecNr;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_halt(void)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_HALT_CARD;
- Rf_Buffer[LENGTH]=0x00;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_read(unsigned char _Adr,unsigned char *_Data)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_READ;
- Rf_Buffer[LENGTH]=0x01;
- Rf_Buffer[ADR]=_Adr;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- if(Rf_Buffer[STATUS]==MI_OK)
- memcpy(_Data,&Rf_Buffer[DATABYTES-1],16);
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_load_key(unsigned char _Mode,unsigned char _SecNr,unsigned char *_Nkey)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_LOADKEY;
- Rf_Buffer[LENGTH]=0x08;
- Rf_Buffer[MODE]=_Mode;
- Rf_Buffer[SECNR]=_SecNr;
- memcpy(&Rf_Buffer[NKEY],_Nkey,6);
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- return Rf_Buffer[STATUS];
- }
- unsigned char mifs_reset(unsigned char _Msec)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_MIFARE_RESET_CARD;
- Rf_Buffer[LENGTH]=1;
- Rf_Buffer[TIME]=_Msec;
-
- if(Rf_Command(DELAY_TIME1)!=MI_OK) return RF_ERR;
-
- return Rf_Buffer[STATUS];
- }
- char SetCardApplicationSector(sttCardStruct *pstCardStruct)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_SET_CARD_APP_SECTOR;
- Rf_Buffer[LENGTH]=16;
- memcpy(&Rf_Buffer[DATA],pstCardStruct,16);
- if(Rf_Command(DELAY_TIME0)!=MI_OK)return RF_ERR;
- return Rf_Buffer[STATUS];
- }
- char ReadCardholderInfo(unsigned char CardSerialNo[],
- unsigned char AuthMode,
- unsigned char Key[],
- sttCardholderInfo *pstCardholderInfo)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_READ_CARDHOLDER_INFO;
- Rf_Buffer[LENGTH]=11;
- memcpy(&Rf_Buffer[DATA],CardSerialNo,4);
- Rf_Buffer[DATA+4] = AuthMode;
- memcpy(&Rf_Buffer[DATA+5],Key,6);
- if(Rf_Command(DELAY_TIME2)!=MI_OK) return RF_ERR;
- if(Rf_Buffer[STATUS]==MI_OK)
- memcpy(pstCardholderInfo,&Rf_Buffer[DATA],41);
- return Rf_Buffer[STATUS];
- }
- char ReadBusinessInfo(unsigned char CardSerialNo[],
- unsigned char AuthMode,
- unsigned char Key[],
- sttBusinessInfo *pstBusinessInfo)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_READ_BUSINESS_INFO;
- Rf_Buffer[LENGTH]=23;
- memcpy(&Rf_Buffer[DATA],CardSerialNo,4);
- Rf_Buffer[DATA+4] = AuthMode;
- memcpy(&Rf_Buffer[DATA+5],Key,18);
- if(Rf_Command(DELAY_TIME3)!=MI_OK) return RF_ERR;
- if(Rf_Buffer[STATUS]==MI_OK)
- memcpy(pstBusinessInfo,&Rf_Buffer[DATA],44);
- return Rf_Buffer[STATUS];
- }
- char SetCardState(unsigned char CardSerialNo[],
- unsigned char AuthMode,
- unsigned char Key[],
- unsigned char State)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_SET_CARD_STATE;
- Rf_Buffer[LENGTH]=12;
- memcpy(&Rf_Buffer[DATA],CardSerialNo,4);
- Rf_Buffer[DATA+4] = AuthMode;
- memcpy(&Rf_Buffer[DATA+5],Key,6);
- Rf_Buffer[DATA+11] = State;
- if(Rf_Command(DELAY_TIME2)!=MI_OK) return RF_ERR;
- return Rf_Buffer[STATUS];
- }
- char Fill(unsigned char CardSerialNo[],
- sttBusinessInfo *pstBusinessInfo)
- {
- Rf_Buffer[SEQNR]=0;
- Rf_Buffer[COMMAND]=SPI_CMD_FILL;
- Rf_Buffer[LENGTH]=20;
- memcpy(&Rf_Buffer[DATA],CardSerialNo,4);
- memcpy(&Rf_Buffer[DATA+4],&pstBusinessInfo->stFillInfo,18);
- if(Rf_Command(DELAY_TIME3)!=MI_OK) return RF_ERR;
- if(Rf_Buffer[STATUS]==MI_OK)
- memcpy(pstBusinessInfo,&Rf_Buffer[DATA],22);
- return Rf_Buffer[STATUS];
- }