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

Status
Not open for further replies.
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
 
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
 
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/32bit/doc/app_note/AN4381.pdf
 
Last edited:
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.
 
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.
 
Status
Not open for further replies.
Back
Top