Rf500fun.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:9k
- #include "my_head.h"
- #include "RFIC.h"
- /*************************************/
- #define RF_RST P2_5
- #define RF_CS P3_2 //INT0
- #define RF_SCLK P3_4
- #define RF_SDAT P3_5
- /*************************************/
- #define RF_OPEN() IE0=0;EX0=1;
- #define RF_CLOSE() EX0=0;
- BYTE idata rf_buffer[26];
- bit newdata = 0;
- void Rf_Int0(void) interrupt 0 using 1 //rf_rcv(rf_buffer);
- {
- BYTE Rf_count;
- BYTE ii,jj,ch;
- RF_SCLK = 0;
- RF_SDAT = 1;
- for(ii=10;ii--;);//delay;
- Rf_count= 0;
- while(!RF_CS)
- {
- for(ii=0;ii<8;ii++)
- {
- RF_SCLK = 1;ch <<=1;
- ch |= RF_SDAT;for(jj=2;jj--;);//delay;
- RF_SCLK = 0;for(jj=2;jj--;);//delay;
- }
- rf_buffer[Rf_count]= ch;
- if(++Rf_count==26)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
- RF_CLOSE();
- EA = 1;
- RF_CS = 1;
- RF_SCLK = 0;
- RF_SDAT = 0;
- RF_RST = 0; delay_ms(45);
- RF_RST = 1; delay_ms(45);
- }
- BYTE rf_send(BYTE *buf,BYTE count)
- {
- BYTE ii,jj,ch;
- RF_CLOSE();
- RF_SDAT = 1;
- RF_SCLK = 0;
- Set_Timer20ms(3);
- while(RF_SDAT)if(!Get_Timer20ms())return(RF_ERR);
- RF_CS = 0;
- Set_Timer20ms(3);
- while(!RF_SDAT)if(!Get_Timer20ms())return(RF_ERR);
- for(ii=0;ii<count;ii++)
- {
- ch=buf[ii];
- for(jj=0;jj<8;jj++)
- {
- 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(RF_OK);
- }
- BYTE rf500cmd(BYTE length)
- {
- BYTE i,BCC_sum,rf_wdg=10;
- rf_buffer[length]=rf_buffer[0];
- for(i=1;i<length;i++)
- {
- rf_buffer[length]^=rf_buffer[i];
- }
- rf_buffer[length]=~rf_buffer[length];
-
- if(rf_send(rf_buffer,length+1)!=RF_OK)
- {
- SendStr("test1n",7);//for test!
- return RF_ERR;
- }
- RF_OPEN();
- while(!newdata && rf_wdg)
- {
- if(!Get_Timer20ms())
- {
- Set_Timer20ms(3);
- rf_wdg--;
- }
- }
- RF_CLOSE();
-
- if(newdata==0)
- {
- //SendStr("test2n",7);//for test!
- return RF_ERR;
- }
- newdata=0;
-
- BCC_sum= rf_buffer[0];
- rf_wdg = rf_buffer[LENGTH]+3;
- for(i=1;i<rf_wdg;i++)
- {
- BCC_sum^=rf_buffer[i];
- }
- if(BCC_sum==~rf_buffer[rf_wdg])
- {
- //SendStr("test3n",7);//for test!
- return RF_OK;
- }
- //SendStr(rf_buffer,Rf_count);//for test!
- //SendStr("test4n",7);//for test!
- return MI_CRCERR;
- }
- BYTE mifs_request(BYTE _Mode,BYTE idata *_TagType)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x41;
- rf_buffer[LENGTH]=0x01;
- rf_buffer[MODE]=_Mode;
-
- if(rf500cmd(4)!=RF_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];
- }
- BYTE mifs_anticoll(BYTE _Bcnt,BYTE idata *_SNR)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x42;
- rf_buffer[LENGTH]=0x01;
- rf_buffer[MODE]=_Bcnt;
-
- if(rf500cmd(4)!=RF_OK)return RF_ERR;
- if(rf_buffer[STATUS]==MI_OK)
- {
- memcpy(_SNR,&rf_buffer[SERNR],4);
- }
- return rf_buffer[STATUS];
- }
- BYTE mifs_select(BYTE idata *_SNR,BYTE idata *_Size)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x43;
- rf_buffer[LENGTH]=0x04;
- memcpy(&rf_buffer[SERNR],_SNR,4);
-
- if(rf500cmd(7)!=RF_OK)return RF_ERR;
-
- if(rf_buffer[STATUS]==MI_OK)
- {
- *_Size=rf_buffer[SIZE];
- }
- return rf_buffer[STATUS];
- }
- BYTE mifs_authentication(BYTE _Mode,BYTE _SecNr)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x44;
- rf_buffer[LENGTH]=0x02;
- rf_buffer[MODE]=_Mode;
- rf_buffer[SECNR]=_SecNr;
-
- if(rf500cmd(5)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- /*
- BYTE mifs_authentication2(BYTE _Mode,BYTE _SecNr,BYTE _KeyNr)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x72;
- rf_buffer[LENGTH]=0x03;
- rf_buffer[MODE]=_Mode;
- rf_buffer[SECNR]=_SecNr;
- rf_buffer[DATA+2]=_KeyNr;
-
- if(rf500cmd(6)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- */
- BYTE mifs_authKey(BYTE _Mode,BYTE _SecNr,BYTE *_Key)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x73;
- rf_buffer[LENGTH]=0x08;
- rf_buffer[DATA]=_Mode;
- rf_buffer[DATA+1]=_SecNr;
- memcpy(&rf_buffer[DATA+2],_Key,6);
-
- if(rf500cmd(11)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_halt(void)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x45;
- rf_buffer[LENGTH]=0x00;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_read(BYTE _Adr,BYTE idata *_Data)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x46;
- rf_buffer[LENGTH]=0x01;
- rf_buffer[ADR]=_Adr;
-
- if(rf500cmd(4)!=RF_OK)return RF_ERR;
-
- if(rf_buffer[STATUS]==MI_OK)
- {
- memcpy(_Data,&rf_buffer[DATABYTES-1],16);
- }
- return rf_buffer[STATUS];
- }
- BYTE mifs_write(BYTE _Adr,BYTE idata *_Data)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x47;
- rf_buffer[LENGTH]=17;
- rf_buffer[ADR]=_Adr;
- memcpy(&rf_buffer[DATABYTES],_Data,16);
-
- if(rf500cmd(20)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- /*
- BYTE mifs_increment(BYTE _Adr,long idata *_Value)
- {
- BYTE *temp=(BYTE *)_Value;
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x48;
- rf_buffer[LENGTH]=5;
- rf_buffer[ADR]=_Adr;
- rf_buffer[VALUE]=*(temp+3);
- rf_buffer[VALUE+1]=*(temp+2);
- rf_buffer[VALUE+2]=*(temp+1);
- rf_buffer[VALUE+3]=*temp;
-
- if(rf500cmd(8)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_decrement(BYTE _Adr,long idata *_Value)
- {
- BYTE *temp=(BYTE *)_Value;
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x49;
- rf_buffer[LENGTH]=5;
- rf_buffer[ADR]=_Adr;
- rf_buffer[VALUE]=*(temp+3);
- rf_buffer[VALUE+1]=*(temp+2);
- rf_buffer[VALUE+2]=*(temp+1);
- rf_buffer[VALUE+3]=*temp;
-
- if(rf500cmd(8)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_restore(BYTE _Adr)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x4A;
- rf_buffer[LENGTH]=1;
- rf_buffer[ADR]=_Adr;
-
- if(rf500cmd(4)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_transfer(BYTE _Adr)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x4B;
- rf_buffer[LENGTH]=1;
- rf_buffer[ADR]=_Adr;
-
- if(rf500cmd(4)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- */
- BYTE mifs_load_key(BYTE _Mode,BYTE _SecNr,BYTE *_Nkey)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x4C;
- rf_buffer[LENGTH]=0x08;
- rf_buffer[MODE]=_Mode;
- rf_buffer[SECNR]=_SecNr;
- memcpy(&rf_buffer[NKEY],_Nkey,6);
-
- if(rf500cmd(11)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_reset(BYTE _Msec)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x4E;
- rf_buffer[LENGTH]=1;
- rf_buffer[TIME]=_Msec;
-
- if(rf500cmd(4)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- /*
- BYTE mifs_get_info(BYTE idata *_Info)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x4F;
- rf_buffer[LENGTH]=0;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- if(rf_buffer[STATUS]==MI_OK)
- {
- memcpy(_Info,&rf_buffer[INFO],rf_buffer[LENGTH]);
- }
- return rf_buffer[STATUS];
- }
- BYTE mifs_close(void)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x3F;
- rf_buffer[LENGTH]=0;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- */
- BYTE mifs_config(void)
- {
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x52;
- rf_buffer[LENGTH]=0;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- /*
- BYTE mifs_check_write(BYTE idata *_SNR,BYTE _Authmode,BYTE _Adr,BYTE idata *_Data)
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x53;
- rf_buffer[LENGTH]=22;
- memcpy(&rf_buffer[SERNR],_SNR,4);
- rf_buffer[AUTHMODE]=_Authmode;
- rf_buffer[ADRCHKWR]=_Adr;
- memcpy(&rf_buffer[DATACHKWR],_Data,16);
-
- if(rf500cmd(25)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_set_control_bit()
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x50;
- rf_buffer[LENGTH]=0;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_clr_control_bit()
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x51;
- rf_buffer[LENGTH]=0;
-
- if(rf500cmd(3)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_buzzer(BYTE _Frquence,BYTE _10ms)
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x60;
- rf_buffer[LENGTH]=2;
- rf_buffer[3]=_Frquence;
- rf_buffer[4]=_10ms;
-
- if(rf500cmd(5)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_read_E2(BYTE _Adr,BYTE _Length,BYTE idata *_Data)
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x61;
- rf_buffer[LENGTH]=2;
- rf_buffer[ADR]=_Adr;
- rf_buffer[4]=_Length;
-
- if(rf500cmd(5)!=RF_OK)return RF_ERR;
-
- if(rf_buffer[STATUS]==MI_OK)
- {
- memcpy(_Data,&rf_buffer[INFO],_Length);
- }
- return rf_buffer[STATUS];
- }
- BYTE mifs_write_E2(BYTE _Adr,BYTE _Length,BYTE idata *_Data)
- {
- rf_buffer[0]=0;
- rf_buffer[COMMAND]=0x62;
- rf_buffer[LENGTH]=_Length+2;
- rf_buffer[ADR]=_Adr;
- rf_buffer[4]=_Length;
- memcpy(&rf_buffer[5],_Data,_Length);
-
- if(rf500cmd(_Length+5)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- BYTE mifs_value(BYTE _Mode,BYTE _Adr,long *_Value,BYTE _Trans_Adr)
- {
- BYTE *temp=(BYTE *)_Value;
- rf_buffer[SEQNR]=0;
- rf_buffer[COMMAND]=0x70;
- rf_buffer[LENGTH]=7;
- rf_buffer[DATA] =_Mode;
- rf_buffer[DATA+1]=_Adr;
- rf_buffer[DATA+2]=*(temp+3);
- rf_buffer[DATA+3]=*(temp+2);
- rf_buffer[DATA+4]=*(temp+1);
- rf_buffer[DATA+5]=*temp;
- rf_buffer[DATA+6]=_Trans_Adr;
-
- if(rf500cmd(10)!=RF_OK)return RF_ERR;
-
- return rf_buffer[STATUS];
- }
- */