#include <reg51.h> #define uchar unsigned char #define uint unsigned int sbit led1=P1^0; sbit led2=P1^3; sbit k1=P3^2; sbit k2=P3^3; uchar num0,num1; void init() { TMOD=0x11; //设置定时器0位工作模式1(M1,M0位0,1) TH0=(65536-45872)/256; //装初值11.0592M晶振定时50ms数为45872 TL0=(65536-45872)%256; TH0=0X2C; TL0=0X00; TH1=0X3C; TL1=0X00; EA=1; //开总中断 ET0=1; //开定时器0中断 ET1=1; TR1=1; TR0=1; //启动定时器0 } void main() { init(); while(1) { if(num0==20) { led1=~led1; //让发光管状态取反 num0=0; } else {if(num1==20) { led2=~led2; //让发光管状态取反 num1=0; } } } } void T0_time()interrupt 1 { TR0=0; TH0=(65536-46873)/256; //重新装载初值 TH0=0X2C; TL0=0X00; TR0=1; //TL0=(65536-46873)%256; num0++; } void T1_time()interrupt 3 { TR1=0; TH1=(65536-45872)/256; //重新装载初值 TL1=(65536-45872)%256; TH1=0X3C; TL1=0X00; TR1=1; num1++; }