单片机按键数字时钟制作资料.doc
文本预览下载声明
#includeregx52.h
#define key1 P1_0 //秒、分、时间的切换键
#define key2 P1_1 //加1
#define key3 P1_2 //减1
#define speaker P3_1
unsigned char code table1[ ]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //笔段码
unsigned char code table2[ ]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; //位选码
unsigned char count1=0 ;
/****************************************************/
unsigned char hour = 12, min = 0,sec = 0; //时钟赋初值
/******************T0__50mS中断程序*******************/
void T0_1s(void) interrupt 1 // 50mS中断程序
{
static unsigned char count = 0;
TR0 = 0;
TH0 = (65536 - 50000 ) / 256; //设置T0初始值为:15536
TL0 = (65536 - 50000 ) % 256; //记数为50000次//50000*1us=50ms*20=1s
TR0 = 1;
count++;
if(count == 20) //中断服务程序
{ //定时1s时间到(需要中断20次)
count = 0;
sec++;
if(sec==60) //1分钟时间到
{
sec=0;
min++;
if(min==60) //1小时时间到
{
min=0;
hour++;
if(hour==24) //24小时时间到
{
sec =0;
min =0;
hour=0;
}
}
}
}
}
/**************************************************************/
void delay_5ms() //动态扫描显示--软件延时
{
unsigned char i,j;
for(i=0;i20;i++)
for(j=0;j50;j++) //50
;
}
/*****************************************************************/
void disp_led()
{
/****************显示时钟 ******************************/
P0=table1[hour/10]; //显示小时十位数
P2=table2[0];
delay_5ms();
P0=0xff;
P0=table1[hour%10]; //显示小时个位数
P2=table2[1];
delay_5ms();
P0=0xff;
P0=0xbf; //显示分隔符‘-’
P2=table2[2];
delay_5ms();
P0=0xff;
P0=table1[min/10]; //显示分钟十位数
P2=table2[3];
delay_5ms();
P0=0xff;
P0=table1[min%10]; //显示分钟个位数
P2=table2[4];
delay_5ms();
P0=0xff;
P0=0xbf; //显示分隔符‘-’
P2=table2[5];
delay_5ms();
P0=0xff;
P0=table1[sec/10]; //显
显示全部