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

PWM PIC24 Calculations - help needed

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



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PWM PIC24 Calculations - help needed
PostPosted: Fri Sep 21, 2012 8:16 am     Reply with quote

Compiler: 4.132
PIC: 24HJ128GP306

Please, I *know* that there's documentation to read, but I have the docs opened in front of me but I'm a bit lost and need help, which is what this board is for so please, bear with me:

So on a previous circuit with a PIC18, the calculations for the PWM frequency was the following:
Code:

PWM =      Xtal frequency
       ----------------------------
       (mode) x (period + 1) x 4

Mode can be only 1/4/16. Period is a value between 0-255, 4 is a fixed number required by the equation. Using a 24.576MHz crystal, I can get 128kHz flat using MODE=1, PERIOD = 47.

At the moment, my board with a PIC24HJ, it only has a 20Mhz crystal and on my oscilloscope I see 128.2kHz. I got to this PWM value by trial-and-error because I just can't wrap my head around the docs. I'll change the crystal to a 24.576MHz when I have a chance but I'll work and calculate for now using what I have...

So, given the hardware configuration, this is my code to get to 128.2kHz:
Code:

setup_compare( 1, COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 77 );
set_pwm_duty( 1, 39 );

This is where I'm confused with the PIC24 PWM calculations. I don't get all the parts of the calculations (as much as _I am reading_ the datasheets, I just can't wrap my head around it). The Microchip datasheet says the following:

PWM Period = [(PRy) + 1] * Tcy * (TMRy Prescale Value)
PWM Frequency = 1/PWM Period

For the frequency, that's fine: 1/0.0000078 = 128205 Hz

Where I'm having the problem is how to _get to_ 0.0000078 given that I am using a 20Mhz crystal.

PRy+1 is any value between 0 and 65535, in my case, 77 SO:

128205 = [(77)+1] * Tcy * (TMRy Prescale Value)

In the PIC18's calculations, there's the x4 "fixed" part which I don;t see for the PIC24 calculations. However, if I was to use the PIC18's calculations, I simply need to change the fixed part '4' to '2' and that works:

Code:

PWM =      Xtal frequency
          ----------------------------
          (mode) x (period + 1) x 2 (for PIC18, it's 4)

128205 =        20,000,000
          --------------------------
             1 x (77 + 1 ) x 2

128205 = 20,000,000 / 156

But in the datasheets, I don't see anything pertaining to that "fixed" part and I'd like it if someone could perhaps shed some light.

For the PIC18, I made myself an Excel spreadsheet with two calculations:

1) I enter the crystal frequency, the mode and period and it gives me the PWM output frequency;
2) I enter the desired PWM frequency, mode and period and it gives me the required crystal;

I'd like to be able to do the same thing with the PIC24 but I'm confused.

Thank you.

Benoit
ck



Joined: 02 May 2012
Posts: 18

View user's profile Send private message

PostPosted: Fri Sep 21, 2012 9:37 am     Reply with quote

Hi,
I have pic24hj and i have setup my PWM in this way:
I have 10MHz External Cristal with PLL enabled so fcycle=40MHz
so Tcycle=25nsec
Code:

setup_timer2(TMR_INTERNAL|TMR_DIV_BY_1,40000);   
setup_compare(1,COMPARE_PWM|COMPARE_TIMER2|COMPARE_CONTINUE_IDLE);

Timer 2 is the Base frequency an 40000 is counter. so i have 25nsec*40000=1ms -> so f_PWM=1KHz

Duty cycle is easy. For example, Duty=25%
You have to 25*40000/100=10000
Code:

set_pwm_duty (1,10000);
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Sep 21, 2012 10:03 am     Reply with quote

Hello, thanks for your info but it's not quite what I'm asking or perhaps, it's not very clear.

So, your "frequency" is 40MHz (25ns). If I follow your example and make the same calculation using my 20MHz crystal, this is what I get:

0.00000005 (50ns) * 78 = 0.0000039 (3.9us)

1/3.9us = 256410 <--- PROBLEM, it's twice what I'm currently seeing on my oscilloscope.

So again, using MY code in MY example, how did I get to 128205Hz? Your calculations don't jive with my calculations although you seem to be getting the PWM value you're looking for and I'M getting what I'm looking for, but I don't understand the calculation:

setup_compare( 1, COMPARE_PWM | COMPARE_TIMER2 );
setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, 77 );
set_pwm_duty( 1, 39 );

With the above three lines AND the information I provided, what makes it that I get to 128.205KHz? Again, I'm using a 20MHz crystal and the period value is 77 (the PWM duty is 50% therefore the value 39)...
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Fri Sep 21, 2012 10:26 am     Reply with quote

UPDATE:

I changed my crystal from a 20MHz to a 38.4MHz and now, the value for setup_timer2( TMR_INTERNAL | TMR_DIV_BY_1, ulPWMValue ); is 149. This gives me a perfect, on the spot, 128kHz PWM @ 50% duty (set_pwm_duty value = 75).

So now, how is the calculation done (referring to my PIC18 calculation I posted above)? If I use CK's calculation, it doesn't make any sense:

1/38400000 = 26.041667ns * 149 = 3.8802083ms -> 257.718kHz, which doesn't make any sense at all (when I change CK's values for mine) because my oscilloscope shows me a perfect 128kHz.

Thanks.
Ttelmah



Joined: 11 Mar 2010
Posts: 19538

View user's profile Send private message

PostPosted: Sun Oct 14, 2012 9:06 am     Reply with quote

Your original calculation is exactly right.

So:

38,400,000/((149+1)*2)

= 38400000/(300 = 128000

The key point about the factor of two, is the peripheral clock. In the PIC18, this is Fosc/4. On the PIC24, it is Fosc/2.

It is shown in the oscillator diagram for most of the PIC24's, with 'Fp', coming from Fosc/2.

On the PIC18, it is slightly less obvious, being instead shown on the timer block diagrams.

Best Wishes
benoitstjean



Joined: 30 Oct 2007
Posts: 566
Location: Ottawa, Ontario, Canada

View user's profile Send private message

PostPosted: Mon Oct 15, 2012 5:38 am     Reply with quote

Good! I wasn't too sure if it was the /2 factor. So that all makes sense now. Thank you!
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