我想应用增加的电压并保持我的arduino UNO的输出。我意识到arduino不允许我输出模拟值,因此我决定使用R2R梯形图(使用R-22kohms和2R-47kohms)。这将允许我转换为模拟电压。我利用arduino上的8个数字引脚来设置8位R2R梯形图。我可以输出一个正弦波,用我当前的设置,但有点不确定如何输出一个上升到最大值并停止的波。 (即如下图所示的波浪)。 这个波浪基本上是一个三角波,甚至是一个正弦波,它达到最大值并保持在那里(脉冲持续时间为200微秒)。
我也通过输出正弦波来尝试我的问题。我的代码如下:
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;
}
}
由于某种原因,产生的脉冲并不是我想要的。有什么我做错了吗?或者有更好的实现这样的事情?另外,如果我的问题中遗漏了一些内容,请告诉我。
我意识到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
}