1、用定时器0制作一个时钟,//用stc-isp带的软件 uint tt
显示格式xx-xx-xx //Display函数 uchar hour,min,sec;
uchar code tab_duan{}
uchar code tab_wei{}
2、当到达半点时,led0以0.2s闪烁3次 //系统自带延时函数sbit led0 = P0^0;
//void led0_();
当到达整点时,所有led以0.2s闪烁3次 //void led_all();
附加:
继电器 P0^4
蜂鸣器 P0^6
#include <reg52.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sfr AUXR = 0x8e;
sbit led0 = P0^0;
uchar hour=0,min=0,sec=0;
uchar tt=0;
uchar code tab_duan[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x98,0xbf};
uchar code tab_wei[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
void allinit();
void Delay10ms(); //@11.0592MHz
void Delay1ms(); //@11.0592MHz
void led_0(); //led0间隔0.2s闪烁3下
void led_all(); //所有小灯间隔0.2s闪烁3下
void Timer0Init(); //定时器0
void Clock(); //得到hour,min,sec的值
void Display(); //用数码管显示hour,min,sec
void main()
{
allinit();
Timer0Init();
EA = 1;
ET0 = 1;
while(1)
{
Display();
}
}
void allinit()
{
P2=0xa0;P0=0x00;//关闭蜂鸣器
P2=0x80;P0=0xff;//关闭所有小灯
P2=0xc0;P0=0xff;//选中所有的数码管
P2=0xe0;P0=0xff;//关闭所有的数码管
}
void Timer0Init(void) //5毫秒@11.0592MHz
{
AUXR |= 0x80; //定时器时钟1T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x28; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
}
void Timer0() interrupt 1
{
tt++;
Clock();
}
void Clock()
{
if(200 == tt)
{
tt = 0;
sec++;
if(60 == sec)
{
sec = 0;
min++;
if(30 == min)
{
led_0(); //当为半点时,led0闪烁3下
}
if(60 == min)
{
min = 0;
hour++;
led_all(); //hour加1时,所有的小灯闪烁3下
if(24 == hour)
{
hour = 0;
}
}
}
}
}
void Delay10ms() //@11.0592MHz
{
unsigned char i, j;
i = 108;
j = 145;
do
{
while (--j);
} while (--i);
}
void Display()
{
P2=0xc0;
P0=tab_wei[0];
P2=0xe0;
P0=tab_duan[hour/10];
Delay1ms();
P2=0xc0;
P0=tab_wei[1];
P2=0xe0;
P0=tab_duan[hour%10];
Delay1ms();
P2=0xc0;
P0=tab_wei[2];
P2=0xe0;
P0=tab_duan[10];
Delay1ms();
P2=0xc0;
P0=tab_wei[3];
P2=0xe0;
P0=tab_duan[min/10];
Delay1ms();
P2=0xc0;
P0=tab_wei[4];
P2=0xe0;
P0=tab_duan[min%10];
Delay1ms();
P2=0xc0;
P0=tab_wei[5];
P2=0xe0;
P0=tab_duan[10];
Delay1ms();
P2=0xc0;
P0=tab_wei[6];
P2=0xe0;
P0=tab_duan[sec/10];
Delay1ms();
P2=0xc0;
P0=tab_wei[7];
P2=0xe0;
P0=tab_duan[sec%10];
Delay1ms();
}
void Delay1ms() //@11.0592MHz
{
unsigned char i, j;
_nop_();
_nop_();
_nop_();
i = 11;
j = 190;
do
{
while (--j);
} while (--i);
}
void led_0()
{
uchar i,j;
for(i=0;i<3;i++)
{
P2=0x80;
led0=0;
for(j=0;j<20;j++)
Delay10ms();
P2=0x80;
led0=1;
for(j=0;j<20;j++)
Delay10ms();
}
P2=0x80;
led0=1;
}
void led_all()
{
uchar i,j;
for(i=0;i<3;i++)
{
P2=0x80;
P0=0x00;
for(j=0;j<20;j++)
Delay10ms();
P2=0x80;
P0=0xff;
for(j=0;j<20;j++)
Delay10ms();
}
P2=0x80;
P0=0xff;
}