#include
#include
#define uchar unsigned char
#define uint unsigned int
#define rs_1 P5OUT|=BIT2
#define rs_0 P5OUT&=~BIT2
#define rw_1 P5OUT|=BIT1
#define rw_0 P5OUT&=~BIT1
#define en_1 P5OUT|=BIT0
#define en_0 P5OUT&=~BIT0
#define s1 P2IN&BIT0
#define s2 P2IN&BIT1
#define s3 P2IN&BIT2
uchar zhou[28]="Mon TuesWed ThurFri Sat Sun ";
uchar yue,ri,shi,fen,miao,xingqi;
long nian;
void delay(uchar n);
void lcd_init();
void write_com(uchar n);
void write_data(uchar n);
void calculate_week(uint y,uchar m,uchar d);
void write_week(uchar hang,uchar lie,uchar n);
void key_scan();
void write_erweishu(uchar hang,uchar lie,uchar num);
void write_nian(uchar hang,uchar lie,long n);
void delay(uchar n)
{
uchar i,j;
for(i=0;i
for(j=0;j<110;j++);
}
void write_com(uchar n)
{
rs_0;
rw_0;
P4OUT=n;
en_1;
delay(5);
en_0;
}
void write_data(uchar n)
{
rs_1;
rw_0;
P4OUT=n;
en_1;
delay(5);
en_0;
}
void write_erweishu(uchar hang,uchar lie,uchar num)
{
uchar i,shiwei,gewei;
shiwei=num/10;
gewei=num%10;
if(hang==1)i=0;
if(hang==2)i=0x40;
write_com(0x80+i+lie-1);
write_data(0x30+shiwei);
write_data(0x30+gewei);
}
void write_nian(uchar hang,uchar lie, long n)
{
uchar i,ge,shi,bai,qian;
if(hang==1)i=0;
if(hang==2)i=0x40;
write_com(0x80+i+lie-1);
qian=n/1000;
bai=(n/100)%10;
shi=(n/10)%10;
ge=n%10;
write_data(0x30+qian);
write_data(0x30+bai);
write_data(0x30+shi);
write_data(0x30+ge);
}
void calculate_week(uint y,uchar m,uchar d)
{
if(m==1||m==2)
{
m+=12;
y--;
}
xingqi=(d+1+2*m+3*(m+1)/5+y+y/4-y/100+y/400)%7;
}
void write_week(uchar hang,uchar lie,uchar n)
{
uchar k,i;
if(hang==1)i=0;
if(hang==2)i=0x40;
write_com(0x80+i+lie-1);
if(n!=0) //xingqi=1~6的时候是和周几对应的
{
for(k=4*n-4;k<4*n;k++)
write_data(zhou[k]);
}
else //xingqi=0的时候是周日
{
for(k=24;k<28;k++)
write_data(zhou[k]);
}
}
void lcd_init()
{
en_0;
write_com(0x38); //设置16x2 显示5x7 点阵,8 位数据接口
write_com(0x0c); //设置开始显示不显示光标
write_com(0x06); //写一个字符后地址指针加1
write_com(0x01); //lcd1602清屏
write_com(0x80); //设置起始位置
}
void key_scan()
{
uchar s1_num,run;
while(1)
{
if(s1==0) //s1键用来移动光标,配合s2(调大时间),s3(调小时间)调整时间
{
delay(5); //按键去抖
if(s1==0)
{
CCTL0&=~CCIE; //一旦s1键按下,禁止CCR0中断
++s1_num;
switch(s1_num)
{
case 1: write_com(0x80+3);write_com(0x0f);break; //调整年
case 2: write_com(0x80+6);break; //调整月
case 3: ri=28;write_erweishu(1,9,ri);write_com(0x80+9);break; //调整日
case 4: write_com(0x80+0x40+5);break; //调整时
case 5: write_com(0x80+0x40+8);break; //调整分
case 6: write_com(0x80+0x40+11);break; //调整秒
case 7: write_com(0x38);write_com(0x0c);write_com(0x06);s1_num=0;
calculate_week(nian,yue,ri); write_week(1,13,xingqi);CCTL0=CCIE;break;//CCR0中断允许,重新开始计时
}
while(s1==0); //这个while是用来防止s1长时间按下而导致的按键执行程序不断循环的现象
}
}
if((P2IN&BIT1)==0)
{
delay(5);
if((P2IN&BIT1)==0)
{
while((P2IN&BIT1)==0);
if(s1_num==1) //调大年
{
write_nian(1,1,++nian);
write_com(0x80+3);
}
if(s1_num==2) //调大月
{
if(yue++==12)yue=1;
write_erweishu(1,6,yue);
write_com(0x80+6);
}
if(s1_num==3) //调大日
{
if(nian%400==0||(nian%100!=0&&nian%4==0))run=1; //判断是否为闰年
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri++==31)ri=1;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri++==30)ri=1;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
if(yue==2)
{
if(run==1)
{
run=0;
if(ri++==29)ri=1;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
else
{
if(ri++==28)ri=1;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
}
}
if(s1_num==4) //调大时
{
if(shi++==23)shi=0;
{
write_erweishu(2,5,shi);
write_com(0x80+0x40+5);
}
}
if(s1_num==5) //调大分
{
if(fen++==59)fen=0;
{
write_erweishu(2,8,fen);
write_com(0x80+0x40+8);
}
}
if(s1_num==6) //调大秒
{
if(miao++==59)miao=0;
{
write_erweishu(2,11,miao);
write_com(0x80+0x40+11);
}
}
}
}
if((P2IN&BIT2)==0)
{
delay(5);
if((P2IN&BIT2)==0)
{
while((P2IN&BIT2)==0);
if(s1_num==1) //调小年
{
write_nian(1,1,--nian);
write_com(0x80+3);
}
if(s1_num==2) //调小月
{
if(yue--==1)yue=12;
write_erweishu(1,6,yue);
write_com(0x80+6);
}
if(s1_num==3) //调小日
{
if(nian%400==0||(nian%100!=0&&nian%4==0))run=1; //判断是否为闰年
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri--==1)ri=31;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri--==1)ri=30;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
if(yue==2)
{
if(run==1)
{
run=0;
if(ri--==1)ri=29;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
else
{
if(ri--==1)ri=28;
write_erweishu(1,9,ri);
write_com(0x80+9);
}
}
}
if(s1_num==4) //调小时
{
if(shi--==0)shi=23;
{
write_erweishu(2,5,shi);
write_com(0x80+0x40+5);
}
}
if(s1_num==5) //调小分
{
if(fen--==0)fen=59;
{
write_erweishu(2,8,fen);
write_com(0x80+0x40+8);
}
}
if(s1_num==6) //调小秒
{
if(miao--==0)miao=59;
{
write_erweishu(2,11,miao);
write_com(0x80+0x40+11);
}
}
}
}
}
}
void main()
{
WDTCTL=WDTPW+WDTHOLD;
P4DIR=0xff;
P5DIR=0x07;
P2DIR=0x00;
CCTL0=CCIE;
CCR0=32768;
TACTL=TASSEL_1+MC_1;
_BIS_SR(GIE);
nian=2014;yue=12;ri=31;
shi=23;fen=59;miao=45;
lcd_init();
write_nian(1,1,nian);write_data('-'); //keil uVsion4(uVsion3没有)在这里似乎有一个bug,如果直接用write_nian(1,1,nian)显示2014的话,它只会显示0000;
write_erweishu(1,6,yue);write_data('-');
write_erweishu(1,9,ri);
calculate_week(nian,yue,ri); write_week(1,13,xingqi); //写星期几
write_erweishu(2,5,shi); write_data(':');
write_erweishu(2,8,fen); write_data(':');
write_erweishu(2,11,miao);
key_scan();
}
#pragma vector=tiMERA0_VECTOR
__interrupt void Timer_A()
{
uchar run; //判断是否为闰年的时候用到这个变量
if(miao++==59)
{
miao=0;
if(fen++==59)
{
fen=0;
if(shi++==23)
{
shi=0;
if(nian%400==0||(nian%100!=0&&nian%4==0))run=1; //判断是否为闰年
if(yue==1||yue==3||yue==5||yue==7||yue==8||yue==10||yue==12)
{
if(ri++==31)
{
ri=1;if(yue++==12){nian++;yue=1;write_nian(1,1,nian);write_data('-');}
}
}write_erweishu(1,6,yue);write_data('-');write_erweishu(1,9,ri);
if(yue==4||yue==6||yue==9||yue==11)
{
if(ri++==30)
{
ri=1;if(yue++==12){nian++;yue=1;write_nian(1,1,nian);write_data('-');}
}
}write_erweishu(1,6,yue);write_data('-');write_erweishu(1,9,ri);
if(yue==2)
{
if(run==1)
{
run=0;
if(ri++==29)
{
ri=1;
if(yue++==12){nian++;yue=1;write_nian(1,1,nian);write_data('-');}
}write_erweishu(1,6,yue);write_data('-');write_erweishu(1,9,ri);
}
else
{
if(ri++==28)
{
ri=1;
if(yue++==12){nian++;yue=1;write_nian(1,1,nian);write_data('-');}
}write_erweishu(1,6,yue);write_data('-');write_erweishu(1,9,ri);
}
}
}write_erweishu(2,5,shi);calculate_week(nian,yue,ri);write_week(1,13,xingqi); //写星期几
}write_erweishu(2,8,fen);
}write_erweishu(2,11,miao);
}
|