題目:顯示000~999跑表,先讀數(shù)據(jù)到EEPROM,再從EEPROM讀出,再在數(shù)碼管上顯示出 來
本文引用地址:http://m.butianyuan.cn/article/201611/320191.htm解決方案:(C 語言編程)
#include
#include
#define uchar unsigned char
#define uint unsigned int
uchar code tabledu[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};
uchar code tablewe[]={0xff,0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
uchar bai,shi,ge,num,flag;
uint shu;
void delay1(uchar);
void display(uchar ba,uchar sh,uchar g);
sbit scl=P3^3;
sbit sda=P3^4;
sbit le=P3^6;
sbit leden=P2^5;
sbit wkle=P3^7;
void delay() //短延時子程序
{
;;
}
void init()//初使化子程序
{
leden=0;
wkle=0;
flag=0;
shu=0;
bai=0;
shi=0;
ge=0;
TMOD=0x01;//定時器初始化
TH0=0xd8;
TL0=0xf0;
EA=1;
ET0=1;
TR0=1;
sda=1;
scl=1;
}
void start() //開始子程序
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void write_byte(uchar dat) //寫入字節(jié)子程序
{
uchar a,temp;
temp=dat;
for(a=0;a<8;a++)
{
temp=temp<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte() //讀出字節(jié)子程序
{
uchar a,b,c;
scl=0;
delay();
sda=1;
delay();
for(a=0;a<8;a++)
{
scl=1;
delay();
b=sda;
c=(c<<1)|b;
scl=0;
delay();
}
return c;
}
void respon()//響應(yīng)子程序
{
uchar a;
scl=1;
delay();
while((sda==1)&&(a<100))a++;
scl=0;
delay();
}
void write(uchar add,uchar dat) //寫入子程序
{
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
write_byte(dat);
respon();
stop();
}
uchar read(uchar add) //讀出子程序
{
uchar dat;
start();
write_byte(0xa0);
respon();
write_byte(add);
respon();
start();
write_byte(0xa1);
respon();
dat=read_byte();
respon();
stop();
return dat;
}
void main() //main 函數(shù)
{
init();
while(1)
{
display(bai,shi,ge);
}
}
void time0() interrupt 1//中斷子函數(shù)
{
uchar num;
TH0=0xd8;
TL0=0xf0;
num++;
if(num==10){
num=0;
flag=1;
shu++;
if(shu==999)
shu=0;
bai=shu/100;
shi=shu%100/10;
ge=shu%10;
write(1,tabledu[bai]);
delay1(3);
bai=read(1);
delay1(3);
write(2,tabledu[shi]);
delay1(3);
shi=read(2);
delay1(3);
write(3,tabledu[ge]);
delay1(5);
ge=read(3);
}
}
void display(uchar ba,uchar sh,uchar g) //顯示子程序
{
le=1;
P2=tablewe[1];
le=0;
le=1;
P0=ba;
le=0;
delay1(1);
le=1;
P2=tablewe[2];
le=0;
le=1;
P0=sh;
le=0;
delay1(1);
le=1;
P2=tablewe[3];
le=0;
le=1;
P0=g;
le=0;
delay1(1);
}
void delay1(uchar a) { //延時子函數(shù)
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--) ;
}
評論