题目:利用单片机内部中断制作简易电子表
要求:用两个按键,P1.0和P1.1控制。没有按下P1.0时,按P1.1,不做任何动作。
当按下P1.0,再按P1。1,则调节年、月、日、星期、时、分、秒。
解决方案:(C 语言编程)
#include
#define uchar unsigned char
#define uint unsigned int
uchar code table[]="2009-02-01 Mon.";
uchar code table1[]=" 00:00:00";
uchar code xingqi[]="Mon. Tue. Wed. Thu. Fri. Sat. Sun.";
uchar num,sfcount;
char hour,min,sec,week,day,month,year,rmonth;
sbit lcdrs=P1^7; //端口定义
sbit lcdrw=P1^6;
sbit lcde=P2^4;
sbit le=P3^ 6;
sbit leden=P2^5;
sbit dkle=P3^7;
sbit sf=P1^0;
sbit sup=P1^1;
void delay(uint); //函数声明
void write_com(uchar com) //写命令子函数
{
lcde=0;
lcdrw=0;
lcdrs=0;
delay(1);
lcde=1;
delay(1);
P0=com;
delay(1);
lcde=0;
}
void write_data(uchar dat) //写数据子函数
{
lcde=0;
lcdrw=0;
lcdrs=1;
delay(1);
lcde=1;
delay(1);
P0=dat;
delay(1);
lcde=0;
}
void init()
{
month=2;
day=1;
year=9;
TMOD=0x01; //定时器初始化
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
lcde=0; //LCD1602初始化
le=0;
leden=0;
write_com(0x38);
write_com(0x0c);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++) //在第一行显示“ 2000-00-00 Mon.”
{
write_data(table[num]);
}
write_com(0x80+0x40);
for(num=0;num<10;num++) //在第二行末尾显示“ 00:00:00”
{
write_data(table1[num]);
}
}
void write_time(uchar add,char dat) //写入时间子函数
{
uchar shi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x80+0x40+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_riqi(uchar add,char dat) //写入日期子函数
{
uchar shi,ge;
shi=dat/10;
ge=dat%10;
write_com(0x80+add);
write_data(0x30+shi);
write_data(0x30+ge);
}
void write_xingqi(char dat) //写入星期子函数
{
write_com(0x80+0x0b);
switch(dat)
{
case 0: for(num=0;num<4;num++)
{
write_data(xingqi[num]);
};break;
case 1: for(num=5;num<9;num++)
{
write_data(xingqi[num]);
};break;
case 2: for(num=10;num<14;num++)
{
write_data(xingqi[num]);
};break;
case 3: for(num=15;num<19;num++)
{
write_data(xingqi[num]);
};break;
case 4: for(num=20;num<24;num++)
{
write_data(xingqi[num]);
};break;
case 5: for(num=25;num<29;num++)
{
write_data(xingqi[num]);
};break;
case 6: for(num=30;num<34;num++)
{
write_data(xingqi[num]);
};break;
}
}
char keyscan() //按键扫描子函数
{
if(sf==0) //功能键
{
delay(2);
if(sf==0)
{
while(!sf);
sfcount++;
if(sfcount==1) //秒针闪烁
{
TR0=0;
write_com(0x80+0x40+9);
write_com(0x0f);
}
if(sfcount==2) //分针闪烁
{
write_com(0x80+0x40+6);
}
if(sfcount==3) //时针闪烁
{
write_com(0x80+0x40+3);
}
if(sfcount==4) //星期闪烁
{
write_com(0x80+0x0d);
}
if(sfcount==5) //天闪烁
{
write_com(0x80+9);
}
if(sfcount==6) //月闪烁
{
write_com(0x80+6);
}
if(sfcount==7) //年闪烁
{
write_com(0x80+3);
}
if(sfcount==8) //取消闪烁
{
sfcount=0;
write_com(0x0c);
TR0=1;
}
}
}
if(sup==0) //调节时间(上升键)
{
delay(5);
if(sup==0)
{
while(!sup);
if(sfcount==1) //调节秒
{
sec++;
if(sec==60)
sec=0;
write_time(8,sec);
write_com(0x80+0x40+9);
}
if(sfcount==2) //调节分钟
{
min++;
if(min==60)
min=0;
write_time(5,min);
write_com(0x80+0x40+6);
}
if(sfcount==3) //调节小时
{
hour++;
if(hour==60)
hour=0;
write_time(2,hour);
write_com(0x80+0x40+3);
}
if(sfcount==4) //调节星期
{
week++;
if(week==7)
week=0;
write_xingqi(week);
write_com(0x80+0x0d);
}
if(sfcount==5) //调节日
{
day++;
if(day==32)
day=1;
write_riqi(8,day);
write_com(0x80+9);
}
if(sfcount==6) //调节月
{
month++;
if(month==13)
month=1;
write_riqi(5,month);
write_com(0x80+6);
}
if(sfcount==7) //调节年
{
year++;
if(year==100)
year=9;
write_riqi(2,year);
write_com(0x80+3);
}
}
}
return month;
}
void main() //main函数
{
init();
while(1)
{
rmonth=keyscan();
}
}
void time0() interrupt 1 //中断子函数
{
uchar tt;
TH0=0x3c;
TL0=0xb0;
tt++;
if(tt==20)
{
tt=0;
sec++;
if(sec==60)
{
sec=0;
min++;
if(min==60)
{
min=0;
hour++;
if(hour==24)
{
hour=0;
week++;
if(week==7)
week=0;
write_xingqi(week);
write_com(0x80+0x0b);
if((rmonth==1)||(rmonth==3)||(rmonth==5)||(rmonth==7)||(rmonth==8)||(rmonth==10)||(rmonth==12)) //有31天的月份
{
day++;
if(day==32)
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
if(year==100)
year=9;
write_riqi(2,day);
write_com(0x80+3);
}
write_riqi(5,month);
write_com(0x80+6);
}
}
else if((rmonth==4)||(rmonth==6)||(rmonth==9)||(rmonth==11))//有30天的月份
{
day++;
if(day==31)
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
if(year==100)
year=9;
write_riqi(2,day);
write_com(0x80+3);
}
write_riqi(5,month);
write_com(0x80+6);
}
}
else if(rmonth==2){ //二月定为28天
day++;
if(day==29)
{
day=1;
month++;
if(month==13)
{
month=1;
year++;
if(year==100)
year=9;
write_riqi(2,day);
write_com(0x80+3);
}
write_riqi(5,month);
write_com(0x80+6);
}
}
write_riqi(8,day);
write_com(0x80+9);
}
write_time(2,hour);
write_com(0x80+0x40+3);
}
write_time(5,min);
write_com(0x80+0x40+6);
}
write_time(8,sec);
write_com(0x80+0x40+9);
}
}
void delay(uint a) { //延时子函数
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--) ;
}