题目:显示000~999跑表,先读数据到
EEPROM,再从EEPROM读出,再在数码管上显示出
来
解决方案:(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) //写入字节子程序
{
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() //读出字节子程序
{
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()//响应子程序
{
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 函数
{
init();
while(1)
{
display(bai,shi,ge);
}
}
void time0() interrupt 1//中断子函数
{
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) { //延时子函数
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--) ;
}