【问题标题】:Arduino pulse trainArduino脉冲列车
【发布时间】:2018-02-13 08:26:58
【问题描述】:

我给你做个小介绍:

我正在研究 Stanley Meyer 的水燃料电池。不了解水燃料电池的可以看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 赫兹到 100 赫兹)的脉冲序列来调制这个脉冲,以充当脉冲门。我正在考虑使用 Timer 0 来计数并在计数某个值时关闭信号,并在再次达到相同值时激活,就像这样

TCCR0A = _BV(COM0A0) | _BV(COM0B0) | _BV(WGM01);
TCCR0B = _BV(CS02);
OCR0A = 90;
OCR0B = OCR0A * 0.8;

并与计数器比较

 if (TCNT0 <= OCR0A)
     TCCR2A ^= (1 << COM2A0);

但是效果不好。对此有何想法?

【问题讨论】:

    标签: arduino pwm


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-04
      • 2017-02-27
      • 2010-12-06
      相关资源
      最近更新 更多