新手问大神 关于51单片机 AT24C02 写入读出数据异常
时间:10-02
整理:3721RD
点击:
想要实现的功能:
利用定时器0产生一个0~99秒变化的秒表,并且显示在数码管上,每过一秒这个变化的数写在AT24C02芯片内部,当关闭电源,再次打开电源时,单片机先从AT24C02中将原来写入的数读取出来,接着次数变化并显示在数码管上。
实际的情况,最初我从地址2开始读起,后来断电后一直从40读起,,具体的情况见gif。自己调试许久没有答案,故来此请教大神
代码有些长,希望大神指点
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
sbit sda=P2^0;
sbit 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产生一个0~99秒变化的秒表,并且显示在数码管上,每过一秒这个变化的数写在AT24C02芯片内部,当关闭电源,再次打开电源时,单片机先从AT24C02中将原来写入的数读取出来,接着次数变化并显示在数码管上。
实际的情况,最初我从地址2开始读起,后来断电后一直从40读起,,具体的情况见gif。自己调试许久没有答案,故来此请教大神
代码有些长,希望大神指点
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit wela=P2^7;
sbit dula=P2^6;
sbit sda=P2^0;
sbit 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;
}
}
}
24c02是有一个写保护引脚wp的,这个引脚状态对不对
感谢小编
我找到了解决方法,在
一开始加上:sbit CS_DA=P2^4;
在初始化函数中加上 CS_DA=0;
这和AD模块有关吗? 有点想不明白
那个保护引脚wp,我这块板直接接地的
那要看你的板子了,或许板子上的DA和24C02有引脚共用了。
好的,谢谢你的回答