/*硬件资源:
RAM ADDRESS :0000H---7FFFH
CF card ADDRESS:8000H---8FFFH
CPLD ADDRESS:9000H---FFFFH
RAM SIZE :32K FLASH SIZE:8MBYTE = 4K*512PAGE*4PCS
PCB:V10
*/
#include #include //#include #include #include #include //#include #include //绝对地址 #include #include #define uchar unsigned char #define uint unsigned int #define byte unsigned char #define ulong unsigned long #define bool bit #define TRUE 1 #define FALSE 0 #define V_TH0 0xff /* 时间常数高8位 (65536-500)/256 */ #define V_TL0 0xff /* 时间常数低8位 (65536-500)>256 11.0592*/ //#define V_TH0 (65536-1536)/256 //#define V_TL0 (65536-1536)>256 #define V_TMOD 0x01 /* 定时器T0方式控制字 */ #define WriteDeviceAddress 0xa0 #define ReadDviceAddress 0xa1 sfr CHPCON= 0xbf; sfr CHPENR=0xf6; sfr PMR= 0xC4; sfr SBUF1=0xc1; sfr SCON1=0xc0; sbit SM01 = SCON1^7; /* alternative SM0_FE_1 */ sbit SM11 = SCON1^6; /* alternative SM1_1 */ sbit SM21 = SCON1^5; /* alternative SM2_1 */ sbit REN1 = SCON1^4; /* alternative REN_1 */ sbit TB81 = SCON1^3; /* alternative TB8_1 */ sbit RB81 = SCON1^2; /* alternative RB8_1 */ sbit TI1 = SCON1^1; /* alternative TI_1 */ sbit RI1 = SCON1^0; /* alternative RI_1 */ sbit ES1 = IE^6; /* 定义定时器T0的时间常数值和方式控制字 */ #define REGL XBYTE[0x9003] //控制寄存器护栏管的长度 #define FPGH XBYTE[0x9001] //闪存页高位 #define FPGL XBYTE[0x9002] //闪存页低位 //-------- CF CARD REG ADDRESS #define EVEN_Data XBYTE[0xa400] #define ODD_Data XBYTE[0xa400] #define Data_Reg XBYTE[0xa000] //DATA REGISTER(R/W) #define Error_Reg XBYTE[0xa001] //ERROR REGISTER(READ) #define Features XBYTE[0xa001] //FEATURE REGISTER(WRITE) #define Sectr_Cnt XBYTE[0xa002]//SECTOR COUNT REGISTER(R/W) #define Sectr_No XBYTE[0xa003] //SECTOR NUMBER REGISTER(R/W) #define Cylinder_Low XBYTE[0xa004] //CYLINDER LOW REGISTER(R/W) #define Cylinder_Hi XBYTE[0xa005] //CYLINDER HIGH REGISTER(R/W) #define Drv_Head XBYTE[0xa006] //DIRVE/HEAD REGISTER(R/W) #define Status XBYTE[0xa007] //STATUS REGISTER(READ) #define Command XBYTE[0xa007] //COMMAND REGISTER(WRITE) #define Alt_Status XBYTE[0xa00e] //Alternate Status Regiter(read only) #define Device_Ctrl XBYTE[0xa00e] //Device Control Register(Write only) #define Device_Addrs XBYTE[0xa00f] //Drive Address Register(read only).Not used in this demo #define Config XBYTE[0x8200] //Configuration Option Register #define Socket XBYTE[0x8206] //Socket and copy Register //CF card All command code #define ChkPwr 0xe5 #define Diagnostic 0x90 #define EraseSctr 0xc0 #define Format 0x50 #define Identify 0xec #define Idle 0xe3 #define Idlelmm 0xe1 #define Initialize 0x91 #define ReadBuf 0xe4 #define ReadLong 0x22 #define ReadMulti 0xc4 #define ReadSctr 0x20 #define ReadVerify 0x40 #define Recalibrate 0x10 #define ReadSense 0x03 #define Seek 0x70 #define SetFeature 0xef #define SetMulti 0xc6 #define Sleep 0xe6 #define Standby 0xe2 #define Standbylmm 0xe0 #define Translate 0x87 #define WearLevel 0xf5 #define WriteBuf 0xe8 #define WriteLong 0x32 #define WriteMulti 0xc5 #define WriteSctr 0x30 #define WriteVerify 0x3c #define WrtMwoErase 0xcd #define WrtSwoErase 0x38 #define debug 1; //********************************************** sbit senden=P3^2; sbit SDA=P3^3; sbit SCL=P3^5; sbit wdgclr=P1^7; //************************************************* uchar Select; uchar LBA[6]; //Sets up sector ,CF Card Address uint zhepe; //帧频 uint ms; //毫秒定时 uchar ADD; //地址 uchar idata iic_buf[96]; //接收缓冲区 uint pointer; uchar xdata *bufp; uchar subchk; //接收指针 和校验 uchar paklen; //包长 uint Dcyli; //总柱面 uint Dhead; //总磁头 uint sec_track; //扇区/磁道 uchar bdata flag; sbit frameok=flag^0; //rece ok sbit color=flag^1; //0 sing red color 1 red green color sbit rec_sta=flag^2; //1 进入接状态,不显示 sbit rec_end=flag^3; sbit timout=flag^4; //jmp_buf env; //int retval; /***************************************************************************/ void I2cDelay(unsigned int number) { unsigned char temp; for(;number!=0;number--,wdgclr=!wdgclr) { for(temp=112;temp!=0;temp--) { } } } //******************************* void SomeNOP(void) { uchar i; i=5; while(i--); } /***************************************************************************/ void Start() { SDA=1;SomeNOP(); SCL=1;SomeNOP(); SDA=0;SomeNOP(); SCL=0;SomeNOP(); } /***************************************************************************/ void Stop() { SCL=0;SomeNOP(); SDA=0;SomeNOP(); SCL=1;SomeNOP(); SDA=1;SomeNOP(); } /***************************************************************************/ void Ack() { SDA=0;SomeNOP(); SCL=1;SomeNOP(); SCL=0;SomeNOP(); SDA=1;SomeNOP(); } /***************************************************************************/ void NoAck() { SDA=1;SomeNOP(); SCL=1;SomeNOP(); SCL=0;SomeNOP(); } /***************************************************************************/ bit TestAck() { bit ErrorBit; SDA=1;SomeNOP(); SCL=1;SomeNOP(); ErrorBit=SDA; SCL=0; return(ErrorBit); } /***************************************************************************/ Write8Bit(unsigned char input) { unsigned char temp; for(temp=8;temp!=0;temp--) { SDA=(bit)(input&amt;0x80); SCL=1;SomeNOP(); SCL=0;SomeNOP(); input=input<<1; } } /***************************************************************************/ void Write24c02(unsigned char xdata *Wdata,unsigned char RomAddress,unsigned char number) { wdgclr=!wdgclr; Start(); Write8Bit(WriteDeviceAddress); TestAck(); Write8Bit(RomAddress); TestAck(); for(;number!=0;number--) { Write8Bit(*Wdata); TestAck(); Wdata++; } Stop(); I2cDelay(10); } /***************************************************************************/ unsigned char Read8Bit() { unsigned char temp,rbyte=0; for(temp=8;temp!=0;temp--) { SCL=1; rbyte=rbyte<<1; rbyte=rbyte|((unsigned char)(SDA)); SCL=0; } return(rbyte); } /***************************************************************************/ void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes) { // unsigned char temp,rbyte; wdgclr=!wdgclr; Start(); Write8Bit(WriteDeviceAddress); TestAck(); Write8Bit(RomAddress); TestAck(); Start(); Write8Bit(ReadDviceAddress); TestAck(); while(bytes!=1) { *RamAddress=Read8Bit(); Ack(); RamAddress++; bytes--; } *RamAddress=Read8Bit(); NoAck(); Stop(); } /*************************************************
相关文章