Arduino生成上升波形(正弦或三角形)

时间:2019-03-22 03:12:41

标签: arduino hardware arduino-uno

我想施加一个不断增加的电压,并保持arduino UNO的输出。我意识到arduino不允许我输出模拟值,因此我决定使用R2R梯形图(R-22kohms和2R-47kohms)。这将允许我转换为模拟电压。我利用Arduino上的八个数字引脚来设置8位R2R阶梯。在当前设置下,我能够输出正弦波,但是对于如何输出达到最大值并停止的波形有点不确定。 (即下图所示的波浪)。 enter image description here 该波基本上是三角波,甚至是正弦波,一直上升到最大值并停留在那里(具有200微秒的脉冲持续时间)。

我创建了电路的视觉效果,以更好地演示我的问题: enter image description here

我也通过输出正弦波来尝试解决我的问题。我的代码如下:

void setup() {
  //set pins 0-7 as outputs
  for (int i=0; i<8; i++){
    pinMode(i, OUTPUT);
  }
}

void loop() {
  double value =0; 
  int check=0; int t=0; 
  while(check==0){
    if (value<254){ 
      value = 127+127*sin(2*3.14*t/100); 
      //this sends a sine wave centered around (127/255 * 5)=2.5V
      //max will reach when t=25
      PORTD=value; 
      delayMicroseconds(4); //wait 4 micro seconds
      //this means that the max value will reach at ~25*6 =150 microseconds 
    }
    else{
      value =255; 
      PORTD=value; //just output the max of the sine wave (i.e. 255)
      delayMicroseconds(50); //delay to ensure total duration is 150+50=200 microseconds
      PORTD=0; //output back a 0
      check=1; //condition to exit the loop
    }
    t=t+1; 
  }
}

由于某种原因,生成的脉冲与我所寻找的不完全相同。我做错什么了吗?还是对这样的东西有更好的实现?此外,如果我的问题中缺少某些内容,请告诉我。

1 个答案:

答案 0 :(得分:0)

  

我意识到arduino不允许我输出模拟值

为了输出模拟值,请使用Arduino的模拟输出之一。 它们被标记为

这是Arduino reference中的一个示例:

int ledPin = 9;      // LED connected to digital pin 9
int analogPin = 3;   // potentiometer connected to analog pin 3
int val = 0;         // variable to store the read value

void setup() {
  pinMode(ledPin, OUTPUT);  // sets the pin as output
}

void loop() {
  val = analogRead(analogPin);  // read the input pin
  analogWrite(ledPin, val / 4); // analogRead values go from 0 to 1023, analogWrite values from 0 to 255
}