请问各位发烧友,我用
单片机写程序,输出方波信号,但是输出的波形都是高电平和低电平时都在缓慢变化(如图),示波器器直接接输出信号引脚。我用
仿真和实物测量时都是这个样子,这是为什么呢?
源代码如下:
- /****************************************
- * Queation 6
- * Keys use P1
- * Signals use P0
- * Author: liaoshaohui
- ****************************************/
- #include "reg52.h"
- unsigned int count1; //record the number of 100us
- unsigned int count2; //record the number of 100us
- unsigned int frequency1; //signal1's frequency
- unsigned int frequency2; //signal2's frequency
- unsigned int step_length; //step length
- unsigned char i;
- sbit key1=P1^0; //frequency1+
- sbit key2=P1^1; //frequency1-
- sbit key3=P1^2; //frequency2+
- sbit key4=P1^3; //frequency2-
- sbit key5=P1^4; //phase+
- sbit key6=P1^5; //phase-
- /****************************************
- * Delay function for key scan
- ****************************************/
- void Dlms(void)
- {
- for(i=200; i>0; i--) {}
- }
- /****************************************
- * Interruption function
- ****************************************/
- void Time0_Int() interrupt 1
- {
- if (count1<frequency1&&count2<frequency2)
- P0=0x03;
- else if (count1>=frequency1&&count2<frequency2)
- P0=0x02;
- else if (count1<frequency1&&count2>=frequency2)
- P0=0x01;
- else
- P0=0x00;
- count1=(count1+1)%(2*frequency1);
- count2=(count2+1)%(2*frequency2);
- }
- /****************************************
- * Main function
- ****************************************/
- void main()
- {
- frequency1=400;
- frequency2=800;
- i=0;
- count1=0;
- count2=0;
- step_length=2;
- //Initialize Timer 0
- TMOD = 0x02; //Timer 0, method 2
- TH0= 0x9C;
- TL0= 0x9C; //timing 100us
- TR0=1;
- IE= 0x82;
- //Read key value
- while(1)
- {
- P1=0xFF;
- if ((P1&0xFF)!=0xFF)
- {
- // Dlms();//uncomment when in real work
- if ((P1&0xFF)!=0xFF)
- {
- if((P1&0x01)==0x00)
- frequency1=frequency1+step_length;
- if((P1&0x02)==0x00)
- frequency1=frequency1-step_length;
- if((P1&0x04)==0x00)
- frequency2=frequency2+step_length;
- if((P1&0x08)==0x00)
- frequency2=frequency2-step_length;
- if((P1&0x10)==0x00)
- count2=count2+frequency1/180;
- if((P1&0x20)==0x00)
- count2=count2-frequency1/180;
- if((P1&0x40)==0x00)
- step_length+=2;
- if((P1&0x80)==0x00)
- step_length-=2;
- }
- }
- }
- }
复制代码
-
-
一周热门 更多>