#include #define uint unsigned int //定義unsigned int 為uint#define uchar unsigned char //定義unsigned char 為uchar#define uchar unsigned char //定義unsigned char 為uchar#define uint unsigned int //定義unsigned int 為uintsbit sda=P3^3;//定義I2C數(shù)據(jù)線sbit scl=P3^2;//定義I2C時(shí)鐘線void delay2()//延遲6us函數(shù){;;;}void start()//I2C開(kāi)始時(shí)鐘函數(shù){sda=1;delay2();//延遲6us是時(shí)序要求。scl=1;delay2();sda=0;delay2();}void stop()//I2C結(jié)束時(shí)鐘函數(shù){sda=0;delay2();//延遲6us是時(shí)序要求。scl=1;delay2();sda=1;delay2();}void respons()//應(yīng)答函數(shù){uchar i;//定義變量Iscl=1;delay2();while((sda==1)&&(i<250))i++;//沒(méi)有收到應(yīng)答信號(hào)在等//過(guò)一段時(shí)間后還是沒(méi)收到自動(dòng)跳出該語(yǔ)句.相與。scl=0;delay2();}void init()//初始化函數(shù)。{scl=1;sda=1;}void write_byte(uchar date)//寫(xiě)一字節(jié)函數(shù)。{uchar i,temp;temp=date;for(i=0;i<8;i++){temp=temp<<1;//TEMP左移一位!scl=0;delay2();sda=CY;//送走最高位,依次把八位送完。scl=1;//保持?jǐn)?shù)據(jù)穩(wěn)定。delay2();}scl=0;delay2();sda=1;//總線釋放。delay2();}uchar read_byte()//讀一字節(jié)函數(shù)。帶返回值的函數(shù){uchar i,j,k;scl=0;delay2();sda=1;//總線釋放。delay2();for(i=0;i<8;i++){ scl=1;delay2();j=sda;k=(k<<1)|j;//k左移一位最低位變成了0,再與J相或,J就送到K最低位。scl=0;//置低以便下次讀數(shù)據(jù)。delay2();}return k;}void write_add(uchar address,uchar date){ start();//I2C開(kāi)始時(shí)鐘函數(shù)write_byte(0xa0);//at24c02的固定地址A,1010,AO-A3都接地都為0。respons();//寫(xiě)完后調(diào)用應(yīng)答函數(shù)write_byte(address);//確定從at24c02的第address位置寫(xiě)數(shù)據(jù)。respons();//寫(xiě)完后調(diào)用應(yīng)答函數(shù)write_byte(date);//在芯片第address位置寫(xiě)date.respons();//寫(xiě)完后調(diào)用應(yīng)答函數(shù)stop();//I2C結(jié)束時(shí)鐘函數(shù)}uchar read_add(uchar address){ uchar date;start();//I2C開(kāi)始時(shí)鐘函數(shù)write_byte(0xa0);//開(kāi)始讀。respons();//讀完后調(diào)用應(yīng)答函數(shù)write_byte(address);//確定從at24c02的第address位置讀數(shù)據(jù)。respons();//讀完后調(diào)用應(yīng)答函數(shù)start();//I2C開(kāi)始時(shí)鐘函數(shù)write_byte(0xa1);//讀數(shù)據(jù)respons();//讀完后調(diào)用應(yīng)答函數(shù)date=read_byte();//送到P1口顯示。stop();//I2C結(jié)束時(shí)鐘函數(shù)return date;}uint intnum,aa,ee,dd,cc;sbit D1=P3^4;//位定義sbit D2=P3^5;//位定義sbit D3=P3^7;//位定義sbit D4=P1^6;//位定義sbit D5=P1^7;//位定義void delay (uint z);void main (){P1=0xff;dd=0;D2=0;D3=0;delay (2200); //aa=5;//ee=11000;while(1){ uchar f,k;f=2;init();//寫(xiě)直址,最后低位應(yīng)為0。delay (1);k=read_add(f);//送到P1口顯示。//從23地址讀數(shù)據(jù)aa=k;ee=aa*2200;if(D4==0){ delay (100); if(D4==1){aa++;delay (1);init();//寫(xiě)直址,最后低位應(yīng)為0。write_add(2,aa);//23為at24c02內(nèi)部?jī)?chǔ)存地址,0xaa為寫(xiě)到23地址的數(shù)據(jù)。delay (1);k=read_add(2);//送到P1口顯示。//從23地址讀數(shù)據(jù)aa=k;stop();if(aa==8){aa=7;}}}if(aa>1){if(D5==0){ delay (100); if(D5==1){aa--;delay (1);init();//寫(xiě)直址,最后低位應(yīng)為0。write_add(2,aa);//23為at24c02內(nèi)部?jī)?chǔ)存地址,0xaa為寫(xiě)到23地址的數(shù)據(jù)。delay (1);k=read_add(2);//送到P1口顯示。//從23地址讀數(shù)據(jù)aa=k;stop();//ee=aa*2200;if(aa==1){aa=2;}}}}if(dd==0){if(D1==0){ delay (5000);if(D1==0){D2=0;D3=1;dd=1;delay (ee);D2=0;D3=0;}}}if(dd==1){if(D1==1){delay (5000);if(D1==1){D2=1;D3=0;dd=0;delay (ee); D2=0;D3=0;}}}}}void delay(uint z)//代參數(shù)無(wú)返回值{ uint x,y;for (x=300;x>0;x--)for (y=z;y>0;y--);//100*2000次}
評(píng)論