ds1302 1602程序
时间:10-02
整理:3721RD
点击:
最近做数字钟,写了一段1302的程序,用1602显示。还没加按键部分程序。初步调试结果数字显示出错,求助有经验的朋友,分析一下程序出错的根源。谢谢。
程序如下:
#include<reg52.h>
#define uc unsigned char
sbit rs=P3^5;
sbit wr=P3^6; //1602 bit definitions
sbit lcden=P3^4;
sbit dula=P2^6;
sbit wela=P2^7; //bit definitions
sbit sclk=P2^4;
sbit io=P2^5;
sbit rst=P1^1;
sbit A0=ACC^0;
sbit A7=ACC^7;
uc table1[]=" 20-- --/--/---"; //codes
uc table2[]=" --:--:-- ---C ";
uc table3[]="0123456789";
//uc sec1,sec2,min1,min2,hr1,hr2,date1,date2,mon1,mon2,year1,year2,tem,weekday;
uc sec,min,hr,date,mon,year,weekday;
void delay(uc x);
void display_1602();
void write_com1602(uc com);
void write_data1602(uc dat);
void initial_1602();
void initial_1302();
void write_byte1302(uc dat);
uc read_byte1302();
void write_1302(uc add,uc dat);
uc read_1302(uc add);
uc bcd_convert(uc bcd); //16-10
void data_processing(); //read data and convert it
void main()
{
initial_1602();
delay(5);
initial_1302();
delay(5);
while(1)
{
write_1302(0x8e,0x00);
data_processing();
write_1302(0x8e,0x80);
delay(5);
display_1602();
}
}
//* 1602 settings**//
void initial_1602()
{
uc a;
dula=0;
wela=0;
write_com1602(0x38);
//delay(20);
write_com1602(0x0c); //开显示,关光标
// delay(20);
write_com1602(0x06); //光标右移,字符不移
// delay(20);
write_com1602(0x01); //清屏
delay(20);
write_com1602(0x80);
a=0;
while(table1[a]!='\0')
{ //display initial codes
write_data1602(table1[a]);
delay(5);
a++;
}
a=0;
write_com1602(0x80+0x40);
while(table2[a]!='\0')
{
write_data1602(table2[a]);
delay(5);
a++;
}
}
void write_com1602(uc com) //
{
P0=com;
wr=0;
rs=0;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data1602(uc dat) //
{
P0=dat;
wr=0;
rs=1;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void display_1602()
{
write_com1602(0x80+0x03);
// write_data1602(table3[year1]); //year
// write_data1602(table3[year2]);
write_data1602(table3[year/10]);
write_data1602(table3[year%10]);
write_com1602(0x80+0x06);
// write_data1602(table3[mon1]); //month
// write_data1602(table3[mon2]);
write_data1602(table3[mon/10]);
write_data1602(table3[mon%10]);
write_com1602(0x80+0x09); //date
// write_data1602(table3[date1]);
// write_data1602(table3[date2]);
write_data1602(table3[date/10]);
write_data1602(table3[date%10]);
write_com1602(0x80+0x0c); //weekday
switch(weekday)
{
case 1: write_data1602('M');
write_data1602('O');
write_data1602('N');
break;
case 2: write_data1602('T');
write_data1602('U');
write_data1602('E');
break;
case 3: write_data1602('W');
write_data1602('E');
write_data1602('D');
break;
case 4: write_data1602('T');
write_data1602('H');
write_data1602('U');
break;
case 5: write_data1602('F');
write_data1602('R');
write_data1602('I');
break;
case 6: write_data1602('S');
write_data1602('A');
write_data1602('T');
break;
case 7: write_data1602('S');
write_data1602('U');
write_data1602('N');
break;
default: write_data1602('M');
write_data1602('O');
write_data1602('N');
break;
}
write_com1602(0x80+0x40+0x02);
// write_data1602(table3[hr1]); //hour
// write_data1602(table3[hr2]);
write_data1602(table3[hr/10]);
write_data1602(table3[hr%10]);
write_com1602(0x80+0x40+0x05);
// write_data1602(table3[min1]); //minute
// write_data1602(table3[min2]);
write_data1602(table3[min/10]);
write_data1602(table3[min%10]);
write_com1602(0x80+0x40+0x08); //second
// write_data1602(table3[sec1]);
// write_data1602(table3[sec2]);
write_data1602(table3[sec/10]);
write_data1602(table3[sec%10]);
/* write_com1602(0x80+0x40+0x0c); */ //temprature
}
//************************//
void delay(uc x)
{
uc a,b; //delay function
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
//**********1302 settings*******//
void initial_1302()
{
rst=0;
sclk=0;
write_1302(0x8e,0x80); //write enable
write_1302(0x80,0x30); //second
write_1302(0x82,0x31); //miniute
write_1302(0x84,0x19); //hour
write_1302(0x86,0x04); //date
write_1302(0x88,0x11); //month
write_1302(0x8a,0x01); //weekday
write_1302(0x8c,0x13); //year
write_1302(0x8e,0x80); //write protection
}
void write_byte1302(uc dat)
{
uc i;
ACC=dat;
rst=1;
for(i=0;i<8;i++)
{
A0=io;
sclk=0;
sclk=1;
ACC=ACC>>1;
}
}
uc read_byte1302()
{
uc i;
rst=1; //read enable
for(i=0;i<8;i++)
{
ACC=ACC>>1;
sclk=1; //falling pulse to read
sclk=0;
A7=io;
}
return (ACC);
}
void write_1302(uc add,uc dat)
{
rst=0;
sclk=0;
rst=1; // rising pulse to write
write_byte1302(add);
write_byte1302(dat);
sclk=1;
rst=0;
}
uc read_1302(uc add)
{
uc temp;
rst=0;
sclk=0;
rst=1;
write_byte1302(add);
temp=read_byte1302();
sclk=1;
rst=0;
return(temp);
}
uc bcd_convert(uc bcd)//BCD码转十进制函数,输入BCD,返回十进制
{
uc number;
number=((bcd&0xf0)>>4)*10+bcd&0x0f;
return (number);
}
//**********************//
void data_processing() //process details concerning about clock
{
uc temp;
temp=read_1302(0x81); //second processing
// sec1=(temp&0xf0)>>4;
// sec2=temp&0x0f;
sec=bcd_convert(temp);
temp=read_1302(0x83); //minute processing
// min1=(temp&0xf0)>>4;
//min2=temp&0x0f;
min=bcd_convert(temp);
temp=read_1302(0x85); //hour processing
// hr1=(temp&0xf0)>>4;
// hr2=temp&0x0f;
hr=bcd_convert(temp);
temp=read_1302(0x87); //date processin
// date1=(temp&0xf0)>>4;
// date2=temp&0x0f;
date=bcd_convert(temp);
temp=read_1302(0x89); //mon processing
// mon1=(temp&0xf0)>>4;
// mon2=temp&0x0f;
mon=bcd_convert(temp);
temp=read_1302(0x8b); //weekday processing
weekday=bcd_convert(temp);
temp=read_1302(0x8d); //year processing
// year1=(temp&0xf0)>>4;
// year2=temp&0x0f;
year=bcd_convert(temp);
//********convert the initial tables defined above//
}
程序如下:
#include<reg52.h>
#define uc unsigned char
sbit rs=P3^5;
sbit wr=P3^6; //1602 bit definitions
sbit lcden=P3^4;
sbit dula=P2^6;
sbit wela=P2^7; //bit definitions
sbit sclk=P2^4;
sbit io=P2^5;
sbit rst=P1^1;
sbit A0=ACC^0;
sbit A7=ACC^7;
uc table1[]=" 20-- --/--/---"; //codes
uc table2[]=" --:--:-- ---C ";
uc table3[]="0123456789";
//uc sec1,sec2,min1,min2,hr1,hr2,date1,date2,mon1,mon2,year1,year2,tem,weekday;
uc sec,min,hr,date,mon,year,weekday;
void delay(uc x);
void display_1602();
void write_com1602(uc com);
void write_data1602(uc dat);
void initial_1602();
void initial_1302();
void write_byte1302(uc dat);
uc read_byte1302();
void write_1302(uc add,uc dat);
uc read_1302(uc add);
uc bcd_convert(uc bcd); //16-10
void data_processing(); //read data and convert it
void main()
{
initial_1602();
delay(5);
initial_1302();
delay(5);
while(1)
{
write_1302(0x8e,0x00);
data_processing();
write_1302(0x8e,0x80);
delay(5);
display_1602();
}
}
//* 1602 settings**//
void initial_1602()
{
uc a;
dula=0;
wela=0;
write_com1602(0x38);
//delay(20);
write_com1602(0x0c); //开显示,关光标
// delay(20);
write_com1602(0x06); //光标右移,字符不移
// delay(20);
write_com1602(0x01); //清屏
delay(20);
write_com1602(0x80);
a=0;
while(table1[a]!='\0')
{ //display initial codes
write_data1602(table1[a]);
delay(5);
a++;
}
a=0;
write_com1602(0x80+0x40);
while(table2[a]!='\0')
{
write_data1602(table2[a]);
delay(5);
a++;
}
}
void write_com1602(uc com) //
{
P0=com;
wr=0;
rs=0;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_data1602(uc dat) //
{
P0=dat;
wr=0;
rs=1;
lcden=0;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void display_1602()
{
write_com1602(0x80+0x03);
// write_data1602(table3[year1]); //year
// write_data1602(table3[year2]);
write_data1602(table3[year/10]);
write_data1602(table3[year%10]);
write_com1602(0x80+0x06);
// write_data1602(table3[mon1]); //month
// write_data1602(table3[mon2]);
write_data1602(table3[mon/10]);
write_data1602(table3[mon%10]);
write_com1602(0x80+0x09); //date
// write_data1602(table3[date1]);
// write_data1602(table3[date2]);
write_data1602(table3[date/10]);
write_data1602(table3[date%10]);
write_com1602(0x80+0x0c); //weekday
switch(weekday)
{
case 1: write_data1602('M');
write_data1602('O');
write_data1602('N');
break;
case 2: write_data1602('T');
write_data1602('U');
write_data1602('E');
break;
case 3: write_data1602('W');
write_data1602('E');
write_data1602('D');
break;
case 4: write_data1602('T');
write_data1602('H');
write_data1602('U');
break;
case 5: write_data1602('F');
write_data1602('R');
write_data1602('I');
break;
case 6: write_data1602('S');
write_data1602('A');
write_data1602('T');
break;
case 7: write_data1602('S');
write_data1602('U');
write_data1602('N');
break;
default: write_data1602('M');
write_data1602('O');
write_data1602('N');
break;
}
write_com1602(0x80+0x40+0x02);
// write_data1602(table3[hr1]); //hour
// write_data1602(table3[hr2]);
write_data1602(table3[hr/10]);
write_data1602(table3[hr%10]);
write_com1602(0x80+0x40+0x05);
// write_data1602(table3[min1]); //minute
// write_data1602(table3[min2]);
write_data1602(table3[min/10]);
write_data1602(table3[min%10]);
write_com1602(0x80+0x40+0x08); //second
// write_data1602(table3[sec1]);
// write_data1602(table3[sec2]);
write_data1602(table3[sec/10]);
write_data1602(table3[sec%10]);
/* write_com1602(0x80+0x40+0x0c); */ //temprature
}
//************************//
void delay(uc x)
{
uc a,b; //delay function
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
//**********1302 settings*******//
void initial_1302()
{
rst=0;
sclk=0;
write_1302(0x8e,0x80); //write enable
write_1302(0x80,0x30); //second
write_1302(0x82,0x31); //miniute
write_1302(0x84,0x19); //hour
write_1302(0x86,0x04); //date
write_1302(0x88,0x11); //month
write_1302(0x8a,0x01); //weekday
write_1302(0x8c,0x13); //year
write_1302(0x8e,0x80); //write protection
}
void write_byte1302(uc dat)
{
uc i;
ACC=dat;
rst=1;
for(i=0;i<8;i++)
{
A0=io;
sclk=0;
sclk=1;
ACC=ACC>>1;
}
}
uc read_byte1302()
{
uc i;
rst=1; //read enable
for(i=0;i<8;i++)
{
ACC=ACC>>1;
sclk=1; //falling pulse to read
sclk=0;
A7=io;
}
return (ACC);
}
void write_1302(uc add,uc dat)
{
rst=0;
sclk=0;
rst=1; // rising pulse to write
write_byte1302(add);
write_byte1302(dat);
sclk=1;
rst=0;
}
uc read_1302(uc add)
{
uc temp;
rst=0;
sclk=0;
rst=1;
write_byte1302(add);
temp=read_byte1302();
sclk=1;
rst=0;
return(temp);
}
uc bcd_convert(uc bcd)//BCD码转十进制函数,输入BCD,返回十进制
{
uc number;
number=((bcd&0xf0)>>4)*10+bcd&0x0f;
return (number);
}
//**********************//
void data_processing() //process details concerning about clock
{
uc temp;
temp=read_1302(0x81); //second processing
// sec1=(temp&0xf0)>>4;
// sec2=temp&0x0f;
sec=bcd_convert(temp);
temp=read_1302(0x83); //minute processing
// min1=(temp&0xf0)>>4;
//min2=temp&0x0f;
min=bcd_convert(temp);
temp=read_1302(0x85); //hour processing
// hr1=(temp&0xf0)>>4;
// hr2=temp&0x0f;
hr=bcd_convert(temp);
temp=read_1302(0x87); //date processin
// date1=(temp&0xf0)>>4;
// date2=temp&0x0f;
date=bcd_convert(temp);
temp=read_1302(0x89); //mon processing
// mon1=(temp&0xf0)>>4;
// mon2=temp&0x0f;
mon=bcd_convert(temp);
temp=read_1302(0x8b); //weekday processing
weekday=bcd_convert(temp);
temp=read_1302(0x8d); //year processing
// year1=(temp&0xf0)>>4;
// year2=temp&0x0f;
year=bcd_convert(temp);
//********convert the initial tables defined above//
}