我编个时钟程序,可是那个AT24c02一直不工作,让我老郁闷。。。。那位高手给看看程序,小弟我实在是找不出来是哪里出错了
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit lcden=P3^4;
sbit dula=P2^6;
sbit wela=P2^7;
sbit key1=P3^5;
sbit key2=P3^6;
sbit key3=P3^7;
sbit feng=P2^3;
sbit sda=P2^0;//定义数据控制线。
sbit scl=P2^1;//定义时钟控制线.
uchar t0,xiaomiao,miao,fen,shifen,baifen,qianfen,i;
void delayms(uint xms)//正常延时
{
uint i,j;
for(i=xms;i>0;i--)
for(j=113;j>0;j--);
}
void delay()//微妙延时
{;;}
/*蜂鸣器设置 */
void fengming()
{
feng=0;
delayms(10);
feng=1;
delayms(10);
}
/*液晶读写程序 */
void write_cmd(uchar cmd)
{
key1=0;
P0=cmd;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
void write_date(uchar date)
{
key1=1;
P0=date;
delayms(5);
lcden=1;
delayms(5);
lcden=0;
}
/*键控程序段*/
void keyscan()//设两个变量作为加减项
{
if(key2==0)
{
delayms(10);
if(key2==0)
{
TR0=0;
fengming();
i++;
if(i>3)
{
i=0;
TR0=1;
}
while(!key2);
}
}
if(key3==0)
{
delayms(10);
if(key3==0)
{
fengming();
if(i==1)
xiaomiao++;
if(i==2)
fen++;
if(i==3)
baifen++;
while(!key3);
}
}
}
/*AT24c02程序 */
void start()
{
sda=1;
delay();
scl=1;
delay();
sda=0;
delay();
}
void stop()
{
sda=0;
delay();
scl=1;
delay();
sda=0;
delay();
}
void respons()
{
uchar i;
scl=1;
delay();
while((sda==1)&&(i<255))
i++;
scl=0;
delay();
}
void write_byte(uchar date)
{
uchar j,temp;
temp=date;
for(j=0;j<8;j++)
{
scl=0;
temp=temp<<1;
delay();
sda=CY;
delay();
scl=1;
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=0;
delay();
k=k<<1|sda;
scl=1;
delay();
}
scl=0;
delay();
sda=1;
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();
write_byte(address);
respons();
date=read_byte();
stop();
return date;
}
void init()//初始化
{
/*定时器初始化*/
TMOD=0x01;
TH0=(65536-45782)/256;
TL0=(65536-45782)%256;
EA=1;
ET0=1;
TR0=1;
/*锁存器初始化*/
dula=0;
wela=0;//由于锁存器的缘故
lcden=0;//使能端工作在高电平,所以开始要定义在低电平
/*AT24c02初始化*/
scl=1;
delay();
sda=1;
delay();
/*1602初始化*/
write_cmd(0x38);
write_cmd(0x0c);
write_cmd(0x06);
write_cmd(0x01);
}
void main()
{
init();
/*读数据程序段*/
xiaomiao=read_add(2);
miao=read_add(1);
fen=read_add(3);
shifen=read_add(4);
baifen=read_add(5);
qianfen=read_add(6);
while(1)
{
keyscan();
if(xiaomiao==10)
{
xiaomiao=0;
miao++;
if(miao==6)
{
miao=0;
fen++;
}
}
if(fen==10)
{
fen=0;
shifen++;
if(shifen==6)
{
shifen=0;
baifen++;
}
}
if(baifen==10)
{
baifen=0;
qianfen++;
}
if((qianfen>=2)&&(baifen>=4))
{
miao=0;
xiaomiao=0;
fen=0;
shifen=0;
baifen=0;
qianfen=0;
}
/*保存数据的程序段*/
write_add(1,xiaomiao);
write_add(2,miao);
write_add(3,fen);
write_add(4,shifen);
write_add(5,baifen);
write_add(6,qianfen);
write_cmd(0x80+0x04);
write_date(0x30+qianfen);// 千分
delayms(50);
write_date(0x30+baifen);//百分
delayms(50);
write_date(':');//:
delayms(50);
write_date(0x30+shifen);//十分
delayms(50);
write_date(0x30+fen);//分
delayms(50);
write_date(':');//:
delayms(50);
write_date(0x30+miao);//秒
delayms(50);
write_date(0x30+xiaomiao); //小秒
delayms(50);
}
}
void t0_time() interrupt 1
{
TH0=(65536-45782)/256;
TL0=(65536-45782)%256;
t0++;
if(t0==20)
{
t0=0;
xiaomiao++;
}
} |