#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define write_second 0x80
#define read_second 0x81
#define write_minute 0x82
#define read_minute 0x83
#define write_hour 0x84
#define read_hour 0x85
#define write_day 0x86
#define read_day 0x87
#define write_month 0x88
#define read_month 0x89
#define write_week 0x8a
#define read_week 0x8b
#define write_year 0x8c
#define read_year 0x8d
#define write_protect 0x8e
uchar second_Tab[3];
uchar minute_Tab[3];
uchar hour_Tab[3];
uchar month_Tab[3];
uchar day_Tab[3];
uchar year_Tab[3];
sbit P1_7=P1^7;
sbit RS=P2^6;
sbit RW=P2^5;
sbit EN=P2^7;
sbit CE=P3^5;
sbit DIO=P3^4;
sbit SCLOCK=P3^6;
LCD_CHEKCBUSY();
void delay(uchar x)
{
uchar y,z;
for(y=x;y>0;y--)
for(z=125;z>0;z--);
}
LCD_CHECKBUSY()
{
uchar temp;
RS=0;
RW=1;
EN=0;
delay(5);
EN=1;
temp=(P0&0X80)>>7;
delay(5);
EN=0;
return temp;
}
LCD_CMD(uchar cmd)
{
while(LCD_CHECKBUSY());
RS=0;
RW=0;
EN=0;
delay(5);
EN=1;
P0=cmd;
delay(1);
EN=0;
}
LCD_INITIAL(void)
{
while(LCD_CHECKBUSY());
LCD_CMD(0X38);//双行显示
LCD_CMD(0X06);//光标移动增量方式
LCD_CMD(0X0C);//整体显示开
LCD_CMD(0X01);//清屏
}
LCD_POSITION(uchar pos)
{
while(LCD_CHECKBUSY());
LCD_CMD(pos|0x80);//DDRAM地址
}
LCD_WRITE(uchar dat1)
{
while(LCD_CHECKBUSY());
RS=1;
RW=0;
EN=0;
delay(5);
EN=1;
P0=dat1;
delay(2);
EN=0;
}
LCD_READ(uchar dat2)
{
while(LCD_CHEKCBUSY());
RS=1;
RW=1;
EN=0;
delay(5);
EN=1;
delay(2);
P0=dat2;
delay(5);
EN=0;
}
//对ds_1302进行写入一个字节的数据,首先给予一个命令字节
//然后将数据写入,命令字节和字节数据都是在SCLOCK的上升沿完成
//值得注意的是数据字节dat3应该为BCD码形式
DS_1302WRITE(uchar add1,dat3)
{
uchar i,j,tem1;
CE=0;
SCLOCK=0;
CE=1;
for(i=0;i<8;i++)//命令字节——“写入”
{
tem1=add1;
SCLOCK=0;
DIO=(bit)(tem1&0x08);
add1>>=1;
SCLOCK=1;
}
for(j=0;j<8;j++) //写数据
{
tem1=dat3;
SCLOCK=0;
DIO=(bit)(tem1&0x01);
dat3>>=1;
SCLOCK=1;
}
CE=0;
}
//对ds_1302读出一个字节的数据(为BCD码形式)并将其转换成十六进制,
//命令字节的读出是在SCLOCK的上升沿,数据读出是在SCLOCK的下降沿
DS_1302READ(uchar add2)
{
uchar r,s,t,tem2,tem3;
if(add2==read_second) t=0;
if(add2==read_minute) t=1;
if(add2==read_hour) t=2;
if(add2==read_year) t=3;
if(add2==read_month) t=4;
if(add2==read_day) t=5;
CE=0;
SCLOCK=0;
CE=1;
for(r=0;r<8;r++)//命令字节——“读出”
{
tem2=add2;
SCLOCK=0;
DIO=(bit)(tem2&0x01);
add2>>=1;
SCLOCK=1;
}
for(s=0;s<8;s++)//读出一个字节的数据
{
SCLOCK=1;
P1_7=DIO;
ACC>>=1;
SCLOCK=0;
}
CE=0;
//二~十进制数BCD码转换为十六进制数
tem2=ACC;
tem3=tem2%10;
tem2=tem2/10;
tem2=tem3+tem2*16;
if(tem2>=10)
{
switch(t)
{
case 0:{second_Tab[0]=tem2%10;second_Tab[1]=tem2/10;}break;
case 1:{minute_Tab[0]=tem2%10;minute_Tab[1]=tem2/10;}break;
case 2:{hour_Tab[0]=tem2%10;hour_Tab[1]=tem2/10;}break;
case 3:{year_Tab[0]=tem2%10;year_Tab[1]=tem2/10;}break;
case 4:{month_Tab[0]=tem2%10;month_Tab[1]=tem2/10;}break;
case 5:{day_Tab[0]=tem2%10;day_Tab[1]=tem2/10;}break;
}
}
else if(tem2<10)
{
switch(t)
{
case 0:second_Tab[0]=tem2%10;break;
case 1:minute_Tab[0]=tem2%10;break;
case 2:hour_Tab[0]=tem2%10;break;
case 3:year_Tab[0]=tem2%10;break;
case 4:month_Tab[0]=tem2%10;break;
case 5:day_Tab[0]=tem2%10;break;
}
}
}
DS_1302_INITIAL()
{
DS_1302WRITE(write_protect,0x00);
DS_1302WRITE(write_second,0x00);
DS_1302WRITE(write_minute,0x00);
DS_1302WRITE(write_hour,0x13);
DS_1302WRITE(write_month,0x12);
DS_1302WRITE(write_day,0x12);
DS_1302WRITE(write_year,0x11);
DS_1302WRITE(write_protect,0x80);
}
void main()
{
DS_1302_INITIAL();
LCD_INITIAL();
while(1)
{
LCD_POSITION(0X01);//LCD上第一行空一格开始显示日历
LCD_WRITE(year_Tab[1]+48);//读ds_1302年,并在LCD上显示
LCD_POSITION(0X02);
LCD_WRITE(year_Tab[0]+48);
LCD_POSITION(0X03);
LCD_WRITE(47);//在年显示后显示“/”符号
LCD_POSITION(0X04);
LCD_WRITE(month_Tab[1]+48);//读ds_1302月,并在LCD上显示
LCD_POSITION(0X05);
LCD_WRITE(month_Tab[0]+48);
LCD_POSITION(0X06);
LCD_WRITE(47);//在月显示后显示“/”符号
LCD_POSITION(0X07);
LCD_WRITE(day_Tab[1]+48);//读ds_1302日,并在LCD上显示
LCD_POSITION(0X08);
LCD_WRITE(day_Tab[0]+48);
LCD_POSITION(0X09);
LCD_WRITE(47);//在年显示后显示“/”符号
LCD_POSITION(0X41);//在LCD第二行空一格开始显示时间
LCD_WRITE(hour_Tab[1]+48);//读ds_1302小时,并在LCD上显示
LCD_POSITION(0X42);
LCD_WRITE(hour_Tab[0]+48);
LCD_POSITION(0X43);
LCD_WRITE(58);//在小时后面显示“:”
LCD_POSITION(0X44);
LCD_WRITE(minute_Tab[1]+48);//读ds_1302分钟,并在LCD上显示
LCD_POSITION(0X45);
LCD_WRITE(minute_Tab[0]+48);
LCD_POSITION(0X46);
LCD_WRITE(58);//在分钟后面显示“:”
LCD_POSITION(0X47);
LCD_WRITE(second_Tab[1]+48);//读ds_1302秒钟,并在LCD上显示
LCD_POSITION(0X48);
LCD_WRITE(second_Tab[0]+48);
}
}
此帖出自
小平头技术问答
一周热门 更多>