定时器0方式2
#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit led1=P1; uint num; void main() { TMOD=0x02; TH0=6; TL0=6; EA=1; ET0=1; TR0=1; while(1) { if(num==3686) { num=0; led1=~led1; } } } void T0_timer() interrupt 1 { num++; }
方式3
#include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit led1=P1^0; sbit led2=P1^1; uint num1,num2; void main() { TMOD=0x03; TH0=6; TL0=6; EA=1; ET0=1; ET1=1; TR0=1; TR1=1; while(1) { if(num1>=3686)//1s时间已到 { num1=0; led1=~led1; } if(num2>=1843) //半秒时间已到 { num2=0; led2=~led2; } } } void TL0_timer() interrupt 1 { TL0=6; num1++; } void TH0_timer() interrupt 3 { TH0=6; num2++; }