CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Question about #use pwm() and frequency

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

Question about #use pwm() and frequency
PostPosted: Fri Apr 16, 2021 11:32 am     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 16, 2021 1:48 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 16, 2021 2:29 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Apr 16, 2021 4:29 pm     Reply with quote

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

View user's profile Send private message Visit poster's website

PostPosted: Fri Apr 16, 2021 4:33 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Sat Apr 17, 2021 12:01 am     Reply with quote

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).
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group