題目:利用單片機內(nèi)部中斷制作簡易電子表要求:用兩個按鍵,P1.0和P1.1控制。沒有按下P1.0時,按P1.1,不做任何動作。
本文引用地址:http://m.butianyuan.cn/article/201611/320193.htm 當按下P1.0,再按P1。1,則調(diào)節(jié)年、月、日、星期、時、分、秒。
解決方案:(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);//函數(shù)聲明
void write_com(uchar com) //寫命令子函數(shù)
{
lcde=0;
lcdrw=0;
lcdrs=0;
delay(1);
lcde=1;
delay(1);
P0=com;
delay(1);
lcde=0;
}
void write_data(uchar dat)//寫數(shù)據(jù)子函數(shù)
{
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) //寫入時間子函數(shù)
{
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) //寫入日期子函數(shù)
{
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) //寫入星期子函數(shù)
{
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() //按鍵掃描子函數(shù)
{
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) //調(diào)節(jié)時間(上升鍵)
{
delay(5);
if(sup==0)
{
while(!sup);
if(sfcount==1)//調(diào)節(jié)秒
{
sec++;
if(sec==60)
sec=0;
write_time(8,sec);
write_com(0x80+0x40+9);
}
if(sfcount==2) //調(diào)節(jié)分鐘
{
min++;
if(min==60)
min=0;
write_time(5,min);
write_com(0x80+0x40+6);
}
if(sfcount==3) //調(diào)節(jié)小時
{
hour++;
if(hour==60)
hour=0;
write_time(2,hour);
write_com(0x80+0x40+3);
}
if(sfcount==4) //調(diào)節(jié)星期
{
week++;
if(week==7)
week=0;
write_xingqi(week);
write_com(0x80+0x0d);
}
if(sfcount==5) //調(diào)節(jié)日
{
day++;
if(day==32)
day=1;
write_riqi(8,day);
write_com(0x80+9);
}
if(sfcount==6) //調(diào)節(jié)月
{
month++;
if(month==13)
month=1;
write_riqi(5,month);
write_com(0x80+6);
}
if(sfcount==7) //調(diào)節(jié)年
{
year++;
if(year==100)
year=9;
write_riqi(2,year);
write_com(0x80+3);
}
}
}
return month;
}
void main() //main函數(shù)
{
init();
while(1)
{
rmonth=keyscan();
}
}
void time0() interrupt 1//中斷子函數(shù)
{
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) { //延時子函數(shù)
uint i,j;
for(j=a;j>0;j--)
for(i=250;i>0;i--) ;
}
評論