#include "STC15F2K60S2.H"
//==宏定义
#define uchar unsigned char
#define uint unsigned int
uchar buf[8]={1,2,3,4,5,6,7,8};//内容信息存储
uchar shu[8]={1,2,3,4,5,6,7,8};
uchar wei_zhi[8]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//位置信息对应的数据
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;
//==硬件定义区
sbit out=P4^2;//定义输出管脚
sbit led1=P4^0;
sbit buzzer=P3^5; //蜂鸣器
//==函数声明
void disp();//显示
void naozhongdisp();//闹钟显示
void delay(uint);
void shuju();
void naozhongshuju();//闹钟数据
void Timer0Init(); //20毫秒@11.0592MHz 定时器初始化
void key();
void s2_fun();//K1功能函数
void s3_fun();//K2功能函数
void s4_fun();//K3功能函数
void s5_fun();//K4功能函数
void s6_fun();//K5功能函数
void s7_fun();//K6功能函数
main()
{
//timer0_init();
// Int_init();
Timer0Init();//定时器初始化函数
while(1)
{ if(key_flag==1)
{
disp();
}
if(key_flag==0)
{
naozhongdisp();
}
if(shi==hour&Minute==10&miao==0)
{
buzzer=0;
};
key();
}
}
void disp() //显示函数
{ uchar i;//0~255
shuju();//区分十位个位
for(i=0;i<8;i++)
{
P0=wei_zhi[i]; //第几位显示
P2=code_tab[buf[i]];//BCD转成笔画码 显示的数字
delay(1);
}
P0=0XFF;P2=0XFF;
}
void naozhongdisp() //显示函数
{
uchar c;//0~255
naozhongshuju();//区分十位个位
for(c=0;c<8;c++)
{
P0=wei_zhi[c]; //第几位显示
P2=code_tab[shu[c]];//BCD转成笔画码 显示的数字
delay(1);
}
P0=0XFF;P2=0XFF;
}
void shuju()//数据准备
{ 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()//数据准备
{ 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) // 延时函数
{
uint i,j; //变量定义
for(i=xms;i>0;i--)
for(j=200;j>0;j--);
}
void Timer0Init(void) //20毫秒@11.0592MHz 定时器初始化
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0xB8; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
EA=1;//CPU中断开放
ET0=1;//允许T0中断
}
void zhongduan()interrupt 1 //固定的用法 定时器中断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 因为秒等于0的时候也%=0
{ miao=0;
fen++;
if(fen>=60)
{
shi++;
fen=0;
if(shi>=24)
{
shi=0;
}
}
}
}
}
void key() //按键函数
{
uchar temp_key;
temp_key=P1;//读取按键端口 P1断的状态 未动作时都是1111 1111
temp_key=~temp_key;//取反 ~用于整个字节取反 变成 0000 0000
temp_key=temp_key&0xfc;//屏蔽没用的电路 0xfc=1111 1100 使她&oxfc使后面两个不管去1或0 都不输出1
if(temp_key!=0) // 当temp_key不等于0时
{//非0,有按键
delay(10);//10ms延时// 防抖 按键刚按下时会有机械抖动 既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)//有按键时,数据非零,=等待释放 数据为0走吓一条程序
{
temp_key=P1;//读取按键端口
temp_key=~temp_key;//取反
temp_key=temp_key&0xfc;//屏蔽没用的线路
if(key_flag==1)
{
disp();
}
if(key_flag==0)
{
naozhongdisp();
} //显示函数 使按下按键数码管正常跃
}
//按键松开执行功能
switch(key_val) //swith 配合case使用 既swith符合那个条件就走那个函数
{
case 0x04: //s2按下
s2_fun();//s2按键对应功能
break;// 跳出swith
case 0x08: //s3按下
s3_fun();//s2按键对应功能
break;
case 0x10: //s4按下
s4_fun();//s4按键对应功能
break;
case 0x20: //s5按下
s5_fun();//s5按键对应功能
break;
case 0x40: //s6按下
s6_fun();//s6按键对应功能
break;
case 0x80: //s7按下
s7_fun();//s7按键对应功能
break;
}
}
}
}
//s2功能函数
void s2_fun()
{
buzzer=1;
}
void s3_fun() //小时增加 逢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;
}
}
|