问题是LCD现在是乱显示了,麻烦看看程序哪里出了问题: 207 -01-01
7 :7 :7
#include<reg52.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
uchar code write_table[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar code read_table[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar code table3[]={'0','1','2','3','4','5','6','7','8','9'};
uchar table4[]={0x00,0x12,0x20,0x06,0x04,0x03,0x14};
uchar code table5[]="20 - - ";
uchar table6[];
uchar code table7[]=" : : ";
uint read_flag;
sbit lcdrs = P2^6;
sbit lcdrw = P2^5;
sbit lcden = P2^7;
sbit key1=P2^0;
sbit sda=P3^4;
sbit scl=P3^6;
sbit rst=P3^5;
sbit beep=P1^0;
void init();
void write_rtc(uchar temp);
void write_com(uchar com);
void write_data(uchar dat);
void read_rtc();
void set_rtc();
void tranform();
void write_byte(uchar address,uchar date);
void delay(uint z);
uchar read_byte(uchar address);
void delay(uint ms) //延时程序,精确到0.998ms
{
uint x,y;
for(x=ms;x>0;x--)
for(y=121;y>0;y--);
}
void buzzer() //无源蜂鸣器起振程序
{
beep=1;
delay(5);
beep=0;
delay(5);
}
//对LCD1602 定时器 ds1302 初始化
void init()
{
TMOD|=0x01;
TR0=1;
ET0=1;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
rst=0;
sda=0;
scl=0;
lcden=0;
write_com(0x38);
write_com(0x0e);
write_com(0x06);
write_com(0x01);
write_com(0x80);
}
//以下为lcd1602液晶屏程序
void write_com(uchar com) //lcd1602 写字节
{
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data(uchar dat) //lcd1602 写数据
{
lcdrs=1;
lcdrw=0;
lcden=0;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
//以下为DS1302程序
void write_rtc(uchar temp) //写命令
{
int i;
for(i=0;i<8;i++)
{
scl=0;
sda=temp&0x01;
temp=temp>>1;
scl=1;
}
}
void write_byte(uchar address,uchar date) //写数据
{
rst=0;
_nop_();
rst=1;
_nop_();
write_rtc(address);
write_rtc(date);
rst=0;
}
uchar read_byte(uchar address) //阅读字节
{
uchar i,temp=0x00;
rst=0;
_nop_();
scl=0;
_nop_();
rst=1;
_nop_();
write_rtc(address);
for(i=0;i<8;i++)
{
scl=1;
if(sda)
temp|=0x80;
temp=temp>>1;
scl=0;
}
rst=0;
scl=0;
_nop_();
_nop_();
scl=1;
_nop_();
sda=0;
_nop_();
sda=1;
_nop_();
_nop_();
return temp;
}
void read_rtc() //阅读时钟
{
uchar /*i,*p,*/amiao,afen,ashi,ari,ayue,anian;
/* p=read_table;
for(i=0;i<7;i++)
{
table6
=read_byte(*p);
p++;
} */
amiao=read_byte(0x81);
afen=read_byte(0x83);
ashi=read_byte(0x85);
ari=read_byte(0x87);
ayue=read_byte(0x89);
anian=read_byte(0x8d);
write_com(0x80+2);
write_data(table3[anian>>4]);
write_data(table3[anian&0x0f]);
write_com(0x80+5);
write_data(table3[ayue>>4]);
write_data(table3[ayue&0x0f]);
write_com(0x80+8);
write_data(table3[ari>>4]);
write_data(table3[ari&0x0f]);
write_com(0x80+0x40);
write_data(table3[ashi>>4]);
write_data(table3[ashi&0x0f]);
write_com(0x80+0x40+3);
write_data(table3[afen>>4]);
write_data(table3[afen&0x0f]);
write_com(0x80+0x40+6);
write_data(table3[amiao>>4]);
write_data(table3[amiao&0x0f]);
/* write_com(0x80+2);
write_data(table3[table6[6]>>4]);
write_data(table3[table6[6]&0x0f]);
write_com(0x80+5);
write_data(table3[table6[4]>>4]);
write_data(table3[table6[4]&0x0f]);
write_com(0x80+8);
write_data(table3[table6[3]>>4]);
write_data(table3[table6[3]&0x0f]);
write_com(0x80+0x40);
write_data(table3[table6[2]>>4]);
write_data(table3[table6[2]&0x0f]);
write_com(0x80+0x40+3);
write_data(table3[table6[1]>>4]);
write_data(table3[table6[1]&0x0f]);
write_com(0x80+0x40+6);
write_data(table3[table6[0]>>4]);
write_data(table3[table6[0]&0x0f]);
// buzzer(); */
}
void set_rtc() //设定时间
{
uchar *p,i;
p=write_table;
write_byte(0x8e,0x00);
for(i=0;i<7;i++)
{
write_byte(*p,table4);
p++;
}
/* write_byte(0x80,0x00);
write_byte(0x82,0x01);
write_byte(0x84,0x02);
write_byte(0x86,0x03);
write_byte(0x88,0x04);
write_byte(0x8c,0x05);
write_byte(0x8e,0x80); */
}
void main()
{
uint num;
init();
write_com(0x80);
for(num=0;num<10;num++)
{
write_data(table5[num]);
}
write_com(0x80+0x40);
for(num=0;num<8;num++)
{
write_data(table7[num]);
}
set_rtc();
while(1)
{
// buzzer();
if(read_flag)
{
// buzzer();
read_flag=0;
read_rtc();
}
}
}
void timer0() interrupt 1
{
static int i=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
i++;
if(i==20)
{
i=0;
read_flag=1;
}
}
很厉害,佩服
一周热门 更多>