|
|
5.png (20.46 KB, 下載次數(shù): 69)
下載附件
仿真電路圖
2016-5-13 17:18 上傳
#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; //復(fù)位鍵
sbit key1=P1^1; //秒加1鍵
sbit key2=P1^2; //分加1鍵
sbit key3=P1^3; //時(shí)加1鍵
sbit key4=P1^4; //日加1鍵
sbit key5=P1^5; //月加1鍵
sbit key6=P1^6; //年加1鍵
//******************延時(shí)函數(shù)****************
void delay(uint k)
{
uchar j;
while((k--)!=0)
{
for (j=0;j<250;j++);
}
}
//*****************顯示函數(shù)*****************
void disPlay()
{
P3=0x80; //秒和日的個(gè)位顯示
P0=duanxuan[sec%10]; //顯示秒的個(gè)位
P2=duanxuan[day%10]; //顯示日的個(gè)位
delay(1);
P3=0x40; //秒和日的十位顯示
P0=duanxuan[sec/10]; //顯示秒的十位
P2=duanxuan[day/10]; //顯示日的十位
delay(1);
P3=0x20; //時(shí)鐘顯示“-”,日期顯示月的個(gè)位
P0=duanxuan[10]; //調(diào)數(shù)組元素,顯示“-”
P2=duanxuan[month%10]; //顯示月的個(gè)位
delay(1);
P3=0x10; //分的個(gè)位顯示和月的十位顯示
P0=duanxuan[min%10]; //顯示分的個(gè)位
P2=duanxuan[month/10]; //顯示月的十位
delay(1);
P3=0x08; //分的十位顯示和年的個(gè)位顯示
P0=duanxuan[min/10]; //顯示分的十位
P2=duanxuan[year%10]; //顯示年的個(gè)位
delay(1);
P3=0x04; //時(shí)鐘顯示“-”,日期顯示年的十位
P0=duanxuan[10]; //顯示“-”
P2=duanxuan[year/10%10]; //顯示年的十位
delay(1);
P3=0x02; //小時(shí)的個(gè)位和年的百位顯示
P0=duanxuan[hour%10]; //顯示秒的個(gè)位
P2=duanxuan[year/100%10];//顯示年的百位
delay(1);
P3=0x01; //小時(shí)的十位和年的千位顯示
P0=duanxuan[hour/10]; //顯示分的十位
P2=duanxuan[year/1000]; //顯示年的個(gè)位
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;}
}
}
/**************************按鍵調(diào)整時(shí)間函數(shù)*****************/
void keyscan() //按鍵控制函數(shù)
{
if (key0==0)
{
delay(30); //去抖動(dòng)
if (key0==0)
{
sec=0; min=0; hour=0; year=2014;
month=10; day=1; //復(fù)位鍵被按下,恢復(fù)初始值
}
}
if (key1==0) //判斷“秒”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key1==0)
{
sec++; //是“秒”的按鍵被按下,每按鍵一次加1
if (sec==60) //若秒的變量sec加到60,則sec清零
{
sec=0;
}
}
}
if (key2==0) ////判斷“分”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key2==0)
{
min++; //是“分”的按鍵被按下,每按鍵一次加1
if (min==60) //若秒的變量min加到60,則min清零
{
min=0;
}
}
}
if (key3==0) ////判斷“時(shí)”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key3==0)
{
hour++; //是“時(shí)”的按鍵被按下,每按鍵一次加1
if (hour==24) //若時(shí)的變量hour加到24,則hour清零
{
hour=0;
}
}
}
if (key4==0) ////判斷“日”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key4==0)
{
day++; //是“日”的按鍵被按下,每按鍵一次加1
if (day==32) //若日的變量day加到32,則day=1
{
day=1;
}
}
}
if (key5==0) //判斷“月”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key5==0)
{
month++; //是“月”的按鍵被按下,每按鍵一次加1
if (month==13) //若月的變量month加到13,則month=1
{
month=1;
}
}
}
if (key6==0) //判斷“年”的調(diào)整鍵是否被按下
{
delay(30); //去抖動(dòng)
if (key6==0)
{
year++; //是“年”的按鍵被按下,每按鍵一次加1
if (year==2500) //若年的變量year加到2500,則year=2016
{
year=2016;
}
}
}
}
//*************************主函數(shù)******************
void main()
{
TMOD=0x10; //初始化
ET0=1; //開(kāi)定時(shí)器T0中斷
TR0=1; //啟動(dòng)定時(shí)器T0
TH0=(65536-50000) /256; //賦定時(shí)50ms初值
TL0=(65536-50000) %256;
EA=1; //開(kāi)中斷總開(kāi)關(guān)
while(1)
{
keyscan();
}
}
//***************定時(shí)器T0中斷函數(shù)*****************
void time1() interrupt 1
{
TH0=(65536-50000) /256; //賦定時(shí)50ms初值
TL0=(65536-50000) %256;
disPlay(); //調(diào)用顯示函數(shù)
tt++; //計(jì)中斷次數(shù),中斷20次為1s
if (tt=20)
{
tt=0; //如計(jì)滿1s,tt清零
sec++; //秒加1
if (sec==60) //如計(jì)滿60s,變量sec清0
{
sec=0;
min++; //分加1
if (min==60) //如計(jì)滿60min,變量min清0
{
min=0;
hour++; //時(shí)加1
if (hour==24)//如計(jì)滿24小時(shí),變量hour清0
{
hour=0;
day++; //日加1
runnian();//調(diào)用閏年函數(shù),確定每月的天數(shù)
if (day==b) //根據(jù)每天的天數(shù)值b,判斷是否要月加1
{
day=1;
month++; //月加1
if (month==13) //如計(jì)滿12個(gè)月,變量month清零
{
month=1;
year++; //年加1
}
}
}
}
}
}
}
|
評(píng)分
-
查看全部評(píng)分
|