![]() |
#2
董柯文2016-07-28 20:31
|
在占空比为10%的情况下,输出了200个周期为1ms的10%的占空比的波形,在占空比为20%的情况下,输出200个周期为1ms的20%的占空比的波形……以此类推直到输出200个周期为1ms 的100%的波形,也就是从10%变化到100%总共用时2s。再用2s从100%变化到10%;
请各位大神看看哪里不对!

#include <reg52.h>
sbit PWM = P0^0;
unsigned char time=0;//进入中断的次数
unsigned char th=1;//
unsigned char flag=0;//增减标志位
void main()
{
TMOD = 0x20; //定时器1工作模式2:8位自动重装定时器
TH1 = 255-50; //设定初值为50纳秒
TL1 = 255-50;
EA = 1; //开中断
ET1 = 1; //使能T0中断
TR1 = 1;
while(1)
{
}
}
//可控制占空比的方波
void T1_ISR() interrupt 3
{
if(flag==0)//递增
{
time++;
if((th>0)&&(th<=200)) //10%
{
if(time<=2) PWM=1;
else if ((time>2)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>200)&&(th<=400)) //20%
{
if(time<=4) PWM=1;
else if((time>4)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>400)&&(th<=600)) //30%
{
if(time<=6) PWM=1;
else if((time>6)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>600)&&(th<=800)) //40%
{
if(time<=8) PWM=1;
else if((time>8)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>800)&&(th<=1000)) //50%
{
if(time<=10) PWM=1;
else if((time>10)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1000)&&(th<=1200)) //60%
{
if(time<=12) PWM=1;
else if((time>12)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1200)&&(th<=1400)) //70%
{
if(time<=14) PWM=1;
else if((time>14)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1400)&&(th<=1600)) //80%
{
if(time<=16) PWM=1;
else if((time>16)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1600)&&(th<=1800)) //90%
{
if(time<=18) PWM=1;
else if((time>18)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1800)&&(th<=2000)) //100%
{
if(time<=20) PWM=1;
else if(time=21)
{
time=0;
th++;
}
}
if((th>2000))
{
flag=1;
th=1999;
time=0;
}
}
/************************************************************************************************/
else if (flag==1)
{
time++;
if((th>=1800)&&(th<2000))
{
if(time<20) PWM=1;
else if(time==21)
{
time=0;
th--;
}
}
if ((th>=1600)&&(th<1800))
{
if(time<18) PWM=1;
else if ((time>=18)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1400)&&(th<1600))
{
if(time<16) PWM=1;
else if ((time>=16)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1200)&&(th<1400))
{
if(time<14) PWM=1;
else if ((time>=14)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1000)&&(th<1200))
{
if(time<12) PWM=1;
else if ((time>=12)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=800)&&(th<1000))
{
if(time<10) PWM=1;
else if ((time>=10)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=600)&&(th<800))
{
if(time<8) PWM=1;
else if ((time>=8)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=400)&&(th<600))
{
if(time<6) PWM=1;
else if ((time>=6)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=200)&&(th<400))
{
if(time<4) PWM=1;
else if ((time>=4)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=0)&&(th<200))
{
if(time<2) PWM=1;
else if ((time>=2)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if(th<0)
{
flag=0;
th=1;
}
}
}
sbit PWM = P0^0;
unsigned char time=0;//进入中断的次数
unsigned char th=1;//
unsigned char flag=0;//增减标志位
void main()
{
TMOD = 0x20; //定时器1工作模式2:8位自动重装定时器
TH1 = 255-50; //设定初值为50纳秒
TL1 = 255-50;
EA = 1; //开中断
ET1 = 1; //使能T0中断
TR1 = 1;
while(1)
{
}
}
//可控制占空比的方波
void T1_ISR() interrupt 3
{
if(flag==0)//递增
{
time++;
if((th>0)&&(th<=200)) //10%
{
if(time<=2) PWM=1;
else if ((time>2)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>200)&&(th<=400)) //20%
{
if(time<=4) PWM=1;
else if((time>4)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>400)&&(th<=600)) //30%
{
if(time<=6) PWM=1;
else if((time>6)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>600)&&(th<=800)) //40%
{
if(time<=8) PWM=1;
else if((time>8)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>800)&&(th<=1000)) //50%
{
if(time<=10) PWM=1;
else if((time>10)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1000)&&(th<=1200)) //60%
{
if(time<=12) PWM=1;
else if((time>12)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1200)&&(th<=1400)) //70%
{
if(time<=14) PWM=1;
else if((time>14)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1400)&&(th<=1600)) //80%
{
if(time<=16) PWM=1;
else if((time>16)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1600)&&(th<=1800)) //90%
{
if(time<=18) PWM=1;
else if((time>18)&&(time<=20)) PWM=0;
else if(time==21)
{
time=0;
th++;
}
}
if((th>1800)&&(th<=2000)) //100%
{
if(time<=20) PWM=1;
else if(time=21)
{
time=0;
th++;
}
}
if((th>2000))
{
flag=1;
th=1999;
time=0;
}
}
/************************************************************************************************/
else if (flag==1)
{
time++;
if((th>=1800)&&(th<2000))
{
if(time<20) PWM=1;
else if(time==21)
{
time=0;
th--;
}
}
if ((th>=1600)&&(th<1800))
{
if(time<18) PWM=1;
else if ((time>=18)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1400)&&(th<1600))
{
if(time<16) PWM=1;
else if ((time>=16)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1200)&&(th<1400))
{
if(time<14) PWM=1;
else if ((time>=14)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=1000)&&(th<1200))
{
if(time<12) PWM=1;
else if ((time>=12)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=800)&&(th<1000))
{
if(time<10) PWM=1;
else if ((time>=10)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=600)&&(th<800))
{
if(time<8) PWM=1;
else if ((time>=8)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=400)&&(th<600))
{
if(time<6) PWM=1;
else if ((time>=6)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=200)&&(th<400))
{
if(time<4) PWM=1;
else if ((time>=4)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if((th>=0)&&(th<200))
{
if(time<2) PWM=1;
else if ((time>=2)&&(time<20)) PWM=0;
else if(time==21)
{
time=0;
th--;
}
}
if(th<0)
{
flag=0;
th=1;
}
}
}
[此贴子已经被作者于2016-7-29 09:36编辑过]