31.25 kHz clock source for analogWriteFrequency

Status
Not open for further replies.

cchouard

Member
Hello,
Reading the CPU reference document and the kinetis.h and pins_teensy.c files, I am trying to understand where the 31.25 kHz clock source that the analogWriteFrequency uses for low frequencies comes from.
Is it :
- the external 16mHz crystal source divided by 512 (page 490 : MCG_C1 CLKS = 2, FRDIV = 0x100 : divide factor = 16 or 512) ?
or
- the internal 4mHz source divided by 128 (page 499 : MCG_SC ATMS = 1, MCG_SC FCRDIV = 7) ?
I can see that the analogWriteFrequency function uses ftmClockSource = 2 and ftmClock = 31250, but I cannot find where this 31.25 kHz hardware source is setup in the library files.
Thank you in advance for your enlightenment !
 
You can Serial.println(MCG_C1,HEX); and see the register holds 0x20. So Teensy is using external oscillator (16 mhz) divided by 512 = 31250 hz

set in hardware/teensy/avr/cores/teensy3/mk20dx128.c
MCG_C1 = MCG_C1_CLKS(0) | MCG_C1_FRDIV(4);
 
Thank you manitou !
OK, I see where my confusion comes from :
- on the one side analogWriteFrequency sets ftmClockSource = 2, that is FTMx_SC = 2, namely Fixed frequency clock (page 781), and
- on the other side MCG_C1 CLKS = 0 means «*Output of FLL or PLL is selected*» as a clock source, and MCG_C1 IRFES = 0 which means reference clock source for the FLL is external (page 490)
- and Serial.println(MCG_C2,HEX); gives 0x24 which means RANGE0 = 2, namely «*Very high frequency range selected for the crystal oscillator*» and EREFS0 = 1, namely Oscillator requested as the source for the external reference clock (page 491)
Am I right or am I missing something ?
Thks !
 
Status
Not open for further replies.
Back
Top