效果是PWM ,按下按鈕時LED亮度發(fā)生變化,疑問:這個程序怎么沒用TMOD設置?也沒有TH0和TL0初值設置?
#include<reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit out=P3^3; //33
uchar timerh,timerl;
sbit k1= P3^5;
sbit k2= P3^6;
void main() //主函數(shù)
{
out=0;
ET0=1;
EA=1;
TR0=1;
while(1)
{
if(k1==0) {TR0=0; out=0;} //
else if(k2==0) {TR0=0; out=1;} //
else { TR0=1; timerh=2; timerl=130;}
}
}
void timer0() interrupt 1
{
out=~out;
if(out==1)
{TH0=timerl;}
else
{TH0=timerh; }
TL0=0;
}
|