[C語言源程序]
#include
#include
#include?
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
???????????????????????? 0x6d,0x7d,0x07, 0x7f,0x6f,};?
unsigned char sec;??????? //定義計數值,每過1秒,sec加1?
unsigned int tcnt;??????? //定時中斷次數
bit? write=0;???????????? //寫24C08的標志;
sbit gewei=P2^0;????????? //個位選通定義
sbit shiwei=P2^1;???????? //十位選通定義
/////////24C08讀寫驅動程序////////////////////
sbit scl=P3^4;?? // 24c08 SCL
sbit sda=P3^5;? // 24c08 SDA
void delay1(unsigned char x)
{? unsigned int i;
? for(i=0;i
? ;}
void flash()?
{? ;? ; }
void x24c08_init()? //24c08初始化子程序
?{scl=1; flash(); sda=1; flash();}
void start()??????? //啟動I2C總線
{sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();}
void stop()???????? //停止I2C總線
{sda=0; flash(); scl=1; flash(); sda=1; flash();}
void writex(unsigned char j)? //寫一個字節
{? unsigned char i,temp;
?? temp=j;
?? for (i=0;i<8;i++)
?? {temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();}
?? scl=0; flash(); sda=1; flash();
}
unsigned char readx()?? //讀一個字節
{
?? unsigned char i,j,k=0;
?? scl=0;? flash();? sda=1;
?? for (i=0;i<8;i++)
??? {??
flash();? scl=1;? flash();
????? if (sda==1) j=1;
????? else j=0;
????? k=(k<<1)|j;?
?? scl=0;}
?? flash();? return(k);
}
void clock()???????? // I2C總線時鐘
{
?? unsigned char i=0;
?? scl=1;? flash();
?? while ((sda==1)&&(i<255))i++;
?? scl=0; flash();
}
////////從24c02的地址address中讀取一個字節數據/////
unsigned char x24c08_read(unsigned char address)
{
?? unsigned char i;
?? start(); writex(0xa0);
?? clock(); writex(address);
?? clock(); start();
?? writex(0xa1); clock();
?? i=readx(); stop();
?? delay1(10);
?? return(i);
}
//////向24c02的address地址中寫入一字節數據info/////
void x24c08_write(unsigned char address,unsigned char info)
{
?? EA=0;
?? start(); writex(0xa0);
?? clock(); writex(address);
?? clock(); writex(info);
?? clock(); stop();
?? EA=1;
?? delay1(50);
}
/////////////24C08讀寫驅動程序完/////////////////////
void Delay(unsigned int tc)???? //延時程序
{
?while( tc != 0 )???
??? {unsigned int i;???
???? for(i=0; i<100; i++);?
???? tc--;}
}
void LED()????????????????? //LED顯示函數
{
?? shiwei=0; P0=table[sec/10]; Delay(8); shiwei=1;
?? gewei=0;? P0=table[sec%10]; Delay(5); gewei=1;
}
void t0(void) interrupt 1 using 0? //定時中斷服務函數
{?
TH0=(65536-50000)/256; //對TH0 TL0賦值
TL0=(65536-50000)%256; //重裝計數初值
tcnt++;??????? //每過250ust tcnt加一
if(tcnt==20)? //計滿20次(1秒)時
?? {?
??? tcnt=0;?? //重新再計
??? sec++;
??? write=1;? //1秒寫一次24C08
??? if(sec==100) //定時100秒,在從零開始計時
????? {sec=0;}?
?? }?
}
void main(void)?
{?
TMOD=0x01;? //定時器工作在方式1
ET0=1;? EA=1;?
x24c08_init();??????? //初始化24C08
sec=x24c08_read(2);//讀出保存的數據賦于sec
TH0=(65536-50000)/256; //對TH0 TL0賦值
TL0=(65536-50000)%256; //使定時器0.05秒中斷一次
TR0=1;?????????? //開始計時
?while(1)?
? {
??LED();
?if(write==1) //判斷計時器是否計時一秒
??? {
? write=0;????????????? //清零?
???? x24c08_write(2,sec);? //在24c08的地址2中寫入數據sec
?}
?? }
}
#include
#include
#include
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,
???????????????????????? 0x6d,0x7d,0x07, 0x7f,0x6f,};?
unsigned char sec;??????? //定義計數值,每過1秒,sec加1?
unsigned int tcnt;??????? //定時中斷次數
bit? write=0;???????????? //寫24C08的標志;
sbit gewei=P2^0;????????? //個位選通定義
sbit shiwei=P2^1;???????? //十位選通定義
/////////24C08讀寫驅動程序////////////////////
sbit scl=P3^4;?? // 24c08 SCL
sbit sda=P3^5;? // 24c08 SDA
void delay1(unsigned char x)
{? unsigned int i;
? for(i=0;i
void flash()?
{? ;? ; }
void x24c08_init()? //24c08初始化子程序
?{scl=1; flash(); sda=1; flash();}
void start()??????? //啟動I2C總線
{sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();}
void stop()???????? //停止I2C總線
{sda=0; flash(); scl=1; flash(); sda=1; flash();}
void writex(unsigned char j)? //寫一個字節
{? unsigned char i,temp;
?? temp=j;
?? for (i=0;i<8;i++)
?? {temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();}
?? scl=0; flash(); sda=1; flash();
}
unsigned char readx()?? //讀一個字節
{
?? unsigned char i,j,k=0;
?? scl=0;? flash();? sda=1;
?? for (i=0;i<8;i++)
??? {??
flash();? scl=1;? flash();
????? if (sda==1) j=1;
????? else j=0;
????? k=(k<<1)|j;?
?? scl=0;}
?? flash();? return(k);
}
void clock()???????? // I2C總線時鐘
{
?? unsigned char i=0;
?? scl=1;? flash();
?? while ((sda==1)&&(i<255))i++;
?? scl=0; flash();
}
////////從24c02的地址address中讀取一個字節數據/////
unsigned char x24c08_read(unsigned char address)
{
?? unsigned char i;
?? start(); writex(0xa0);
?? clock(); writex(address);
?? clock(); start();
?? writex(0xa1); clock();
?? i=readx(); stop();
?? delay1(10);
?? return(i);
}
//////向24c02的address地址中寫入一字節數據info/////
void x24c08_write(unsigned char address,unsigned char info)
{
?? EA=0;
?? start(); writex(0xa0);
?? clock(); writex(address);
?? clock(); writex(info);
?? clock(); stop();
?? EA=1;
?? delay1(50);
}
/////////////24C08讀寫驅動程序完/////////////////////
void Delay(unsigned int tc)???? //延時程序
{
?while( tc != 0 )???
??? {unsigned int i;???
???? for(i=0; i<100; i++);?
???? tc--;}
}
void LED()????????????????? //LED顯示函數
{
?? shiwei=0; P0=table[sec/10]; Delay(8); shiwei=1;
?? gewei=0;? P0=table[sec%10]; Delay(5); gewei=1;
}
void t0(void) interrupt 1 using 0? //定時中斷服務函數
{?
TH0=(65536-50000)/256; //對TH0 TL0賦值
TL0=(65536-50000)%256; //重裝計數初值
tcnt++;??????? //每過250ust tcnt加一
if(tcnt==20)? //計滿20次(1秒)時
?? {?
??? tcnt=0;?? //重新再計
??? sec++;
??? write=1;? //1秒寫一次24C08
??? if(sec==100) //定時100秒,在從零開始計時
????? {sec=0;}?
?? }?
}
void main(void)?
{?
TMOD=0x01;? //定時器工作在方式1
ET0=1;? EA=1;?
x24c08_init();??????? //初始化24C08
sec=x24c08_read(2);//讀出保存的數據賦于sec
TH0=(65536-50000)/256; //對TH0 TL0賦值
TL0=(65536-50000)%256; //使定時器0.05秒中斷一次
TR0=1;?????????? //開始計時
?while(1)?
? {
??LED();
?if(write==1) //判斷計時器是否計時一秒
??? {
? write=0;????????????? //清零?
???? x24c08_write(2,sec);? //在24c08的地址2中寫入數據sec
?}
?? }
}
評論
查看更多