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

Thread: Teensy Timer/Counter Example for the T3 & T3.1

  1. #1
    Senior Member
    Join Date
    Apr 2013
    Posts
    170

    Teensy Timer/Counter Example for the T3 & T3.1

    Hello,

    is there an simple sample for an Timer/Counter from the T3 and T3.1 Chip analog to this one

    http://arduinomega.blogspot.de/2011/...-lets-get.html

    for the AVR.

    Or does I need to dig into the Manual?

  2. #2
    Senior Member sumotoy's Avatar
    Join Date
    Nov 2012
    Location
    Venezia, Italia
    Posts
    421
    Maybe not fully respond your question but this can help to buildUp precise timers based on isr.
    Teensy3 has IntervalTimer in core (used for Tone) that it's easy to use:

    Code:
    IntervalTimer myTimer;
    
    void setup(void) {
    myTimer.begin(myCallback,1000L*1000); // 1 second
    }
    
    void loop(){
    }
    
    void myCallback(){
    //...do something
    }
    You don't need to include IntervalTimer.h since it's in core.

    You can take a look inside hardware/teensy/cores/teensy3/IntervalTimer.h (..and IntervalTimer.cpp) how it's done

  3. #3
    Senior Member
    Join Date
    Apr 2013
    Posts
    170
    Quote Originally Posted by sumotoy View Post
    Maybe not fully respond your question but this can help to buildUp precise timers based on isr.
    Teensy3 has IntervalTimer in core (used for Tone) that it's easy to use:

    Code:
    IntervalTimer myTimer;
    
    void setup(void) {
    myTimer.begin(myCallback,1000L*1000); // 1 second
    }
    
    void loop(){
    }
    
    void myCallback(){
    //...do something
    }
    You don't need to include IntervalTimer.h since it's in core.

    You can take a look inside hardware/teensy/cores/teensy3/IntervalTimer.h (..and IntervalTimer.cpp) how it's done
    Not really but I see Paul did allready set the FlexTimer Registers in the mk20dx128.h

  4. #4
    Senior Member
    Join Date
    Apr 2013
    Posts
    170
    Btw. that looks interesting for my idea

    http://forum.pjrc.com/threads/610-Te...ll=1#post43884

  5. #5
    Senior Member
    Join Date
    Apr 2013
    Posts
    170
    Did something but the Prescaler seems not to work

    Code:
    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);
    }
    Found this but did not help http://cache.freescale.com/files/32b...ote/AN4381.pdf
    Last edited by Markus_L811; 03-19-2014 at 11:24 PM.

  6. #6
    Senior Member
    Join Date
    Apr 2013
    Posts
    170
    I saw Paul did this in the pins_teensy.c

    Code:
    // create a default PWM at the same 488.28 Hz as Arduino Uno
    #if F_BUS == 48000000
    #define DEFAULT_FTM_MOD (49152 - 1)
    #define DEFAULT_FTM_PRESCALE 1
    #else
    #define DEFAULT_FTM_MOD (49152 - 1)
    #define DEFAULT_FTM_PRESCALE 0
    #endif
    
    //void init_pins(void)
    void _init_Teensyduino_internal_(void)
    {
    	init_pin_interrupts();
    
    	//SIM_SCGC6 |= SIM_SCGC6_FTM0;	// TODO: use bitband for atomic read-mod-write
    	//SIM_SCGC6 |= SIM_SCGC6_FTM1;
    	FTM0_CNT = 0;
    	FTM0_MOD = DEFAULT_FTM_MOD;
    	FTM0_C0SC = 0x28; // MSnB:MSnA = 10, ELSnB:ELSnA = 10
    	FTM0_C1SC = 0x28;
    	FTM0_C2SC = 0x28;
    	FTM0_C3SC = 0x28;
    	FTM0_C4SC = 0x28;
    	FTM0_C5SC = 0x28;
    	FTM0_C6SC = 0x28;
    	FTM0_C7SC = 0x28;
    	FTM0_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
    	FTM1_CNT = 0;
    	FTM1_MOD = DEFAULT_FTM_MOD;
    	FTM1_C0SC = 0x28;
    	FTM1_C1SC = 0x28;
    	FTM1_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
    #if defined(__MK20DX256__)
    	FTM2_CNT = 0;
    	FTM2_MOD = DEFAULT_FTM_MOD;
    	FTM2_C0SC = 0x28;
    	FTM2_C1SC = 0x28;
    	FTM2_SC = FTM_SC_CLKS(1) | FTM_SC_PS(DEFAULT_FTM_PRESCALE);
    #endif
    
    	analog_init();
    	//delay(100); // TODO: this is not necessary, right?
    	delay(4);
    	usb_init();
    }
    Is this maybe an reason, I don't know. Paul or else please help.

  7. #7
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,381
    Your code in reply #5 sets the timer for an external clock, but I don't see any lines that configure a pin. There may be other issues, but that's the one obvious thing I can see from only a quick look.

  8. #8
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    26,381
    Here's some other code I recently wrote that uses the FTM0 timer. It's hardly simple, but maybe it'll help?

    https://github.com/PaulStoffregen/Pu...sePosition.cpp

Posting Permissions

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