單片機(jī)定時(shí)器0工作模式1
//本程序出自:51hei單片機(jī)網(wǎng)
#include "reg_c51.h"
unsigned char hex[16]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,
0x88,0x83,0xC6,0xA1,0x86,0x8E}; //數(shù)碼管十六進(jìn)制譯碼表
void main(void)
{
TMOD = 0xF0; //定時(shí)/計(jì)數(shù)器0運(yùn)行在模式1,16位模式
TMOD |= 0x0D; //GATE0=1; C/T0#=1; M1=0; M0=1;
TH0 = 0xff; //設(shè)置初值,初值=(65536-7)/256=FFH
TL0 = 0xf9; //設(shè)置初值,初值=(65536-7)%256=F9H
//初值為65529,只需要再計(jì)7個(gè)數(shù),定時(shí)/計(jì)數(shù)器就可以溢出
ET0=1; //允許定時(shí)/計(jì)數(shù)器0中斷
EA=1; //允許總中斷
TR0=1; //啟動(dòng)定時(shí)/計(jì)數(shù)器
while(1); //無(wú)限循環(huán)
{
P1=hex[TL0/16]; //顯示TL0的高位
P0=hex[TL0%16]; //顯示TL0的低位
}}
void it_timer0(void) interrupt 1 //定時(shí)/計(jì)數(shù)器0中斷服務(wù)程序
{
P2_0 = ~P2_0; //每次中斷將P2_0引腳取反,就是使LED燈閃爍
TH0 = 0xff; //重置初值
TL0 = 0xf9; //重置初值
}
評(píng)論