谁能帮我把这个程序里的错误找出来吗,我已经弄了1天了,没找到哪错了,在
开发板上也显示不出来
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
#define ds1302_sec_add 0x80 //秒数据地址
#define ds1302_min_add 0x82 //分数据地址
#define ds1302_hr_add 0x84 //时数据地址
#define ds1302_date_add 0x86 //日数据地址
#define ds1302_month_add 0x88 //月数据地址
#define ds1302_day_add 0x8a //星期数据地址
#define ds1302_year_add 0x8c //年数据地址
#define ds1302_control_add 0x8e //控制数据地址
uchar
time_buf[7]={15,11,22,23,37,45,4};//年月日时分秒周
uchar time[8]={" : : "};
uchar date[10]={"20 - - "};
uchar week[3]={" "};
uchar weeklist[]={"SunMonTueWedThuFriSat"};
uchar time_buf1[7];//年月日时分秒周
uchar time_buf2[12];
sbit sck=P3^6;
sbit io=P3^4;
sbit rst=P3^5;
sbit LEN=P2^7;
sbit RS=P2^6;
sbit RW=P2^5;
void write_byte(uchar addr,uchar dat)
{
uchar i;
sck=0;
rst=1;
addr=addr&0xfe;
for(i=0;i<8;i++)
{
if(addr&0x01)
{
io=1;
}
else
{
io=0;
}
addr=addr>>1;
sck=1;
sck=0;
}
sck=0;
for(i=0;i<8;i++)
{
if(dat&0x01)
{
io=1;
}
else
{
io=0;
}
dat=dat>>1;
sck=1;
sck=0;
}
rst=0;
}
uchar read_byte(uchar addr)
{
uchar i,temp;
sck=0;
rst=1;
addr=addr|0x01;
for(i=0;i<8;i++)
{
if(addr&0x01)
{
io=1;
}
else
{
io=0;
}
addr=addr>>1;
sck=1;
sck=0;
}
sck=1;
for(i=0;i<8;i++)
{
temp=temp>>1;
if(io==1)
{
temp=temp|0x80;
}
if(io==0)
{
temp=temp&0x7f;
}
sck=0;
sck=1;
}
rst=0;
return temp;
}
void init_time()
{
uchar i,tmp;
for(i=0;i<7;i++)
{
tmp=time_buf[i]/10;
time_buf1[i]=time_buf[i]%10;
time_buf1[i]=tmp*16+time_buf1[i];
}
write_byte(ds1302_control_add,0x00);
write_byte(ds1302_sec_add,time_buf1[5]);
write_byte(ds1302_min_add,time_buf1[4]);
write_byte(ds1302_hr_add,time_buf1[3]);
write_byte(ds1302_date_add,time_buf1[2]);
write_byte(ds1302_month_add,time_buf1[1]);
write_byte(ds1302_year_add,time_buf1[0]);
write_byte(ds1302_day_add,time_buf1[6]);
write_byte(ds1302_control_add,0x80);
}
void Ds1302_Read_Time(void)
{
unsigned char i,tmp;
time_buf1[0]=read_byte(ds1302_year_add); //年
time_buf1[1]=read_byte(ds1302_month_add); //月
time_buf1[2]=read_byte(ds1302_date_add); //日
time_buf1[3]=read_byte(ds1302_hr_add); //时
time_buf1[4]=read_byte(ds1302_min_add); //分
time_buf1[5]=read_byte(ds1302_sec_add);//秒
time_buf1[6]=read_byte(ds1302_day_add); //周
for(i=0;i<7;i++)
{
tmp=time_buf1[i]/16;
time_buf[i]=time_buf1[i]%16;
time_buf1[i]=tmp*10+time_buf[i];
}
}
void Lcd1602_Delay1ms(uint c) //误差 0us
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void write1602_com(uchar com)
{
RS=0;
RW=0;
LEN=0;
P0=com;
Lcd1602_Delay1ms(1);
LEN=1;
Lcd1602_Delay1ms(1);
LEN=0;
}
void write1602_data(uchar dat)
{
RS=1;
RW=0;
LEN=0;
P0=dat;
Lcd1602_Delay1ms(1);
LEN=1;
Lcd1602_Delay1ms(1);
LEN=0;
}
void DelayMs(uchar t)
{
uchar i;
while(t--)
{
//大致延时1mS
for(i=0;i<250;i++)
for(i=0;i<250;i++);
}
}
void init_1602()
{
write1602_com(0x38); /*显示模式设置*/
DelayMs(5);
write1602_com(0x38);
DelayMs(5);
write1602_com(0x38);
DelayMs(5);
write1602_com(0x38);
write1602_com(0x08); /*显示关闭*/
write1602_com(0x01); /*显示清屏*/
write1602_com(0x06); /*显示光标移动设置*/
DelayMs(5);
write1602_com(0x0C); /*显示开及光标设置*/
}
void display()
{
uchar num;
date[2]=time_buf1[0]/10+0x30;//年
date[3]=time_buf1[0]%10+0x30;
date[5]=time_buf1[1]/10+0x30;//月
date[6]=time_buf1[1]%10+0x30;
date[8]=time_buf1[2]/10+0x30;//日
date[9]=time_buf1[2]%10+0x30;
time[0]=time_buf1[3]/10+0x30;//时
time[1]=time_buf1[3]%10+0x30;
time[3]=time_buf1[4]/10+0x30;//分
time[4]=time_buf1[4]%10+0x30;
time[6]=time_buf1[5]/10+0x30;//秒
time[7]=time_buf1[5]%10+0x30;
week[0]=weeklist[(time_buf1[6]%10)*3];//格式化星期
week[1]=weeklist[(time_buf1[6]%10)*3+1];
week[2]=weeklist[(time_buf1[6]%10)*3+2];
write1602_com(0x80+1);
for(num=0;num<10;num++)
{
write1602_data(date[num]);
}
for(num=0;num<3;num++)
{
write1602_data(week[num]);
}
write1602_com(0x80+0x40);
for(num=0;num<8;num++)
{
write1602_data(time[num]);
}
}
void mian()
{
init_time();
init_1602();
while(1)
{
void Ds1302_Read_Time(void);
display();
}
}
一周热门 更多>