想要实现的功能:
利用定时器0产生一个0~99秒变化的秒表,并且显示在数码管上,每过一秒这个变化的数写在AT24C02芯片内部,当关闭 电源,再次打开电源时, 单片机先从AT24C02中将原来写入的数读取出来,接着次数变化并显示在数码管上。
实际的情况,最初我从地址2开始读起,后来断电后一直从40读起,,具体的情况见gif。自己调试许久没有答案,故来此请教大神
代码有些长,希望大神指点
#include
#include
#define uchar unsigned char
#define uint unsigned int
***it wela=P2^7;
***it dula=P2^6;
***it sda=P2^0;
***it scl=P2^1;
uchar count,sec;
bit write=0;
unsigned char code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,
0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
void init();
void start();
void end();
void respond();
void delay();
void delayms(uint a);
void write_byte();
uchar read_byte();
void write_add(uchar address,uchar date);
uchar read_add(uchar address);
void display(uchar ,uchar);
void main()
{
init();
sec=read_add(2);
if(sec>99)
sec=0;
while(1)
{
display(sec/10,sec%10);
if(write==1)
{
write=0;
write_add(2,sec);
}
}
}
void init()
{
sda=1;
delay();
scl=1;
delay();
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;
ET0=1;
TR0=1;
}
void display(uchar shi,uchar ge)
{
wela=1;
P0=0xfe;
wela=0;
P0=0xff;
dula=1;
P0=table[shi];
dula=0;
delayms(1);
P0=0xff;
wela=1;
P0=0xfd;
wela=0;
P0=0xff;
dula=1;
P0=table[ge];
dula=0;
delayms(1);
}
void delayms(uint a)
{
uint i,j;
for(i=a;i>0;i--)
for(j=110;j>0;j--);
}
void delay()
{
_nop_();_nop_();_nop_();_nop_();
}
void start()
{
sda=1;
scl=1;
delay();
sda=0;
delay();
scl=0;
}
void end()
{
sda=0;
scl=1;
delay();
sda=1;
delay();
}
void respond()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250)) i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i;
for(i=8;i>0;i--)
{
date=date<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar k,i;
scl=0;
delay();
sda=1;
delay();
for(i=8;i>0;i--)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respond();
write_byte(address);
respond();
write_byte(date);
respond();
}
uchar read_add(uchar address)
{
uchar read;
start();
write_byte(0xa0);
respond();
write_byte(address);
respond();
start();
write_byte(0xa1);
respond();
read=read_byte();
end();
return read;
}
void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
count++;
if(count==20)
{
count=0;
write=1;
sec++;
if(sec==100)
{
sec=0;
}
}
}
0
|
|
|
|