#include"stc8H.h"
#include “intrins.h”
#define uint unsigned int
#define uchar unsigned char
uchar count;
char sec,min;
bit flag;
uchar code table[]={
0x03,/0/
0x9F,/1/
0x25,/2/
0x0D,/3/
0x99,/4/
0x49,/5/
0x41,/6/
0x1F,/7/
0x01,/8/
0x09,/9/};
void GPIO_init();
void T0_init() ;
void time_display();
void GPIO_init()
{
P0M0 = 0x00; P0M1 = 0x00;
P1M0 = 0x00; P1M1 = 0x00;
P2M0 = 0x00; P2M1 = 0x00;
P3M0 = 0x00; P3M1 = 0x00;
P5M0 = 0x00; P5M1 = 0x00;
}
void T0_init(void) //5毫秒@11.0592MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x00; //设置定时初始值
TH0 = 0xEE; //设置定时初始值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //使能定时器0中断
EA = 1; //使能总中断
}
void time_display()
{
static uchar i=0;
P1&=0xf0;
switch(i)
{
case 0: P2=table[min/10]; break;
case 1: P2=table[min%10]&0xfe; break;
case 2: P2=table[sec/10]; break;
case 3: P2=table[sec%10]; break;
}
P1&=~(0x01<<i);
i=++i%4;
}
void main()
{
GPIO_init();
T0_init();
while(1)
{
if(flag)
{
flag=0;
time_display();
if(++count >= 200)
{
count = 0;
if(–sec < 0)
{
sec = 59;
if(–min < 0)
min=99;
}
}
}
}
}
void TM0_Isr() interrupt 1 //5毫秒定时器0
{
TL0 = 0x00; //设置定时初始值
TH0 = 0xEE; //设置定时初始值
flag=1;
}