#include <GLOBAL.h> #include <stdlib.h> #include <stdbool.h> #include <msp430x41x.h> #define SysSclOpen P1SEL|=BIT0; #define SysSclClos P1SEL&=~BIT0; #define SysSclIns P1DIR &=~BIT0 #define SysSclOut P1DIR |= BIT0 #define DataOutIn P1DIR &=~ BIT2 #define DataOutSt P1IN & BIT2 #define CardPowerOut P1DIR |= BIT3 #define CardPowerOpen P1OUT |= BIT3 #define CardPowerClose P1OUT &= ~BIT3 #define CardFrequencyOut P1DIR |= BIT5 #define CardFrequencyHig P1OUT |= BIT5 #define CardFrequencyLow P1OUT &= ~BIT5 #define CardChkSt BIT0 #define CardChkDirIn P1DIR &=~ BIT0 #define SendGapTime 170 #define SendHigTime 256 #define SendLowTime 80 //Delay(43);//---延时63.73微秒 //Delay(89);//----延时126.92微秒 //Delay(182);//---延时254.67微秒 //Delay(276);//---延时383.79微秒 const unsigned char CardPassword[4]={0x47,0x48,0x5A,0x42}; void CardInit(void) { CardPowerOut; CardPowerClose; CardChkDirIn; P1IE = CardChkSt; P1IES = CardChkSt; } void FieldClockOpen(void) { //TACCR0 = 13;//------------------------- //TACCTL0=0x0080;//----------------------mode 4 //TACTL=0X0210;//------------------------SMCLK,TA_INT=Dis. SysSclOut; SysSclOpen; CardFrequencyOut; CardFrequencyHig; WDT(); Delay(5000); } void FieldClockClos(void) { TACCR0=0;//------------------------- TACCTL0=0;//----------------------mode 4 TACTL=0;//------------------------SMCLK,TA_INT=Dis. SysSclClos//-----------------------------------------------------------关闭时钟 SysSclIns; CardFrequencyLow; WDT(); Delay(5000); WDT(); Delay(5000); } void CardPowerOpenConn(void) { CardPowerOut; CardPowerOpen; FieldClockClos(); WDT(); Delay(5000); FieldClockOpen(); DataOutIn; } void CardPowerClosConn(void) { CardPowerOut; CardPowerClose; FieldClockClos(); } bool HeardChk(void) { bool ChkOk; unsigned int ChkTime; WDT(); ChkOk=1; ChkTime=0; //------------------------------------------------等待上升沿的到来同步信号检测 do { _NOP(); } while(!(DataOutSt&BIT2)); if(ChkOk) { ChkTime=2; //------------------------------------------------等待下降沿的到来同步信号检测1(24FC---HIG---384) do { ChkTime=ChkTime+12; if(ChkTime>1748)//-----最大极限数值:(384+96)*3.64=1748 { break; }//-------------------此处数据为等待时间(384微妙)同时防治死机! } while(DataOutSt&BIT2); if((1164 < ChkTime)&&(ChkTime < 1630))//检测同步信号维持时间---最小数值:(384-64)*3.64=1164 最大数值:(384+64)*4=1630 { ChkOk=1; } else { ChkOk=0; } } if(ChkOk) { ChkTime=2; //------------------------------------------------等待下降沿的到来同步信号检测1(8FC---LOW---128) do { ChkTime=ChkTime+12; if(ChkTime>815)//-----最大极限数值:(128+96)*3.64=815 { break; }//-------------------此处数据为等待时间(128微妙)同时防治死机! } while(!(DataOutSt&BIT2)); if((232 < ChkTime)&&(ChkTime < 698))//检测同步信号维持时间---最小数值:(128-64)*3.64=349 最大数值:(128+64)*3.64=698 { ChkOk=1; Delay(182);//---延时256 Delay(42);//---延时63.73微秒 Delay(89);//----延时126.92微秒 } else { ChkOk=0; } } return ChkOk; } //-----***-unsigned char RecvConn(void)-***-----// //---功能描述:接收一个数据。 //---输入条件:无。 //---输出项目:RecvData。 void ReadData(void) { unsigned char i; unsigned char Bits; for(i=0;i<24;i++) { Bits = 0; State = 1; RecvData = 0; while(State) { if( Bits < 8 ) { Bits ++ ; RecvData <<= 1; if(DataOutSt&BIT2) { RecvData &= 0xFE; } else { RecvData |= 0x01; } if( Bits < 8) { Delay(170);//---16FC } else { Delay(85);//---8FC //------------------------------------------------等待下降沿的到来同步信号检测1(8FC---LOW---128) if(DataOutSt&BIT2) { do { _NOP(); } while(DataOutSt&BIT2); } else { do { _NOP(); } while(!(DataOutSt&BIT2)); } } } else { State=0; } } Delay(35); CardBuf=RecvData; } } void CardRead(void)//-------------------读卡调用此函数 { unsigned int i; _DINT(); WDT(); CardErr=0; RamCler(CardBuf,24); for(i=0;i<512;i++) { if(HeardChk()) { ReadData(); break; } } WDT(); _EINT(); } //------------------------------------------------------------------------------写卡程序 void SendZero(void) { CardFrequencyHig; Delay(SendLowTime); } void SendOne(void) { CardFrequencyHig; Delay(SendHigTime); } void SendGap(void) { CardFrequencyLow; Delay(SendGapTime); } void RfStart(void) { CardFrequencyLow; Delay(SendGapTime); } void RfReast(void) { RfStart(); SendZero(); SendGap(); SendZero(); SendGap(); FieldClockClos(); } void SendWord(unsigned char Temp) { unsigned char i; for(i=0;i<8;i++) { if(Temp&0x80) { SendOne(); SendGap(); } else { SendZero(); SendGap(); } Temp=Temp<<1; } } void CardWrite(void)//------------------写卡调用此函数 { unsigned char i,j; unsigned char DataDptr,TempData; _DINT(); for(i=1;i<7;i++) { WDT(); DataDptr=(i-1)*4;//----------------节起始地址 RfReast(); RfStart(); SendOne(); SendGap(); SendZero(); SendGap(); for(j=0;j<4;j++) //--------密码发送的四字节循环 { WDT(); SendWord(CardPassword[j]);//----------------------------------发送密码 } SendZero(); SendGap(); for(j=DataDptr;j<DataDptr+4;j++) //-----------块的四字节循环 { SendWord(CardBuf[j]); //-------------发送一块数据(四个字节) } TempData=i<<5; for(j=0;j<3;j++) { if(TempData&0x80) { SendOne(); SendGap(); } else { SendZero(); SendGap(); } TempData=TempData<<1; } FieldClockClos(); } WDT(); Delay(5000); _EINT(); }