9 comments on “Timer Input Capture on Teensy 3.0

  1. Hi!
    Thank you very much for posting your code!!!!

    Could you tell me please, how do you find, which pins are usable for Input Capture?!

    Regards

  2. Dave-

    I’m don’t remember exactly where in the manual I found the info (probably in the pin definitions section) but I did make a note of the pins for the FlexTimer0 module:

    Pins that can be used for Input Capture:
    Teensy   Name    Pin    Signal
    9        PTC3    46     FTM0_CH2
    10       PTC4    49     FTM0_CH3
    22       PTC1    44     FTM0_CH0
    23       PTC2    45     FTM0_CH1

    I believe there are additional pins associated with the FTM1 module so searching the manual for references to these signals should help.

  3. This means that one could one those pins for FTM0 und the others for FTM1 and so on…
    Thank you very much Paul!

    I will fine that information

    Regards

  4. Ok, found it
    For all the poeple who need it

    Page 193, 194, 195, 196 of the Manual. Under the column ALT4.
    It seems one could change ALTx throght the port control module. But I don t know how.

    Regards

  5. Dave-

    In my example code above, the following line selects the FlexTimer function on pin PTC1 (physical pin 44 on the K20 and pin 22 on the Teensy):

    PORTC_PCR1 |= 0×400; //Teensy pin 22

    To have the other pins selected for FlexTimer functions, the code would be

    PORTC_PCR2 |= 0×400; //Teensy pin 23
    PORTC_PCR3 |= 0×400; //Teensy pin 9
    PORTC_PCR4 |= 0×400; //Teensy pin 10

    Similarly for Port D, pin PTD4,

    PORTD_PCR4 |= 0×400; //Teensy pin 6 – selects FTM0_CH4

    I focused on FTM0_CH0 through CH3 because they didn’t interfere with my using the OctoWS2811 library and I didn’t need to use those pins for any other functions.

  6. Hi Paul,

    I see this code great stuff, I looking for something different, I want to set up an counter on the Teensy 3.1 but I did not get it to work. The Prescaler wouldn’t do. My Code can you take a look.


    volatile uint32_t actually;
    volatile uint32_t old = 0;

    #define FTM0_WPEN 0x40

    void setup() {
    // Write Protection Disable
    FTM0_MODE |= FTM_MODE_WPDIS;

    FTM0_MODE |= FTM_MODE_FTMEN;

    // enable Overflow Interrupt
    FTM0_SC |= FTM_SC_TOIE;

    // enable the clock for FTM0
    FTM0_SC |= FTM_SC_CLKS(0b11);
    // 00 No clock selected. This in effect disables the FTM counter.
    // 01 System clock
    // 10 Fixed frequency clock
    // 11 External clock

    // set Prescaler
    //FTM0_SC |= FTM_SC_PS(0b111);
    FTM0_SC |= 0b110;
    // 000 Divide by 1
    // 001 Divide by 2
    // 010 Divide by 4
    // 011 Divide by 8
    // 100 Divide by 16
    // 101 Divide by 32
    // 110 Divide by 64
    // 111 Divide by 128

    // set the counter initial value
    FTM0_CNT = 0;

    // enable the clock for FTM0
    SIM_SCGC6 |= SIM_SCGC6_FTM0;

    // enable IRQ Interrupt
    NVIC_ENABLE_IRQ(IRQ_FTM0);

    FTM0_FMS |= FTM0_WPEN;

    // initialize serial
    Serial.begin(115200);
    }

    void loop() {
    // nothing
    //Serial.println(FTM0_CNT);
    }

    void ftm0_isr() {
    actually = micros();
    Serial.print("ECHO ");
    Serial.println(actually - old);
    old = actually;

    //Serial.println(FTM0_CNT);
    }

  7. Markus-

    The first thing I noticed was that you selected External Clock as the clock source for the Timer Module here: FTM0_SC |= FTM_SC_CLKS(0b11); In my example I used the Fixed Frequency clock. I would have to look at the datasheet again to see how to use an external clock with the Timer Module (pins, frequency, register settings, etc.). Was that intentional on your part? Are you feeding the Teensy with another clock source for this timer? Have you looked at possibly using the IntervalTimer library from PJRC?

  8. Hi Paul,

    I have loaded this code into a Tweensy 3.0 via Arduino 1.0.5 and I cannot get it to work any ideas?

    Trying to make a speedo for my bike ( bluetooth to my S3 phone)

    Regards
    Anthony

  9. Anthony-

    Try the following code for the ISR:

    extern "C" void ftm0_isr(void) {
    // save current interrupt count/time
    FTM0_CNT = 0x0000;
    FTM0Count = FTM0_C0V;
    oneRevolution = 1;
    if ((FTM0_SC&FTM_SC_TOF) != 0)
    //timerOverflow = FTM0_SC&FTM_SC_TOF;
    timerOverflow = 1;
    else
    timerOverflow = 0;
    FTM0_SC &= ~FTM_SC_TOF;
    // clear channel interrupt flag (CHF)
    FTM0_C0SC &= ~0x80;
    }

    That is what I am doing in my code for the bike wheel.

    Thanks,

    Paul

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>