51单片机 时钟芯片DS1302 在LCD1602上显示程序问题 找了半天 不知道是哪错了,我估计是时钟芯片的读或写程序有点问题。我把read_rtc();注释掉之后能正常显示23:00:00,应该说明转换和显示程序没错误!!
#include
#include
***it sck=P0^1;
***it io=P0^2;
***it rest=P0^0;
***it rs= P1^0;
***it rw = P1^1;
***it ep = P1^2;
unsigned char code dis1[12] = "0123456789: ";
unsigned char time_data[7]={12,4,6,21,23,0,0,};
unsigned char write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
unsigned char read_add[7]={0x8d,0x8b,0x89,0x87,0x84,0x83,0x81};
unsigned char disp1[8];
void write_byte(unsigned char dat)
{unsigned char i;
for(i=0;i<8;i++)
{ sck=0;
io=dat&0x01;
dat=dat>>1;
sck=1;
}
}
void write(unsigned char add,unsigned char dat)
{ rest=0;
_nop_();
sck=0;
rest=1;
_nop_();
write_byte(add);
write_byte(dat);
rest=0;
_nop_();
sck=1;
io=1;
}
unsigned char read_byte(unsigned char dat)
{ unsigned char i,value;
rest=0;
_nop_();
sck=0;
_nop_();
rest=1;
_nop_();
write_byte(dat);
for(i=0;i<8;i++)
{
value=value>>1;
sck=0;
if(io==1)
value=value|0x80;
sck=1;
}
rest=0;
_nop_();
sck=0;
_nop_();
sck=1;
io=1;
return value;
}
void set_rtc(void)
{ unsigned char i,j;
for(i=0;i<7;i++)
{ j=time_data[i]/10;
j=j*16+time_data[i]%10;
time_data[i]=j;
}
write(0x8e,0);
for(i=0;i<7;i++)
{ write(write_add[i],time_data[i]);
}
write(0x8e,0x80);
}
void read_rtc(void)
{ unsigned char i;
for(i=0;i<7;i++)
{time_data[i]=read_byte(read_add[i]);
}
}
void time_process(void)
{disp1[7]=time_data[6]%16;
disp1[6]=time_data[6]/16;
disp1[5]=10;
disp1[4]=time_data[5]%16;
disp1[3]=time_data[5]/16;
disp1[2]=10;
disp1[1]=time_data[4]%16;
disp1[0]=time_data[4]/16;
}
void delay(unsigned char ms)
{unsigned char i;
while(ms--)
{for(i = 0; i< 250; i++)
{_nop_();
_nop_();
_nop_();
_nop_();
}
}}
bit lcd_bz()
{bit result;
rs = 0;
rw = 1;
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(P3 & 0x80);
ep = 0;
return result;
}
void lcd_wcmd(unsigned char cmd)
{while(lcd_bz());//判断LCD是否忙碌
rs = 0;
rw = 0;
ep = 0;
_nop_();
_nop_();
P3 = cmd;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
void lcd_pos(unsigned char pos)
{lcd_wcmd(pos | 0x80);
}
void lcd_wdat(unsigned char dat)
{
while(lcd_bz());//判断LCD是否忙碌
rs = 1;
rw = 0;
ep = 0;
P3 = dat;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 1;
_nop_();
_nop_();
_nop_();
_nop_();
ep = 0;
}
void lcd_init()
{lcd_wcmd(0x38);
delay(1);
lcd_wcmd(0x0c);
delay(1);
lcd_wcmd(0x06);
delay(1);
lcd_wcmd(0x01);
delay(1);
}
void disp(void)
{
unsigned char m,n;
lcd_init();// 初始化LCD
delay(10);
lcd_pos(0x00);//设置显示位置
for(m=0;m<8;m++)
{n=disp1[m];
lcd_wdat(dis1[n]);//显示字符
}
}
void main(void)
{ set_rtc();
while(1)
{read_rtc();
time_process();
disp();
}}
|