#include "reg51.h"
#define uchar unsigned char
#define uint unsigned int
#define setbit(var, bitt) ( var |= (0x01<<bitt) )
#define clrbit(var, bitt) ( var &= (~(0x01<<bitt)) )
#define checkbit(var,bitt) (var & (0x01 << bitt))
uchar table[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; /*七段数码编码数据 1 2 3 4 */
const scan[4]={0X0E,0X0D,0X0B,0X07};
uchar i=0;
unsigned char T0_count; /*定义定时器中断次数变量*/
unsigned char sec; /*定义1s计数变量*/
unsigned char min;
typedef struct {
char minute;
char second;
} time;
time now;
void delay(void)
{
unsigned int i;
for (i = 1;i < 10000;i++);
}
void T0_int(void) interrupt 1 /*定时器T0中断,12M的晶振,定时50ms*/
{
TH0 = 0x3c; /*重装载计数初值*/
TL0 = 0xb0;
T0_count++; /*中断次数累加*/
if (T0_count==20) /*共进了20次定时中断,实现阶1S定时*/
{
T0_count = 0; /*中断次数累加*/
now.second++; /*50ms计数变量累加*/
if (now.second==60)
{
now.second=0;
now.minute++;
if(now.minute==60)
{
now.minute=0;
}
}
}
switch(i)
{
case 0 : P1=table[now.minute%10];
delay();
break;
case 1 : P1=table[now.minute/10];
delay();
break;
case 2 : P1=table[now.second%10];
delay();
break;
case 3 : P1=table[now.second/10];
delay();
break;
}
P0=scan[i];
i++;
if(i==4) i=0;
}
void main(void) /*主程序开始*/
{
static unsigned int i;
static unsigned int j;
uchar sel;
TMOD = 0x11; /*8位寄存器 00010001 其中T0,T1为定时方式,方式1*/
TH0 = 0x3c; /*自动重装载值*/
TL0 = 0xb0; /*计数初值*/
TR0 = 1; /*启动T0*/
IE = 0x82; /*1000 0010: 开总中断、T0*/
IP = 0x00; /*默认优先级*/
P1 = 0xff; /*上电后P1口全部为高电平*/
T0_count = 0;
P0 =0xff;
loop:
sel=0x11;
P0=sel;
sel=sel<<1;
goto loop;
}