我将给你一个小小的介绍:
我正在研究斯坦利·迈耶的水燃料电池。对于那些不知道水燃料电池的人,你可以在here上看到它。
对于水燃料电池,人们必须建立一个电路。这是the diagram

现在我正在研究脉冲发生器(变量)和脉冲门(变量)来生成这个波形。

所以,我想用Arduino计时器来做这个。我已经可以用下面的代码在引脚3上产生一个“高频”脉冲发生器(1 kHz - 10 kHz,取决于TCCR2B寄存器的预分频):
pinMode(3, OUTPUT);
pinMode(11, OUTPUT);
TCCR2A = _BV(COM2A0) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(WGM22) | _BV(CS21) | _BV(CS20);
OCR2A = 180;
OCR2B = 50;我可以使用以下命令修改频率和脉冲:
sensorValue = analogRead(analogInPin);
sensorValue2 = analogRead(analogInPin2);
// Map it to the range of the analog out:
outputValue = map(sensorValue, 0, 1023, 30, 220);
outputValue2 = map(sensorValue2, 0, 1023, 10, 90);
OCR2A = outputValue;这工作得很好。
现在我想用另一个具有“低频”(大约20 Hz到100 Hz )的脉冲序列来调制这个脉冲,作为一个脉冲门。我在考虑使用计时器0来计数和关闭信号,当它计数到某个值时,并在再次达到相同的值时激活,如下所示
TCCR0A = _BV(COM0A0) | _BV(COM0B0) | _BV(WGM01);
TCCR0B = _BV(CS02);
OCR0A = 90;
OCR0B = OCR0A * 0.8;并与计数器进行比较
if (TCNT0 <= OCR0A)
TCCR2A ^= (1 << COM2A0);但它并不能很好地工作。对此有什么想法吗?
发布于 2016-12-22 02:06:33
这些天来,我一直在尝试创建一个像你问题中提到的那样的波形发生器。我不能消除出现的抖动或不匹配,但我可以创建这样的波。尝试此示例并对其进行修改:
#include <TimerOne.h>
const byte CLOCKOUT = 11;
volatile byte counter=0;
void setup() {
Timer1.initialize(15); // Every 15 microseconds change the state
// of the pin in the wave function giving
// a period of 30 microseconds
Timer1.attachInterrupt(Onda);
pinMode(CLOCKOUT, OUTPUT);
digitalWrite(CLOCKOUT, HIGH);
}
void loop() {
if (counter>=29) { // With 29 changes I achieve the amount of pulses I need.
Timer1.stop(); // Here I create the dead time, which must be in HIGH.
PORTB = B00001000;
counter = 0;
delayMicroseconds(50);
Timer1.resume();
}
}
void Onda(){
PORTB ^= B00001000; // Change pin status
counter += 1;
}https://stackoverflow.com/questions/39130310
复制相似问题