PWM问题
我用定时器T0,T1调节PWM从而改变灯的亮度,可是好像灯没有感觉,一直亮着的,我直接上代码,也不长,谁能帮忙看看
#include<reg52.h>
sbit PWM=P1^0;
unsigned char i=0;
unsigned char ratio;
unsigned char j;
unsigned char code tab[10]={0,1,2,3,4,5,6,7,8,9};
void main()
{
TMOD=0x11; //高4位 timer1,低4位 timer0
TH0=(65536-1000)/256; //1ms
TL0=(65536-1000)%256;
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
EA=1;
TR0=1;
TR1=1;
ET0=1;
ET1=1;
PWM=0;
while(1);
}
void timer0(void) interrupt 1 using 1
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
i++;
PWM=0; //
if(i==10)
{
i=0;
j++;
if(j==10)
j=0;
}
}
void timer1(void) interrupt 2
{
TH1=(65536-1000)/256;
TL1=(65536-1000)%256;
ratio++;
if(ratio<10)
{
if(ratio<tab[j])
PWM=1;
else
PWM=0;
}
else
ratio=0;
}