每一次按下k3,屏幕右下角会出现一个2
代码如下
#include
#include
#define uint unsigned int
#define uchar unsigned char
#define lcddatapins P0
***it DSIO=P3^4;
***it RST=P3^5;
***it SCLK=P3^6;
***it E=P2^7;
***it RW=P2^5;
***it RS=P2^6;
***it K1=P3^1;
***it K2=P3^0;
***it K3=P3^2;
***it K4=P3^3;
uchar setstate,setplace;
//---DS1302写入和读取时分秒的地址命令---//
//---秒分时日月周年 最低位读写位;-------//
uchar code READ_RTC_ADDR[7] = {0x81, 0x83, 0x85, 0x87, 0x89, 0x8b, 0x8d};
uchar code WRITE_RTC_ADDR[7] = {0x80, 0x82, 0x84, 0x86, 0x88, 0x8a, 0x8c};
//---DS1302时钟初始化2013年1月1日星期二12点00分00秒。---//
//---存储顺序是秒分时日月周年,存储格式是用BCD码---//
uchar tiME[7]= {0, 0, 0x12, 0x01, 0x01, 0x02, 0x14};
void ds1302write(uchar addr,uchar dat)
{
uchar n;
RST=0;
_nop_();
SCLK=0;
_nop_();
RST=1;
_nop_();
for(n=0;n<8;n++)
{
DSIO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
for(n=0;n<8;n++)
{
DSIO=dat&0x01;
dat>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
RST=0;
_nop_();
}
uchar ds1302read(uchar addr)
{
uchar n,dat,dat1;
RST=0;
_nop_();
SCLK=0;
_nop_();;
RST=1;
_nop_();
for(n=0;n<8;n++)
{
DSIO=addr&0x01;
addr>>=1;
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
for(n=0;n<8;n++)
{
dat1=DSIO;
dat=(dat>>1)|(dat1<<7);
SCLK=1;
_nop_();
SCLK=0;
_nop_();
}
RST=0;
_nop_();
SCLK=1;
_nop_();
DSIO=0;
_nop_();
DSIO=1;
_nop_();
return dat;
}
void ds1302init()
{
uchar n;
ds1302write(0x8e,0x00);
for(n=0;n<7;n++)
{
ds1302write(WRITE_RTC_ADDR[n],TIME[n]);
}
ds1302write(0x8e,0x80);
}
void ds1302ReadTime()
{
uchar n;
for(n=0;n<7;n++)
{
TIME[n]=ds1302read(READ_RTC_ADDR[n]);
}
}
void delay1ms(uint c)
{
uchar a;
for(;c>0;c--)
for(a=110;a>0;a--);
}
void lcdwritecom(uchar com)
{
E=0;
RS=0;
RW=0;
lcddatapins=com;
delay1ms(1);
E=1;
delay1ms(5);
E=0;
}
void lcdwritedata(uchar dat)
{
E=0;
RS=1;
RW=0;
lcddatapins=dat;
delay1ms(1);
E=1;
delay1ms(5);
E=0;
}
void lcdinit()
{
lcdwritecom(0x38);
lcdwritecom(0x0c);
lcdwritecom(0x06);
lcdwritecom(0x01);
lcdwritecom(0x80);
}
void lcddisplay()
{
lcdwritecom(0x80+0x40);
lcdwritedata('0'+TIME[2]/16);
lcdwritedata('0'+(TIME[2]&0X0F));
lcdwritedata(':');
lcdwritedata('0'+TIME[1]/16);
lcdwritedata('0'+(TIME[1]&0X0F));
lcdwritedata(':');
lcdwritedata('0'+TIME[0]/16);
lcdwritedata('0'+(TIME[0]&0X0F));
lcdwritecom(0x80);
lcdwritedata('2');
lcdwritedata('0');
lcdwritedata('0'+TIME[6]/16); //年
lcdwritedata('0'+(TIME[6]&0x0f));
lcdwritedata('-');
lcdwritedata('0'+TIME[4]/16); //月
lcdwritedata('0'+(TIME[4]&0x0f));
lcdwritedata('-');
lcdwritedata('0'+TIME[3]/16); //日
lcdwritedata('0'+(TIME[3]&0x0f));
lcdwritecom(0x8D);
lcdwritedata('0'+(TIME[5]&0x07));
}
void int0configuration()
{
IT0=1;
EX0=1;
EA=1;
}
void main()
{
uchar i;
int0configuration();
lcdinit();
ds1302init();
while(1)
{
if(setstate==0)
ds1302ReadTime();
else
{
if(K1==0)
{
delay1ms(50);
if(K1==0)
{
setplace++;
if(setplace>=7)
setplace=0;
}
while((i<50)&&(K1==0))
{
delay1ms(10);
i++;
}
i=0;
}
if(K2==0)
{
delay1ms(50);
if(K2==0)
{
TIME[setplace]++;
if((TIME[setplace]&0x0f)>9)
TIME[setplace]+=6;
if((TIME[setplace]>=0x60)&&(setplace<2))
TIME[setplace]=0;
if((TIME[setplace]>=0x24)&&(setplace==2))
TIME[setplace]=0;
if((TIME[setplace]>=0x32)&&(setplace==3))
TIME[setplace]=0;
if((TIME[setplace]>=0x13)&&(setplace==4))
TIME[setplace]=0;
if((TIME[setplace]>=0x7)&&(setplace==5))
TIME[setplace]=1;
}
}
while((i<50)&&(K2==0))
{
delay1ms(5);
i++;
}
i=0;
}
lcddisplay();
}
}
void int0() interrupt 0
{
delay1ms(10);
if(K3==0)
{
delay1ms(50);
if(K3==0)
{
setstate=~setstate;
setplace=0;
ds1302init();
}
}
}
|