51单片机PWM

# include & ltSTC 51 . h & gt;

int X;PWM的周期单位:us微秒

国际关税;//占空比变量

void main()

{

X = 2000//周期为100uS,即PWM的频率=10KHZ。

关税= 50;//50%占空比

TMOD = 0x 11;

EA = 1;

ET0 = 1;

et 1 = 1;

TH0 =(65535-duty)/256;//定时器0改变占空比。

TL0 =(65535-duty)% 256;

th 1 =(65535-X)/256;//Timer1改变频率

TL 1 =(65535-X)% 256;

TR0 = 1;

tr 1 = 1;

while(1);

}

void timer 0 _ ISR()interrupt 1//timer 0;改变占空比

{

TR0 = 0;

p 1 = 0x 00;

++职责;

if(duty & gt;= X-1)duty = 1;

}

Voidtimer1 _ ISR()中断3//timer 1;改变PWM频率,即周期

{

p 1 = 0x ff;

th 1 =(65535-X)/256;//改变周期

TL 1 =(65535-X)% 256;

TH0 =(65535-duty)/256;//更改占空比

TL0 =(65535-duty)% 256;

TR0 = 1;

//tr 1 = 1;

}