1、#include #ifndef false#define false 0#endif#ifndef true#define true 1#endif#define WriteDeviceAddress 0xa0#define ReadDviceAddress 0xa1 sbit SDA = P3 5; /根据实际连接的管脚定义sbit SCL = P3 4;sbit led = P10;sbit led2 = P11;unsigned char code Num21=0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x40,0x79,0x2
2、4,0x30,0x19,0x12,0x02,0x78,0x00,0x10,0x89; unsigned char code Disdigit4 = 0x7F,0xBF,0xDF,0xEF;unsigned char Disbuf4;unsigned char code write_data5 = 5,2,5,1,8 ;unsigned char read_data5;/-void Delayus(unsigned int number) for(;number!=0;number-)/-void DelayMs(unsigned int number) unsigned char temp;f
3、or(;number!=0;number-)for(temp=112;temp!=0;temp-)/-void Start() SDA=1;Delayus(4);SCL=1;Delayus(4);SDA=0;Delayus(4);SCL=0;Delayus(4);/-void Stop() SCL=0;Delayus(4);SDA=0;Delayus(4);SCL=1;Delayus(4);SDA=1;Delayus(4);/-void Ack() SDA=0;Delayus(4);SCL=1;Delayus(4);SCL=0;Delayus(4);SDA=1;Delayus(4);/-voi
4、d NoAck() SDA=1;Delayus(4);SCL=1;Delayus(4);SCL=0;Delayus(4);SDA=0; /-bit TestAck() bit ErrorBit;SDA=1;Delayus(4);SCL=1;Delayus(4);ErrorBit=SDA;Delayus(4);SCL=0;return(ErrorBit);/-void Write8Bit(unsigned char input) unsigned char temp;for(temp=8;temp!=0;temp-)SDA=(bit)(inputDelayus(4);SCL=1;Delayus(
5、4);SCL=0;Delayus(4);input=input1;/-unsigned char Read8Bit() unsigned char temp,rbyte=0;for(temp=8;temp!=0;temp-)SCL=1;Delayus(4);rbyte=rbyte1;rbyte=rbyte|(unsigned char)(SDA);SCL=0;return(rbyte);void Write24c02(unsigned char *Wdata,unsigned char RomAddress,unsigned char number)Start();Write8Bit(Writ
6、eDeviceAddress);TestAck();Write8Bit(RomAddress);TestAck();for(;number!=0;number-) Write8Bit(*Wdata);TestAck();Wdata+;Stop();DelayMs(10);void Read24c02(unsigned char *RamAddress,unsigned char RomAddress,unsigned char bytes)Start();Write8Bit(WriteDeviceAddress);TestAck();Write8Bit(RomAddress);TestAck(
7、);Start();Write8Bit(ReadDviceAddress);TestAck();while(bytes!=1) *RamAddress=Read8Bit();Ack();RamAddress+;bytes-;*RamAddress=Read8Bit();NoAck();Stop();void Display(void) /显示unsigned int i = 0;unsigned int temp,count;temp = Disdigitcount; P2 =temp;temp = Disbufcount;temp = Numtemp;P0 =temp;count+; if
8、(count=4)count=0; void time1() interrupt 3 using 2Display(); TH1 = (65535 - 2000)/256;TL1 = (65535 - 2000)%256;void main()TMOD = 0x15;TR1 = 1;EA = 1;ET1 = 1; Write24c02(write_data,0x00,3);DelayMs(200); Read24c02(read_data,0x00,4);while(1)Disbuf0 = read_data0;Disbuf1 = read_data1;Disbuf2 = read_data2;Disbuf3 = read_data3;