View previous topic :: View next topic |
Author |
Message |
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
Question about #use pwm() and frequency |
Posted: Fri Apr 16, 2021 11:32 am |
|
|
Hi,
I'm trying to use the #use pwm() directive like so:
Code: | #pin_select CCP4OUTA=BLDC_PWM
#use PWM(CCP4, TIMER=2, FREQUENCY=10000, BITS=10, DUTY=0, PWM_ON, STREAM=WIPERPWM) |
I get this compiler output:
Code: | C:\Users\dluu\Desktop\CMB_TESTBED\408-NEW_WIPER\pic24_can_testbed\wipermotor.c:33:1: Info#300 More info: PWM period: 64.00 us, Frequency: 15.625 kHz, Resolution: 10.00 bits: |
If I try to set 5 kHz, I get 3.906 kHz.
In my case, the actual frequency doesn't matter too much for me. However, I'm wondering what's the way to get the correct frequency?
Besides the weird frequency, the duty cycle is all coming out correctly and I'm able to control my motor just fine.
I'm using PIC24FJ1256GL408 on PCD 5.103. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 16, 2021 1:48 pm |
|
|
Here is a pwm calculator:
http://micro.alleypress.org/
Using the lower section, I experimented with plugging in numbers
and got 10KHz with these values:
Pic speed: 64000000
Prescale: 16
PR2 value: 0x63
Pulse Frequency: 10000 |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Apr 16, 2021 2:29 pm |
|
|
Hi PCMP,
thanks for that.
So I guess I'd have to manually go in and set the PR2 register then?
It's not really so critical for me so I'll leave it as is but this is good to know... |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Apr 16, 2021 4:29 pm |
|
|
Remove the BITS=10 parameter.
If you do that, it will give:
Code: | MOVLW 63
MOVWF PR2
MOVLW 06 // prescaler = /16
MOVWF T2CON
|
That should give 10 KHz. |
|
|
dluu13
Joined: 28 Sep 2018 Posts: 395 Location: Toronto, ON
|
|
Posted: Fri Apr 16, 2021 4:33 pm |
|
|
Interesting. I guess bits takes precedence over frequency.
Thanks for the investigation! I'll give that a try on Monday. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Sat Apr 17, 2021 12:01 am |
|
|
Yes. Bits forces the value in the PR2 register to be ((2^bits)/4)-1.
Restricts the actual frequencies supported massively. To get 10K, PCM's
figures are:
Pic speed: 64000000
Prescale: 16
PR2 value: 0x63
Pulse Frequency: 10000
which sets PR2 to 63, allowing 8bits. (64*4 = 256 -> 8 bits). |
|
|
|