Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: PWM pulse generator

  1. #1
    Junior Member
    Join Date
    Mar 2017
    Posts
    1

    PWM pulse generator

    hi all. I want to ask you for help.
    I am using Teensy 3.2 and need to do pulse generator with PWM 1:10 and frequency 10Hz on one pin, and on second 100Hz (doesn't matter which one).
    As far as I understand it needs to be done via interupts on timers. but can you guide me how?
    thanks a lot.

  2. #2
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    Maybe you could use analogWriteFrequency? It's very simple and produces highly accurate results.

    https://www.pjrc.com/teensy/td_pulse.html

  3. #3
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    thanks a lot. the link you sent answeared in 100%. :-)
    I also looked at the link to forum where guy needs to do generator with 2Hz. there was an example of analogWriteFrequencySlow...
    but if i copy this code to my code, compiler throw this exception:

    'FTM1_CH0_PIN' was not declared in this scope
    if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {

    what do I need to include the compiler knows this names correctly?
    thanks

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    20,556
    First, analogWriteFrequency does support slow speeds.

    The thread is probably old info. But who can say, since you didn't give a link.

    You also mentioned a compile error, but didn't post code. That's not how we work here. When you ask for help with any code issue, the expectation is a complete program which can be copied into Arduino to reproduce the problem. If you look at the many threads on this forum, you'll see we do very well at truly solving problems when complete code is posted, and when code is missing, usually the result is a lot of time-wasting guesswork.

    Please, help us to help you. Post complete code if you ask these types of questions.

  5. #5
    Junior Member
    Join Date
    Feb 2014
    Posts
    9
    realy sorry for my bad english!!!
    there wasn't problem in compiler but in my code. now I found solution on some forum.
    I didn't post the code cause I didn't know there to start and everithing what I wrote was trash. but your link solved it all.
    thanks a lot for your help.


    my code:

    #define FTM0_CH0_PIN 22
    #define FTM0_CH1_PIN 23
    #define FTM0_CH2_PIN 9
    #define FTM0_CH3_PIN 10
    #define FTM0_CH4_PIN 6
    #define FTM0_CH5_PIN 20
    #define FTM0_CH6_PIN 21
    #define FTM0_CH7_PIN 5
    #define FTM1_CH0_PIN 3
    #define FTM1_CH1_PIN 4
    #define FTM2_CH0_PIN 32
    #define FTM2_CH1_PIN 25

    const int ledPin = 13;

    const int btn = 1;
    const int FreqOutPin = 5;
    bool bFreqState = true; //true=2Hz, false=100Hz

    void setup()
    {
    pinMode(ledPin, OUTPUT);
    pinMode(FreqOutPin, OUTPUT);
    pinMode(btn, INPUT_PULLUP);

    analogWriteFrequency(FreqOutPin, 2); // 2Hz
    attachInterrupt(btn, isrBtn, FALLING);
    }

    void loop()
    {
    analogWrite(FreqOutPin, 25); //strieda 10% z 255
    }

    void isrBtn()
    {
    cli();
    if( bFreqState )
    {
    bFreqState = false;
    analogWriteFrequencySlow(FreqOutPin, 100); //switch to 100Hz
    }
    else
    {
    bFreqState = true;
    analogWriteFrequencySlow(FreqOutPin, 2); //switch to 2Hz
    }
    delay(600);
    sei();
    }

    void analogWriteFrequencySlow(uint8_t pin, float frequency)
    {
    uint32_t prescale, mod;
    float minfreq;

    for (prescale = 0; prescale < 7; prescale++) {
    minfreq = (float)(31250 >> prescale) / 65536.0f;
    if (frequency >= minfreq) break;
    }
    mod = (float)(31250 >> prescale) / frequency - 0.5f;
    if (mod > 65535) mod = 65535;
    if (pin == FTM1_CH0_PIN || pin == FTM1_CH1_PIN) {
    FTM1_SC = 0;
    FTM1_CNT = 0;
    FTM1_MOD = mod;
    FTM1_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
    } else if (pin == FTM0_CH0_PIN || pin == FTM0_CH1_PIN
    || pin == FTM0_CH2_PIN || pin == FTM0_CH3_PIN
    || pin == FTM0_CH4_PIN || pin == FTM0_CH5_PIN
    #ifdef FTM0_CH6_PIN
    || pin == FTM0_CH6_PIN || pin == FTM0_CH7_PIN
    #endif
    ) {
    FTM0_SC = 0;
    FTM0_CNT = 0;
    FTM0_MOD = mod;
    FTM0_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
    }
    #ifdef FTM2_CH0_PIN
    else if (pin == FTM2_CH0_PIN || pin == FTM2_CH1_PIN) {
    FTM2_SC = 0;
    FTM2_CNT = 0;
    FTM2_MOD = mod;
    FTM2_SC = FTM_SC_CLKS(2) | FTM_SC_PS(prescale);
    }
    #endif
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •