Hi everyone!
I have a trouble in using PWM in Maple (r5 with ide 0.0.12)
The frequency should be 1.1kHz,
as written here under "Difference 4: PWM frequency":
http://leaflabs.com/docs/lang/api/analogwrite.html#lang-analogwrite
But in each of pwm pin (i tried pin 0 1 2 3 5 6 7 8 9 11 12 14 24 27 28) i have a frequency of 550Hz!
The delay(), micros() and millis() functions are properly working.
I have a "faulty" Maple or is there a mistake in the reference page?
Is it a known issue?
Anyone had this problem before?
Thank you
Wrong PWM frequency, 1.1kHz or 550Hz?
(8 posts) (3 voices)-
Posted 2 years ago #
-
crk - Welcome to the forum. I hope we can help.
How are you measuring the frequency?
It's almost 3am here, so I'm not going to check, on a Maple I'm off to bed.
However, I think it might be slightly misleading documentation.There are a bunch of modes that Maple's STM32F103 (and Arduino ATmega) timers can operate in.
One mode counts from 0 to 'max' in one cycle, then counts down from 'max' to 0 in one cycle, then it repeats counting up, then down. So each PWM pulse takes two cycles of the timer.
IIRC, this is the mode which two of the Arduino's three timers use (the other is the clock for millis, and operates as a single cycle 0 to 255, 0 to 255 ... counter).
I also think it is the mode that the Maple's STM32F103's timers are running in. This is called "Center-aligned mode (up/down counting)" in the RM0008 manual. It says "Center-aligned mode is active when the CMS bits in TIMx_CR1 register are not equal to '00'." (page 359/1096)
The register is TIMx_CR1, and the CMS bits are bits are bits 5 & 6 (page 388/1096)
If you need it to run at 1.1KHz, you could do several things, using the timer API http://leaflabs.com/docs/lang/api/hardwaretimer.html#lang-hardwaretimer
If you explain what you need to do, we may be able to help.
(Full disclosure: I am not a member of LeafLabs staff.)
Posted 2 years ago # -
Hi gbulmer, and thank you for your interest!
I have measured this frequency with my oscilloscope in each pwm pin,
and comparing each pin with each other i noticed they are all rising edge alligned.
So i think all counters are up counters and all setted identically.
I'm talking about a Maple running at its bootloader defaults without modifying any timers registers.
Infact i don't have nothing particular to do, but i only wants to know if it is an issue or if the reference page linked before has been written wrong.
Have yuo ever measured the pwm frequency of your maple?
Thank you!Posted 2 years ago # -
crk - AFAICT you are correct, the code only sets upcounting. IMHO, you are also correct that the reference page is (partly) wrong.
The Maple start-up code sets all timers pre-scaler is 0xFFFF, so if the source clock were 72MHz, then 72MHz / 0xFFFF = 1,099Hz
However, Timer 1, is AFAICT on a different clock 'domain' (or bus), APB2, to the other timers which are on APB1 ('domain' or bus).
APB2 can run at 72MHz but APB1 runs at a maximum of 36MHz ("Figure 11. Clock tree", page 123/1096, RM0008, Doc ID 13902 Rev 14)
The RM0008 manual says: "The maximum frequency of the AHB and the APB2 domains is 72 MHz. The maximum allowed frequency of the APB1 domain is 36 MHz." (page 124/1096)
So, assuming I've read the manual correctly, the input clock to Timer 1 should be 72MHz, and the input clock to the other Timers should be 36MHz. Timers running at 36MHz should have a period of 36MHz/0xFFFF = 549Hz.
So that, combined with upcounting only, would explain that part of your measurement.IMHO you are correct, the page you linked is incorrect for the 'General Purpose' timers (TIM2 to TIM5).
However AFAICT Timer 1 (TIM1) should be running at 72MHz.So I don't understand why outputs from Timer 1 are running at the same frequency, and not twice that frequency.
IIRC I've only measured the frequency of timer pulses when I am trying to get the timer to run at a specific frequency, usually by fiddling with the prescale value.
Posted 2 years ago # -
The base timer clocks are running at 72Mhz. For a 36 MHz bus, bus clock divisor 2, the timer clock is 2x the bus clock as seen in the timer tree for the RCC.
The setup value of the prescale registers is set 1, giving a prescale division factor of 2, making the timers run at 36 MHz.
With an auto reload of 65536 ( 0xFFFF ) this will give about 550Hz.The setting of PSC = 1 is done in wirish/boards.cpp : timer_default_config()
Posted 2 years ago # -
Hi, thank you everyone for the explanations.
So in conclusion the reference page is plenty wrong about the frequency,
all the outputs coming from all timers (TIM1 included) has not the 1.1kHz frequency.
Not only my oscilloscope, but also the RM0008 manual interpretation says the frequency has to be 550Hz! ;)
I'll contact the Leaflabs team for one their opinion.Posted 2 years ago # -
just set the division factor to one (0 in the PSC registers) and its 1.1 kHz
Posted 2 years ago # -
timer.setPrescaleFactor(1);
This should do it if timer is the hardware timer connected to the specific pin.
The RM does not say PWM must be 550. I have tested it at 1.1 kHz with 16 bit resolution.
Posted 2 years ago #
Reply
You must log in to post.