实时时钟DS12C887C实例程序.docx
《实时时钟DS12C887C实例程序.docx》由会员分享,可在线阅读,更多相关《实时时钟DS12C887C实例程序.docx(19页珍藏版)》请在冰豆网上搜索。
实时时钟DS12C887C实例程序
DS12C887实例程序
#include
#defineucharunsignedchar
#defineuintunsignedint
ucharnian,yue,ri,day,shi,fen,miao,ashi,afen,sshi,sfen;
uchars1num,flag,flag1,flag2,flag3,flag4,settime,setalarm;
/********************************************************************
引脚配置定义GNDVCCVLRSR/WED0D1D2D3D4D5D6D7BLABLK
*********************************************************************/
#defineLCDIOP1
sbitLCD1602_RS=P3^5;
sbitLCD1602_RW=P3^4;
sbitLCD1602_EN=P3^3;
sbits1=P2^1;
sbits2=P2^2;
sbits3=P2^0;
sbits4=P2^3;
/********************************************************************/
voidLCD_delay(void);
voiddelay_ms(unsignedintn);
ucharchange_bcd(uchara);
voidLCD_en_command(unsignedcharcommand);
voidLCD_en_dat(unsignedchartemp);
voidLCD_set_xy(unsignedcharx,unsignedchary);
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s);
voidLCD_init(void);
voidwrite_sfm(uchar,uchar);
voidsetup_time();
voidsetup_alarm();
/********************LCDPART*************************************/
voidLCD_delay(void)
{
_nop_();
_nop_();
_nop_();
}
voiddelay_ms(uintz)
{
uintx,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/******************************************************************/
ucharchange_bcd(uchara)//D0~D7转换程序
{
chartemp,i;
for(i=0;i<8;i++)
{
temp<<=1;
temp|=(a&0x01);
a>>=1;
}
return(temp);
}
/******************************************************************/
voidLCD_en_command(unsignedcharcommand)
{
command=change_bcd(command);
command=_crol_(command,4);
LCD1602_RS=0;
LCD1602_EN=0;
LCDIO&=0X0f;
LCDIO|=(command&0xf0);
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO&=0X0f;
LCDIO|=(command<<4)&0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
delay_ms
(1);
}
voidLCD_en_dat(unsignedchardat)
{
dat=change_bcd(dat);
dat=_crol_(dat,4);
LCD1602_RS=1;
LCD1602_EN=0;
LCDIO&=0X0f;
LCDIO|=(dat&0xf0);
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
LCDIO&=0X0f;
LCDIO|=(dat<<4)&0xf0;
LCD1602_EN=1;
LCD_delay();
LCD1602_EN=0;
delay_ms
(1);
}
/********************************************************************/
voidLCD_set_xy(unsignedcharx,unsignedchary)
{
unsignedcharaddress;
if(y==0)
address=0x80+x;
else
address=0xC0+x;
LCD_en_command(address);
}
/********************************************************************/
voidLCD_write_string(unsignedcharX,unsignedcharY,unsignedchar*s)
{
LCD_set_xy(X,Y);//setaddress
while(*s)//writecharacter
{
LCD_en_dat(*s);
s++;
}
}
/********************************************************************/
voidLCD_init(void)
{
LCD1602_RW=0;
LCD_en_command(0x01);
delay_ms(5);
LCD_en_command(0x01);
delay_ms(5);
LCD_en_command(0x28);
delay_ms(5);
LCD_en_command(0x28);
delay_ms(5);
LCD_en_command(0x28);
delay_ms(5);
LCD_en_command(0x0C);
delay_ms(5);
LCD_en_command(0x80);
delay_ms(5);
LCD_en_command(0x01);
delay_ms(5);
}
/********************************************************************/
voidwrite_sfm(ucharadd,uchardate)
{
ucharshi,ge;
shi=date/10;
ge=date%10;
LCD_en_command(add);
LCD_en_dat(shi+'0');
LCD_en_dat(ge+'0');
}
/*****************************SETDAY&TIME***************************************/
voidsetup_time()
{
if(s1==0)
{
delay_ms(5);
if(s1==0)
{
s1num++;
TR0=0;//关定时器
flag=1;flag1=1;flag3=1;flag4=1;//停止扫描DS12C887_read_time,setup_alarm
while(!
s1);
if(s1num==1)
{
LCD_en_command(0x82);
LCD_en_command(0x0f);
}
}
if(s1num==2)
{
LCD_en_command(0x85);
}
if(s1num==3)
{
LCD_en_command(0x88);
}
if(s1num==4)
{
LCD_en_command(0x8b);
}
if(s1num==5)
{
LCD_en_command(0xc0);
}
if(s1num==6)
{
LCD_en_command(0xc3);
}
if(s1num==7)
{
LCD_en_command(0xc6);
}
if(s1num==8)
{
s1num=0;
TR0=1;//开定时器
flag1=0;flag3=0;flag4=0;//执行扫描setup_alarm
settime=1;//执行settime
LCD_en_command(0x0c);
}
}
if(s1num!
=0)
{
/********************************************************************/
if(s2==0)
{
delay_ms
(2);
if(s2==0)
{
while(!
s2);
if(s1num==1)
{
nian++;
if(nian==100)
miao=0;
write_sfm(0x82,nian);
LCD_en_command(0x82);
}
if(s1num==2)
{
yue++;
if(yue==13)
yue=0;
write_sfm(0x85,yue);
LCD_en_command(0x85);
}
if(s1num==3)
{
ri++;
if(ri==32)
ri=0;
write_sfm(0x88,ri);
LCD_en_command(0x88);
}
if(s1num==4)
{
day++;
if(day==8)
day=1;
write_sfm(0x8b,day);
LCD_en_command(0x8b);
}
if(s1num==5)
{
shi++;
if(shi==24)
shi=0;
write_sfm(0xc0,shi);
LCD_en_command(0xc0);
}
if(s1num==6)
{
fen++;
if(fen==60)
fen=0;
write_sfm(0xc3,fen);
LCD_en_command(0xc3);
}
if(s1num==7)
{
miao++;
if(miao==60)
miao=0;
write_sfm(0xc6,miao);
LCD_en_command(0xc6);
}
}
}
/********************************************************************/
if(s3==0)
{
delay_ms
(2);
if(s3==0)
{
while(!
s3);
if(s1num==1)
{
nian--;
if(nian==-1)
nian=99;
write_sfm(0x82,nian);
LCD_en_command(0x82);
}
if(s1num==2)
{
yue--;
if(yue==0)
yue=12;
write_sfm(0x85,yue);
LCD_en_command(0x85);
}
if(s1num==3)
{
ri--;
if(ri==0)
ri=31;
write_sfm(0x88,ri);
LCD_en_command(0x88);
}
if(s1num==4)
{
day--;
if(day==0)
day=7;
write_sfm(0x8b,day);
LCD_en_command(0x8b);
}
if(s1num==5)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(0xc0,shi);
LCD_en_command(0xc0);
}
if(s1num==6)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(0xc3,fen);
LCD_en_command(0xc3);
}
if(s1num==7)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(0xc6,miao);
LCD_en_command(0xc6);
}
}
}
/********************************************************************/
}
}
/********************************************************************/
/********************************************************************/
voidsetup_alarm()
{
if(s4==0)
{
delay_ms(5);
if(s4==0)
{
s1num++;
TR0=0;//关定时器
flag=1;flag2=1;flag3=1;flag4=1;//停止扫描DS12C887_read_time,setup_time
while(!
s4);
if(s1num==1)
{
LCD_en_command(0x01);
LCD_write_string(0,0,"Startalarmat");
write_sfm(0xcb,ashi);
write_sfm(0xce,afen);
LCD_write_string(0x0d,1,":
");
LCD_en_command(0xcb);
LCD_en_command(0x0f);
}
}
if(s1num==2)
{
LCD_en_command(0xce);
}
if(s1num==3)
{
LCD_en_command(0x01);
LCD_write_string(0,0,"Stopalarmat");
write_sfm(0xcb,sshi);
write_sfm(0xce,sfen);
LCD_write_string(0x0d,1,":
");
LCD_en_command(0xcb);
LCD_en_command(0x0f);
}
if(s1num==4)
{
LCD_en_command(0xce);
}
if(s1num==5)
{
s1num=0;
TR0=1;//开定时器
flag2=0;flag3=0;flag4=0;//执行扫描setup_time
setalarm=1;//执行setalarm
LCD_en_command(0x01);//清LCD屏幕
LCD_write_string(0,0,"20--");
LCD_write_string(2,1,":
:
");
LCD_en_command(0x0c);
}
}
if(s1num!
=0)
{
/********************************************************************/
if(s2==0)
{
delay_ms(5);
if(s2==0)
{
while(!
s2);
if(s1num==1)
{
ashi++;
if(ashi==24)
ashi=0;
write_sfm(0xcb,ashi);
LCD_en_command(0xcb);
}
if(s1num==2)
{
afen++;
if(afen==60)
afen=0;
write_sfm(0xce,afen);
LCD_en_command(0xce);
}
if(s1num==3)
{
sshi++;
if(sshi==24)
sshi=0;
write_sfm(0xcb,sshi);
LCD_en_command(0xcb);
}
if(s1num==4)
{
sfen++;
if(sfen==60)
sfen=0;
write_sfm(0xce,sfen);
LCD_en_command(0xce);
}
}
}
/********************************************************************/
if(s3==0)
{
delay_ms(5);
if(s3==0)
{
while(!
s3);
if(s1num==1)
{
ashi--;
if(ashi==-1)
ashi=23;
write_sfm(0xcb,ashi);
LCD_en_command(0xcb);
}
if(s1num==2)
{
afen--;
if(afen==-1)
afen=59;
write_sfm(0xce,afen);
LCD_en_command(0xce);
}
if(s1num==3)
{
sshi--;
if(sshi==-1)
sshi=23;
write_sfm(0xcb,sshi);
LCD_en_command(0xcb);
}
if(s1num==4)
{
sfen--;
if(sfen==-1)
sfen=59;
write_sfm(0xce,sfen);
LCD_en_command(0xce);
}
}
}
/********************************************************************/
}
}