|
仿真電路圖
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
uchar code duanxuan[12]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0xff};
uchar tt=0;
uchar month=10,day=1,sec=0,min=0,hour=0,b;
uint year=2016;
sbit key0=P1^0; //復位鍵
sbit key1=P1^1; //秒加1鍵
sbit key2=P1^2; //分加1鍵
sbit key3=P1^3; //時加1鍵
sbit key4=P1^4; //日加1鍵
sbit key5=P1^5; //月加1鍵
sbit key6=P1^6; //年加1鍵
//******************延時函數****************
void delay(uint k)
{
uchar j;
while((k--)!=0)
{
for (j=0;j<250;j++);
}
}
//*****************顯示函數*****************
void disPlay()
{
P3=0x80; //秒和日的個位顯示
P0=duanxuan[sec%10]; //顯示秒的個位
P2=duanxuan[day%10]; //顯示日的個位
delay(1);
P3=0x40; //秒和日的十位顯示
P0=duanxuan[sec/10]; //顯示秒的十位
P2=duanxuan[day/10]; //顯示日的十位
delay(1);
P3=0x20; //時鐘顯示“-”,日期顯示月的個位
P0=duanxuan[10]; //調數組元素,顯示“-”
P2=duanxuan[month%10]; //顯示月的個位
delay(1);
P3=0x10; //分的個位顯示和月的十位顯示
P0=duanxuan[min%10]; //顯示分的個位
P2=duanxuan[month/10]; //顯示月的十位
delay(1);
P3=0x08; //分的十位顯示和年的個位顯示
P0=duanxuan[min/10]; //顯示分的十位
P2=duanxuan[year%10]; //顯示年的個位
delay(1);
P3=0x04; //時鐘顯示“-”,日期顯示年的十位
P0=duanxuan[10]; //顯示“-”
P2=duanxuan[year/10%10]; //顯示年的十位
delay(1);
P3=0x02; //小時的個位和年的百位顯示
P0=duanxuan[hour%10]; //顯示秒的個位
P2=duanxuan[year/100%10];//顯示年的百位
delay(1);
P3=0x01; //小時的十位和年的千位顯示
P0=duanxuan[hour/10]; //顯示分的十位
P2=duanxuan[year/1000]; //顯示年的個位
delay(1);
}
//**************************閏年判斷******************
void runnian()
{
if((year%4==0)&&(year%100!=0)||(year%400)==0) //判斷是否為閏年
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) //每月31天的月份
{b=32;}
if((month==4)||(month==6)||(month==9)||(month==11)) //每月30天的月份
{b=31;}
if((month==2)) //如是閏年,2月為29天
{b=30;}
}
else
{
if((month==1)||(month==3)||(month==5)||(month==7)||(month==8)||(month==10)||(month==12)) //每月31天的月份
{b=32;}
if((month==4)||(month==6)||(month==9)||(month==11)) //每月30天的月份
{b=31;}
if((month==2)) //如不是閏年,2月為28天
{b=29;}
}
}
/**************************按鍵調整時間函數*****************/
void keyscan() //按鍵控制函數
{
if (key0==0)
{
delay(30); //去抖動
if (key0==0)
{
sec=0; min=0; hour=0; year=2014;
month=10; day=1; //復位鍵被按下,恢復初始值
}
}
if (key1==0) //判斷“秒”的調整鍵是否被按下
{
delay(30); //去抖動
if (key1==0)
{
sec++; //是“秒”的按鍵被按下,每按鍵一次加1
if (sec==60) //若秒的變量sec加到60,則sec清零
{
sec=0;
}
}
}
if (key2==0) ////判斷“分”的調整鍵是否被按下
{
delay(30); //去抖動
if (key2==0)
{
min++; //是“分”的按鍵被按下,每按鍵一次加1
if (min==60) //若秒的變量min加到60,則min清零
{
min=0;
}
}
}
if (key3==0) ////判斷“時”的調整鍵是否被按下
{
delay(30); //去抖動
if (key3==0)
{
hour++; //是“時”的按鍵被按下,每按鍵一次加1
if (hour==24) //若時的變量hour加到24,則hour清零
{
hour=0;
}
}
}
if (key4==0) ////判斷“日”的調整鍵是否被按下
{
delay(30); //去抖動
if (key4==0)
{
day++; //是“日”的按鍵被按下,每按鍵一次加1
if (day==32) //若日的變量day加到32,則day=1
{
day=1;
}
}
}
if (key5==0) //判斷“月”的調整鍵是否被按下
{
delay(30); //去抖動
if (key5==0)
{
month++; //是“月”的按鍵被按下,每按鍵一次加1
if (month==13) //若月的變量month加到13,則month=1
{
month=1;
}
}
}
if (key6==0) //判斷“年”的調整鍵是否被按下
{
delay(30); //去抖動
if (key6==0)
{
year++; //是“年”的按鍵被按下,每按鍵一次加1
if (year==2500) //若年的變量year加到2500,則year=2016
{
year=2016;
}
}
}
}
//*************************主函數******************
void main()
{
TMOD=0x10; //初始化
ET0=1; //開定時器T0中斷
TR0=1; //啟動定時器T0
TH0=(65536-50000) /256; //賦定時50ms初值
TL0=(65536-50000) %256;
EA=1; //開中斷總開關
while(1)
{
keyscan();
}
}
//***************定時器T0中斷函數*****************
void time1() interrupt 1
{
TH0=(65536-50000) /256; //賦定時50ms初值
TL0=(65536-50000) %256;
disPlay(); //調用顯示函數
tt++; //計中斷次數,中斷20次為1s
if (tt=20)
{
tt=0; //如計滿1s,tt清零
sec++; //秒加1
if (sec==60) //如計滿60s,變量sec清0
{
sec=0;
min++; //分加1
if (min==60) //如計滿60min,變量min清0
{
min=0;
hour++; //時加1
if (hour==24)//如計滿24小時,變量hour清0
{
hour=0;
day++; //日加1
runnian();//調用閏年函數,確定每月的天數
if (day==b) //根據每天的天數值b,判斷是否要月加1
{
day=1;
month++; //月加1
if (month==13) //如計滿12個月,變量month清零
{
month=1;
year++; //年加1
}
}
}
}
}
}
}
|
評分
-
查看全部評分
|