at2402程序的问题,
最近在学习《郭天祥老师10天学会51单片机和C语言》视频第9课最后留个课后作业:让两位数码管自动增长,当断电时自动存入AT24C02A,上电时还能自动在数码管上显示断电前的数字,并继续自动增长。以下是我写的程序,当上电时先显示“00”后在继续增长,不知为何,还请高手指教。[br]
#include<reg52.h> //52单片机头文件
#include<intrins.h>
#define uint unsigned int //宏定义
#define uchar unsigned char //宏定义
sbit sda=P3^7;
sbit scl=P3^6;
uchar numwe,ge,shi,temp,temp1,aa,t1,a;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x98,0x88,0x80,
0xc6,0xc0,0x86,0x8e}; //0,1,2,3,4,5,6,7,8,9,a,b,c,e,f
uchar code tablewe[]={
0xfe,0xfd,0xfb,0xf7};
void delay()
{ ;; }
void start() //开始信号
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop() //停止
{
sda=0;
delay();
scl=1;
delay();
sda=1;
delay();
}
void respons() //应答
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<250))i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar i,temp1;
temp1=date;
for(i=0;i<8;i++)
{
temp1=temp1<<1;
scl=0;
delay();
sda=CY;
delay();
scl=1;
delay();
//scl=0;
// delay();
}
scl=0;
delay();
sda=1;
delay();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay();
sda=1;
delay();
for(i=0;i<8;i++)
{
scl=1;
delay();
k=(k<<1)|sda;
scl=0;
delay();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
void delay1(uint z) //延时子函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void display(uchar ge,uchar shi)
{
if(numwe==2)
numwe=0;
P2=tablewe[numwe];
P0=table[shi];
numwe++;
delay1(5);
P2=tablewe[numwe];
P0=table[ge];
numwe++;
delay1(5);
}
void init()
{
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
sda=1;
delay();
scl=1;
delay();
temp=read_add(23);
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
aa++;
}
void main() //主函数
{
init();//初始化子程序
while(1)
{
if(aa==20)
{
aa=0;
temp++;
write_add(23,temp);
delay1(100);
if(temp==99)
{
temp=0;
}
shi=temp%100/10;
ge=temp%10;
}
display(ge,shi);
}
}
[br]
连接图[br]
[br]
[解决办法]
中断服务程序中得把当时的时间值放在EEPROM,然后再主函数的初始化程序上读EEPROM放时间的地址用显示代码刷一遍时间!
[解决办法]
呵呵,点“结贴”……建议一下LZ(52内置了EEPROM,可以设置寄存器来使用它)不需要买个外围芯片,比较经济,学得也多