Using PWM in Particle Photon

Hello All,
I recently bought Photon for my project using PWM module. I had doubts in build environment & in sample PWM code available.

  1. How do I know the files that are compiles in Web IDE (*.ino file)? It takes most of the definitions automatically
  2. Are the definition of register & memory in cloud or can I have local copy to compile in Partcle Dev IDE?
  3. I was trying to use PWM module, I tried compiling sample code (https://gist.github.com/Sperryfreak01/a4282fce7c64f44036ae) i am getting error as “‘RCC_APB2Periph_AFIO’ was not declared in this scope”. I wanted to know the headers to be included for theses definitions.

regards,
Hemanth

That example code was written for the Core, which uses a STM32F103 microprocessor. The photon uses the STM32F205 - while there is much in common, there are some differences. Specifically, RCC_APB2Periph_AFIO, is not present in the STM32 libraries for the 205.

For the PWM code to work requires rewriting it for the STM32F205.

@bdub / @peekay123 / @mdma

I believe I have pwm ‘working’ for a motor (fan) application. However I do believe there is an issue with trying to set the PWM frequency - it doesn’t appear to honour the TIM_TimeBaseStructure.TIM_Period setting and just uses the default range of 65536.
This can be observed by setting TIM_Period in the same way as bdub’s original code (https://gist.github.com/technobly/8313449), ie taking a stab at the internal advanced clock frequency (72Mhz is referred to on page 10 of http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf) and then dividing by the desired frequency.

The attached gist on a photon does step a fan (dc-dc regulator circuit) nicely from 0-100% but its not as stable at low speeds as it used to be when I was able to reliably set the PWM frequency to 24Khz. Thoughts?

1 Like

Have you measured the new PWM’s frequency, is it 24kHz? Not stable at low PWM could indicate the PWM is glitching or you don’t have enough drive current available for the motor. Actually a lot of factors could contribute to this but if you just swap out a core for photon in your circuit then it’s most likely a glitchier PWM, or wrong frequency.

Not sure how to measure it.

I’m nearly positive that the PWM commands are taking no effect, despite matching the chosen pin to timer to channel. The reason for this is the period scale is still 65k, not the period value I determine after doing the clock/frequency calculation. Everything I read (including Bdub’s code comments) says the set period becomes the 100% value. If I set period to say 1000, setting a value of say 800 does nothing, but if I did 80% of 65k then the motor starts working, hence my impression the period is not being honoured.

I suppose there are a few questions.

  • Should the timer clock frequency be 120MHz or is it double, half, or another number entirely?
  • Has anyone got the timers working with a non-default frequency?
  • Thinking about it, I may try the TX pin that maps to TIM1 / CH2 and see if that works.
  • It’d still be great to hear if anyone has found this out. It’d nearly be interesting to know how to modify the default 500Hz.