code unsigned char tab[]=
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
//***陰數碼管 0-9
unsigned char Dis_Shiwei;//定義十位
unsigned char Dis_Gewei; //定義個位
/******************************************************************/
/* 延時函數 */
/******************************************************************/
void delay(unsigned int cnt)
{
while(--cnt);
}
/******************************************************************/
/* 主函數 */
/******************************************************************/
main()
{
TMOD |=0x01;//定時器設置 10ms in 12M crystal,工作在模式1,16位定時
TH0=0xd8;
TL0=0xf0;
IE= 0x82; //打開中斷
TR0=1; //打開定時開關
while(1)
{
P0=Dis_Shiwei;//顯示十位
P2=0;
delay(300); //短暫延時
P0=Dis_Gewei; //顯示個位
P2=1;
delay(300);
}
}
/******************************************************************/
/* 定時器中斷函數 */
/******************************************************************/
void tim(void) interrupt 1 using 1
{
static unsigned char second,count;
TH0=0xd8; //重新賦值
TL0=0xf0;
count++;
if (count==100) //100x10ms=1S,大致延時時間
{
count=0;
second++; //秒加1
if(second==100)
second=0;
Dis_Shiwei=tab[second/10];//十位顯示值處理
Dis_Gewei=tab[second%10]; //個位顯示處理
}