单片机/MCUwilliam hill官网
直播中

wddh

8年用户 14经验值
私信 关注
[问答]

为什么单片机输出方波信号,方波高低电平都不稳定

请问各位发烧友,我用单片机写程序,输出方波信号,但是输出的波形都是高电平和低电平时都在缓慢变化(如图),示波器器直接接输出信号引脚。我用仿真和实物测量时都是这个样子,这是为什么呢?
源代码如下:
  1. /****************************************
  2. *                                Queation 6
  3. *                                Keys use P1
  4. *                                Signals use P0
  5. *                                Author: liaoshaohui
  6. ****************************************/

  7. #include "reg52.h"

  8. unsigned int count1;                                                                            //record the number of 100us
  9. unsigned int count2;                                                                            //record the number of 100us
  10. unsigned int frequency1;                                                                        //signal1's frequency
  11. unsigned int frequency2;                                                                        //signal2's frequency
  12. unsigned int step_length;                                                                        //step length
  13. unsigned char i;

  14. ***it key1=P1^0;                                                                                                                //frequency1+
  15. ***it key2=P1^1;                                                                                                                //frequency1-
  16. ***it key3=P1^2;                                                                                                                //frequency2+
  17. ***it key4=P1^3;                                                                                                                //frequency2-
  18. ***it key5=P1^4;                                                                                                                //phase+
  19. ***it key6=P1^5;                                                                                                                //phase-


  20. /****************************************
  21. *                                Delay function for key scan
  22. ****************************************/
  23. void Dlms(void)
  24. {
  25.     for(i=200; i>0; i--) {}
  26. }


  27. /****************************************
  28. *                                Interruption function
  29. ****************************************/
  30. void Time0_Int() interrupt 1
  31. {
  32.     if (count1
  33.         P0=0x03;
  34.     else if (count1>=frequency1&&count2
  35.         P0=0x02;
  36.     else if (count1=frequency2)
  37.         P0=0x01;
  38.     else
  39.         P0=0x00;
  40.     count1=(count1+1)%(2*frequency1);
  41.     count2=(count2+1)%(2*frequency2);
  42. }


  43. /****************************************
  44. *                                Main function
  45. ****************************************/
  46. void main()
  47. {
  48.     frequency1=400;
  49.     frequency2=800;
  50.     i=0;
  51.     count1=0;
  52.     count2=0;
  53.     step_length=2;

  54.     //Initialize Timer 0
  55.     TMOD = 0x02;                                                                                                //Timer 0, method 2
  56.     TH0= 0x9C;
  57.     TL0= 0x9C;                                                                                                        //timing 100us
  58.     TR0=1;
  59.     IE= 0x82;

  60.     //Read key value
  61.     while(1)
  62.     {
  63.         P1=0xFF;
  64.         if ((P1&0xFF)!=0xFF)
  65.         {
  66.             //        Dlms();//uncomment when in real work
  67.             if ((P1&0xFF)!=0xFF)
  68.             {
  69.                 if((P1&0x01)==0x00)
  70.                     frequency1=frequency1+step_length;
  71.                 if((P1&0x02)==0x00)
  72.                     frequency1=frequency1-step_length;
  73.                 if((P1&0x04)==0x00)
  74.                     frequency2=frequency2+step_length;
  75.                 if((P1&0x08)==0x00)
  76.                     frequency2=frequency2-step_length;
  77.                 if((P1&0x10)==0x00)
  78.                     count2=count2+frequency1/180;
  79.                 if((P1&0x20)==0x00)
  80.                     count2=count2-frequency1/180;
  81.                 if((P1&0x40)==0x00)
  82.                     step_length+=2;
  83.                 if((P1&0x80)==0x00)
  84.                     step_length-=2;
  85.             }
  86.         }
  87.     }
  88. }


  • simulation.png
  • simulationcurrent.png

回帖(5)

武林

2017-12-31 12:02:53
AC/DC选择错误
无标题.jpg
举报

人中狼

2017-12-31 15:16:01
P0口要加上拉电阻的
举报

wddh

2017-12-31 16:20:03
引用: wulinwl 发表于 2017-12-31 15:51
AC/DC选择错误

十分感谢。。我看到示波器选择的自动就没有管,一调过来就好了。谢谢。
举报

wddh

2017-12-31 16:27:25
引用: 人中狼 发表于 2017-12-31 15:16
P0口要加上拉电阻的

没有注意到P0这点。谢谢你指出。
举报

莫家良

2018-1-3 17:16:48
学学 以后也玩这个
举报

更多回帖

发帖
×
20
完善资料,
赚取积分