注册 登录
编程论坛 单片机编程

有大神可以来看一下C程序哪里不对吗。刚学C的小白

董柯文 发布于 2016-07-28 20:28, 5648 次点击
这个是用51单片机定时器中断写的一个从占空比由10%(每次变化10%)变化到100%,再从100%到10%的一个PWM波的程序。
在占空比为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;
                 }            
    }            
}   
                                            
   


[此贴子已经被作者于2016-7-29 09:36编辑过]

10 回复
#2
董柯文2016-07-28 20:31
我是不是发错区了。。。应该发到单片机区去?
#3
hu9jj2016-07-29 07:46
程序没有半点注释,象我这样的水平看的是一头雾水。
整个程序看起来象是通过定时器对P0.0进行控制,不知道楼主在单片机的P0.0上接了什么硬件。在主程序的循环中没有看到对定时器的读写操作和其他操作,不知道主程序是如何触发子程序T1_ISR()运行的,也许是通过外部中断吧。
我也是刚学单片机,一知半解都谈不上,只能胡乱瞎猜。楼主看来不象是求助,倒象是考官出题,而且不给出程序的运行目的和任何解释,主要考察我们的逻辑分析能力。我上面的猜测也许离题万里,大概只能得0分。
#4
董柯文2016-07-29 09:30
回复 3楼 hu9jj
您是真不懂 还是讽刺我啊。。
#5
hu9jj2016-07-30 11:49
以下是引用董柯文在2016-7-29 09:30:27的发言:

您是真不懂 还是讽刺我啊。。

确实不懂,勉强才看出是对P0.0口进行操作。
#6
醒山2016-08-01 21:46
不懂单片机原理,不了解硬件,C再牛逼也没用
#7
hhlxl2016-08-04 10:20
unsigned char 最多只能技术到256,你那个定时器50ns中断一次,计数应该越界了,可以选择int或long类型
#8
wuguangnan112016-08-05 18:06
感觉是if (time==20) time = 0;
   if (flag == 2000) flag = 1;th不变
#9
sensenhong2016-09-05 09:16
没意义的代码
#10
一指黑2016-09-22 09:19
unsigned char th=1;
能到2000吗
你做好第一个百分比为什么不让它们自行嵌套,你只要记录次数不就行了
#11
q9137942862017-04-27 08:02
这么写没必要,你这样写得到的占空比肯给你是不准的。
你只接用定时器做一个循环自加到100然,然后截取这100的范围做占空比输出不就行了,几行代码搞定的事。
1