#include <STC15F2K60S2.H>
#include "absacc.h" //使用XBYTE时需包含此头文件
//声明函数
extern void Write_Ds1302( unsigned char address,unsigned char dat );
extern unsigned char Read_Ds1302 ( unsigned char address );
void delayms(unsigned int k);
void display(void);
void chaifen();
unsigned char read_key(void);
bit key_re;
unsigned char key_value;
unsigned char key_press;
unsigned char key_temp;
//定义变量
unsigned char hour=0,min=0,sec=0;
// 0 1 2 3 4 5 6 7 8 9 -
code unsigned char tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xbf}; //数码管0-9,- 段码
unsigned char dspbuf[8]; // 数码管显示缓冲区
unsigned char dspcom = 1;
void main()
{
XBYTE[0x8000] = 0xff;
Write_Ds1302( 0x8e,0x00);
Write_Ds1302( 0x84,0x13 ); //设置初始时间:13时
Write_Ds1302( 0x82,0x43 ); //设置初始时间:43分
Write_Ds1302( 0x80,0x58 ); //设置初始时间:58秒
Write_Ds1302( 0x8e,0x80);
while(1)
{
key_temp = read_key();
//进入按键处理程序部分
switch(key_temp)
{
case 1:
while(1)
{
delayms(10); //延时100ms
hour=Read_Ds1302 (0x85);
if(hour<0x12)
{
hour=Read_Ds1302 (0x85); //读当前时间:时
XBYTE[0x8000] = 0xf0;
}
else
{
XBYTE[0x8000] = 0x0f;
hour=Read_Ds1302 (0x85)-0x12;
}
min=Read_Ds1302 (0x83); //读当前时间:分
sec=Read_Ds1302 (0x81); //读当前时间:秒
display();
key_temp = read_key();
if(key_temp==3)
break;
}break;
case 2:
while(1)
{
delayms(10); //延时100ms
hour=Read_Ds1302 (0x85); //读当前时间:时
min=Read_Ds1302 (0x83); //读当前时间:分
sec=Read_Ds1302 (0x81); //读当前时间:秒
display();
key_temp = read_key();
if(hour>0x12)
{
XBYTE[0x8000] = 0x0f;
}
else
XBYTE[0x8000] = 0xf0;
if(key_temp==3)
break;
}break;
}
key_temp = 0; //完成按键处理程序后,清除键值
}
}
//延时kms函数(fosc为12Mhz时)
void delayms(unsigned int k)
{
unsigned int i,j;
for(j=0;j<k;j++)
for(i=0;i<121;i++)
{;}
}
//分别拆分时分秒的高低位,并存入数码管显示缓冲区
void chaifen()
{
dspbuf[0]=hour/16;
dspbuf[1]=hour%16;
dspbuf[2]=10; //显示-
dspbuf[3]=min/16;
dspbuf[4]=min%16;
dspbuf[5]=10; //显示-
dspbuf[6]=sec/16;
dspbuf[7]=sec%16;
}
//显示函数,将dspbuf[8]显示缓冲区的内容显示到8位数码管上:时-分-秒
void display(void)
{
chaifen();
XBYTE[0xE000] = 0xff; //清除显示
XBYTE[0xC000] = (1<<dspcom);
XBYTE[0xE000] = tab[dspbuf[dspcom]]; //段码
dspcom++;
if(dspcom == 8)
{
dspcom = 0;
}
}
//读取键值
unsigned char read_key(void)
{
unsigned char key_temp;
key_temp = (P3&0x0f);
if(key_temp != 0x0f) //有按键按下
key_press++;
else
key_press = 0; //抖动
if(key_press == 5)
{
key_press = 0;
key_re = 1;
switch(key_temp)
{
case 0x0e:
key_value = 1; //
break;
case 0x0d:
key_value = 2; //
break;
case 0x0b:
key_value = 3; //
break;
case 0x07:
key_value = 4; //
break;
}
}
//连续三次检测到按键被按下,并且该按键已经释放
if((key_re == 1) && (key_temp == 0x0f))
{
key_re = 0;
return key_value;
}
return 0xff; //无按键按下或被按下的按键未被释放
}