|
#include "STC15F2K60S2.H"
//==宏定義
#define uchar unsigned char
#define uint unsigned int
uchar buf[8]={1,2,3,4,5,6,7,8};//內(nèi)容信息存儲
uchar shu[8]={1,2,3,4,5,6,7,8};
uchar wei_zhi[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位置信息對應(yīng)的數(shù)據(jù)
uchar code_tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//0~10
uchar shi=15;
uchar fen=8;
uchar miao=59;
uchar hour=12;
uchar Minute=30;
uchar second=30;
uchar key_val;
uchar key_flag=1;
//==硬件定義區(qū)
sbit out=P4^2;//定義輸出管腳
sbit led1=P4^0;
sbit buzzer=P3^5; //蜂鳴器
//==函數(shù)聲明
void disp();//顯示
void naozhongdisp();//鬧鐘顯示
void delay(uint);
void shuju();
void naozhongshuju();//鬧鐘數(shù)據(jù)
void Timer0Init(); //20毫秒@11.0592MHz 定時(shí)器初始化
void key();
void s2_fun();//K1功能函數(shù)
void s3_fun();//K2功能函數(shù)
void s4_fun();//K3功能函數(shù)
void s5_fun();//K4功能函數(shù)
void s6_fun();//K5功能函數(shù)
void s7_fun();//K6功能函數(shù)
main()
{
//timer0_init();
// Int_init();
Timer0Init();//定時(shí)器初始化函數(shù)
while(1)
{ if(key_flag==1)
{
disp();
}
if(key_flag==0)
{
naozhongdisp();
}
if(shi==hour&Minute==10&miao==0)
{
buzzer=0;
};
key();
}
}
void disp() //顯示函數(shù)
{ uchar i;//0~255
shuju();//區(qū)分十位個(gè)位
for(i=0;i<8;i++)
{
P0=wei_zhi[i]; //第幾位顯示
P2=code_tab[buf[i]];//BCD轉(zhuǎn)成筆畫碼 顯示的數(shù)字
delay(1);
}
P0=0XFF;P2=0XFF;
}
void naozhongdisp() //顯示函數(shù)
{
uchar c;//0~255
naozhongshuju();//區(qū)分十位個(gè)位
for(c=0;c<8;c++)
{
P0=wei_zhi[c]; //第幾位顯示
P2=code_tab[shu[c]];//BCD轉(zhuǎn)成筆畫碼 顯示的數(shù)字
delay(1);
}
P0=0XFF;P2=0XFF;
}
void shuju()//數(shù)據(jù)準(zhǔn)備
{ buf[0]=shi/10;
buf[1]=shi%10;
buf[3]=fen/10;
buf[4]=fen%10;
buf[6]=miao/10;
buf[7]=miao%10;
buf[2]=10;
buf[5]=10;
}
void naozhongshuju()//數(shù)據(jù)準(zhǔn)備
{ shu[0]=hour/10;
shu[1]=hour%10;
shu[3]=Minute/10;
shu[4]=Minute%10;
shu[6]=second/10;
shu[7]=second%10;
shu[2]=10;
shu[5]=10;
}
void delay(uint xms) // 延時(shí)函數(shù)
{
uint i,j; //變量定義
for(i=xms;i>0;i--)
for(j=200;j>0;j--);
}
void Timer0Init(void) //20毫秒@11.0592MHz 定時(shí)器初始化
{
AUXR &= 0x7F; //定時(shí)器時(shí)鐘12T模式
TMOD &= 0xF0; //設(shè)置定時(shí)器模式
TL0 = 0x00; //設(shè)置定時(shí)初值
TH0 = 0xB8; //設(shè)置定時(shí)初值
TF0 = 0; //清除TF0標(biāo)志
TR0 = 1; //定時(shí)器0開始計(jì)時(shí)
EA=1;//CPU中斷開放
ET0=1;//允許T0中斷
}
void zhongduan()interrupt 1 //固定的用法 定時(shí)器中斷0
{
uchar count;
uint a;
a++;
if(key_flag==0)
{
count++;
if(count==150)
{
key_flag=1;
}
}
if(a%50==0)
{
miao++;
a=0;
if(miao>=60) //這里不能用miao%60==0 因?yàn)槊氲扔?的時(shí)候也%=0
{ miao=0;
fen++;
if(fen>=60)
{
shi++;
fen=0;
if(shi>=24)
{
shi=0;
}
}
}
}
}
void key() //按鍵函數(shù)
{
uchar temp_key;
temp_key=P1;//讀取按鍵端口 P1斷的狀態(tài) 未動作時(shí)都是1111 1111
temp_key=~temp_key;//取反 ~用于整個(gè)字節(jié)取反 變成 0000 0000
temp_key=temp_key&0xfc;//屏蔽沒用的電路 0xfc=1111 1100 使她&oxfc使后面兩個(gè)不管去1或0 都不輸出1
if(temp_key!=0) // 當(dāng)temp_key不等于0時(shí)
{//非0,有按鍵
delay(10);//10ms延時(shí)// 防抖 按鍵剛按下時(shí)會有機(jī)械抖動 既0101010101,大約10ms
//再讀端口
temp_key=P1;
temp_key=~temp_key;
temp_key=temp_key&0xfc;
if(temp_key!=0)
{key_val=temp_key;
while(temp_key)//有按鍵時(shí),數(shù)據(jù)非零,=等待釋放 數(shù)據(jù)為0走嚇一條程序
{
temp_key=P1;//讀取按鍵端口
temp_key=~temp_key;//取反
temp_key=temp_key&0xfc;//屏蔽沒用的線路
if(key_flag==1)
{
disp();
}
if(key_flag==0)
{
naozhongdisp();
} //顯示函數(shù) 使按下按鍵數(shù)碼管正常躍
}
//按鍵松開執(zhí)行功能
switch(key_val) //swith 配合case使用 既swith符合那個(gè)條件就走那個(gè)函數(shù)
{
case 0x04: //s2按下
s2_fun();//s2按鍵對應(yīng)功能
break;// 跳出swith
case 0x08: //s3按下
s3_fun();//s2按鍵對應(yīng)功能
break;
case 0x10: //s4按下
s4_fun();//s4按鍵對應(yīng)功能
break;
case 0x20: //s5按下
s5_fun();//s5按鍵對應(yīng)功能
break;
case 0x40: //s6按下
s6_fun();//s6按鍵對應(yīng)功能
break;
case 0x80: //s7按下
s7_fun();//s7按鍵對應(yīng)功能
break;
}
}
}
}
//s2功能函數(shù)
void s2_fun()
{
buzzer=1;
}
void s3_fun() //小時(shí)增加 逢24變0
{
shi++;
if(shi>=24)
{
shi=0;
}
}
void s4_fun()//分++
{
fen++;
if(fen>=60)
{
fen=0;
}
}
void s5_fun()
{
key_flag=0;
}
void s6_fun()
{
key_flag=0;
hour++;
if(hour>=24)
{
hour=0;
}
}
void s7_fun()
{
key_flag=0;
Minute++;
if(Minute>=60)
{
Minute=0;
}
}
|
|