60s倒计时
#include <STC8H.H>
#include "delay.h"/*74HC245*/
#define OUTPUT P0/*3——8译码器*/
sbit A0 = P2^2;
sbit A1 = P2^3;
sbit A2 = P2^4;unsigned char second = 60; //秒计数
unsigned char count = 200; //中断200次为1秒 unsigned char code LED_7SEG_AN[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //共阳极数码管0~9
unsigned char code LED_7SEG_CAT[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //共阴极数码管0~9void Timer0Init(void) //5毫秒@24.000MHz
{AUXR &= 0x7F; //定时器时钟12T模式TMOD &= 0xF0; //设置定时器模式TL0 = 0xF0; //设置定时初始值TH0 = 0xD8; //设置定时初始值TF0 = 0; //清除TF0标志TR0 = 1; //定时器0开始计时
}void main(void)
{
/*准双向口模式*/P0M0 = 0x00;P0M1 = 0x00;P2M0 = 0x00;P2M1 = 0xe3;EA = 1; //总中断允许ET0 = 1; //定时器T0中断允许
/*定时器初始化*/Timer0Init(); while(1){A0 = 1;A1 = 0;A2 = 0;OUTPUT = LED_7SEG_CAT[second/10]; delay_ms(5);A0 = 0;A1 = 0;A2 = 0;OUTPUT = LED_7SEG_CAT[second%10]; delay_ms(5); }
}void Timer0() interrupt 1
{count--;if(count == 0){count = 200;second--;if(second == 0){second = 60;}}
}
60s倒计时