#include<reg51.h>
#include"OLED.H"
#include"define.h"
extern unsigned char code Dot[];
//extern char miao,shi,fen,year,month,day,week;
void delay(uint z)//延时函数
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void init()
{//初始化函数
week=7;
/*以下几行在首次设置DS12C887时使用,以后不必再写入*/
write_ds(0x0A,0x20);//打开振荡器
write_ds(0x0B,0x22);//设置24小时模式,数据BCD码格式
//开启闹铃中断
set_time();//设置上电默认时间
//-----------------------------------------------------
}
void write_ds(uchar add,uchar date)
{//写12C887函数
dscs=0;
dsas=1;
dsds=1;
dsrw=1;
P2=add;//先写地址
dsas=0;
dsrw=0;
P2=date;//再写数据
dsrw=1;
dsas=1;
dscs=1;
}
uchar read_ds(uchar add)
{//读12C887函数
uchar ds_date;
dsas=1;
dsds=1;
dsrw=1;
dscs=0;
P2=add;//先写地址
dsas=0;
dsds=0;
P2=0xff;
ds_date=P2;//再读数据
dsds=1;
dsas=1;
dscs=1;
return ds_date;
}
/*---首次操作12C887时给予寄存器初始化---*/
void set_time()
{//首次上电初始化时间函数
write_ds(0,0x40);
write_ds(1,0);
write_ds(2,0x59);
write_ds(3,0);
write_ds(4,0x23);
write_ds(5,0x0);
write_ds(6,0x7);
write_ds(7,0x31);
write_ds(8,0x12);
write_ds(9,0x23);
}
void main(void)
{
int i;
unsigned char shi1,ge;
LCD_Init();
LCD_CLS();
LCD_P8x16Str(20,4,(u8*)"20 - - ");
LCD_P8x16Str(20,6,(u8*)" : : ");
init();
//Draw_BMP(0,0,100,3,(u8*)Dot);
while(1)
{
year=read_ds(9);//读取12C887数据
month=read_ds(8);
day=read_ds(7);
week=read_ds(6);
shi=read_ds(4);
fen=read_ds(2);
miao=read_ds(0);
shi1=(miao>>4)+0x30;
ge=(miao&0x0f)+0x30;
LCD_P8x16Char(68,6,shi1);
LCD_P8x16Char(76,6,ge);
shi1=(fen>>4)+0x30;
ge=(fen&0x0f)+0x30;
LCD_P8x16Char(44,6,shi1);
LCD_P8x16Char(52,6,ge);
shi1=(shi>>4)+0x30;
ge=(shi&0x0f)+0x30;
LCD_P8x16Char(20,6,shi1);
LCD_P8x16Char(28,6,ge);
shi1=(day>>4)+0x30;
ge=(day&0x0f)+0x30;
LCD_P8x16Char(84,4,shi1);
LCD_P8x16Char(92,4,ge);
shi1=(month>>4)+0x30;
ge=(month&0x0f)+0x30;
LCD_P8x16Char(60,4,shi1);
LCD_P8x16Char(68,4,ge);
shi1=(year>>4)+0x30;
ge=(year&0x0f)+0x30;
LCD_P8x16Char(36,4,shi1);
LCD_P8x16Char(44,4,ge);
LCD_P8x16Char(20,2,week+0x30);
for(i=0;i<20000;i++);
}
}
效果图