2016-08-24 104 views
0

我会给你一点小小的介绍:Arduino脉冲列车

我正在研究斯坦利迈尔的水燃料电池。对于那些不知道水燃料电池的人,你可以看到它here

对于水燃料电池必须建立一个电路。这里是the diagram

​​

现在我工作的脉冲发生器(变量)和脉冲门(变量)来生成这个波形。

Pulse train

所以,我想与Arduino的计时器来做到这一点。我已经可以产生一个“高频”脉冲发生器(1千赫 - 10千赫,这取决于预分频在TCCR2B寄存器)PWM引脚3与此代码:

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); 

但它不好用。任何想法?

回答

0

这几天我试图创建一个类似于你的问题的波形发生器。我无法消除出现的抖动或不匹配,但我可以创建一个这样的波形。试试这个例子并修改它:

#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; 
}