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

2019-07-15 09:05发布

请问各位发烧友,我用单片机写程序,输出方波信号,但是输出的波形都是高电平和低电平时都在缓慢变化(如图),示波器器直接接输出信号引脚。我用仿真和实物测量时都是这个样子,这是为什么呢?
源代码如下:
  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. sbit key1=P1^0;                                                                                                                //frequency1+
  15. sbit key2=P1^1;                                                                                                                //frequency1-
  16. sbit key3=P1^2;                                                                                                                //frequency2+
  17. sbit key4=P1^3;                                                                                                                //frequency2-
  18. sbit key5=P1^4;                                                                                                                //phase+
  19. sbit 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<frequency1&&count2<frequency2)
  33.         P0=0x03;
  34.     else if (count1>=frequency1&&count2<frequency2)
  35.         P0=0x02;
  36.     else if (count1<frequency1&&count2>=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
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。