FTM timer problems

Status
Not open for further replies.

so999

Member
Hello
I tested ftm1 in order to understand how it works. I didn’t know how to connect the pin to the timer channel so I simply copied the define macro from “pins_teensy.c” (but I don’t understand what those define lines do).

This is my simple sketch which is supposed to put ftm1 in edge aligned mode and to produce a pulse with a frequency of about 85828 kHz. I thought that putting C0V at half it will produce a 50% duty cycle:

Code:
uint8_t pulsePin1 = 3;
#define FTM1_CH0_PIN 3
#define FTM_PINCFG(pin) FTM_PINCFG2(pin)
#define FTM_PINCFG2(pin) CORE_PIN ## pin ## _CONFIG

void setup() {
  pinMode(pulsePin1, OUTPUT);
  FTM_PINCFG(FTM1_CH0_PIN) = PORT_PCR_MUX(3) | PORT_PCR_DSE | PORT_PCR_SRE;
  
  FTM1_COMBINE=0;
  FTM1_SC=B1001000;
  FTM1_C0SC=B101000;
  FTM1_MOD = 500;
  FTM1_C0V = 250;
}

void loop() {
}

I measured the output with an oscilloscope and the problem is that the output frequency is about 95.8kHz. Also the duty cycle is about 22%.
I need some help to understand why the frequency and duty cycle don’t match.
 
Try using 558 for FTM1_MOD.

Or use this:

Code:
void setup() {
  analogWriteFrequency(3, 85828);
  analogWrite(3, 128);
}

void loop() {
}
 
Thank you Paul!

Yes, with FTM1_MOD=558 the frequency is correct. I figured why the frequency didn’t match. I made a stupid mistake – I used 43Mhz instead of 48Mhz, for F_BUS.
Also the duty cycle wasn’t 50% due to some other components (transistor, resistors). When I put the probe on the output pin everything match perfectly.

I tried analogwrite and it works very nice, but I have some problems using it.
 
Last edited:
Status
Not open for further replies.
Back
Top