#include <STC15F2K60S2.H>
sbit HC138_A=P2^5;
sbit HC138_B=P2^6;
sbit HC138_C=P2^7;
sbit buzz_1= P0^6;
sbit jdq_2= P0^4;
sbit s7=P3^0;//独立按键
sbit s6=P3^1;
unsigned char code SMG_duanma[18]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,0xbf,0x7f};//有第零个位置
//unsigned char yu=1;
unsigned char Sec,Min,Hour;
void DelaySMG(unsigned int num)
{
unsigned int i;
while(num--)
for(i=0; i<628; i++);
}
void Init_HC138(unsigned char n)
{
switch (n)
{
case 4:
HC138_A=0;
HC138_B=0;
HC138_C=1;
break;
case 5:
HC138_A=1;
HC138_B=0;
HC138_C=1;
break;
case 6:
HC138_A=0;
HC138_B=1;
HC138_C=1;
break;
case 7:
HC138_A=1;
HC138_B=1;
HC138_C=1;
break;
}
}
void led_disp()
{
Init_HC138(4);
P0=0xff;
}
void close_buzz()
{
Init_HC138(5);
buzz_1=0;//0是关闭
jdq_2=0;//0是关闭
}
void smg_show_bit(unsigned char dat,unsigned char pos)
{
Init_HC138(6);//数码管的位置
P0=0x01<<pos;
Init_HC138(7);//数码管的内容
P0=dat;
}
void smg_dynamic()
{
// unsigned char i;
// for(i=0;i<8;i++)
// {
//
// smg_show_bit(SMG_duanma[i],i);
// Delay(1) ;
//
// }
//
smg_show_bit(SMG_duanma[Hour/10],0);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[Hour%10],1);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[16],2);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[Min/10],3);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[Min%10],4);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[16],5);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[Sec/10],6);
DelaySMG(1) ;
smg_show_bit(SMG_duanma[Sec%10],7);
DelaySMG(1) ;
}
void delay(unsigned char t)//注意这有两个延时函数,一个延时数码管,一个整体延时
{
while(t--)
{
smg_dynamic();
}
}
/*****************************************************************/
void Timer1Init(void) //1毫秒@12.000MHz
{
AUXR &= 0xBF; //定时器时钟12T模式
TMOD &= 0x0F; //设置定时器模式
TL1 = 0x18; //设置定时初始值
TH1 = 0xFC; //设置定时初始值
TF1 = 0; //清除TF1标志
TR1 = 1; //定时器1开始计时
ET1 = 1; // 允许定时器 1 中断
EA = 1; // 允许系统中断
}
void ServiceTimer1() interrupt 3
{
static unsigned int T1Count;
T1Count++;
if(T1Count>=1000)
{
T1Count=0;
Sec++;
if(Sec>=60)
{
Sec=0;
Min++;
if(Min>=60)
{
Min=0;
Hour++;
if(Hour>=24)
{
Hour=0;
}
}
}
}
}
void Scankey()
{
if(s7==0)
{
delay(10);
if(s7==0)
{
TR1 = ~TR1;
}
}
if(s6==0)
{
delay(10);
if(s6==0)
{
Sec=0;
Min=0;
Hour=0;
}
}
}
/*****************************************************************/
void main()
{
close_buzz();
led_disp();
Timer1Init();
while(1)
{
smg_dynamic();
Scankey();
}
}