源程序设计 #include<reg52.h> #define uchar unsigned char #define uint unsigned int sbit D1=P1^0; uchar aa; void main() { TMOD=0x01; TH0=(65536-50000)/256; TL0=(65536-50000)%256; //定时50ms中断一次 EA=1; //开总中断 ET0=1; //允许定时器T0中断 TR0=1; //起动定时器开始工作 D1=1; //让LED灯初始时处在熄灭状态 while(1); //等待中断产生 } void my_timer0() interrupt 1 //中断服务程序 { TH0=(65536-50000)/256; TL0=(65536-50000)%256; //重新赋初值 aa++; //中断一次变量aa加1 if(aa==20) //当aa=20时中断了20次,定时时间为20*50ms=1s,更改一次P1.0口的输出状态,这样得到的方波周期为2s { aa=0; //将变量aa清零,以便于下次重新定时 D1=~D1; //改变引脚P1.0的输出状态 } } |