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

Options for lower PWM frequency.....

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



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

Options for lower PWM frequency.....
PostPosted: Wed Nov 04, 2015 5:19 pm     Reply with quote

Hi All,

Compiler: v5.050

I'd like to setup my PWM output for about 1000 Hz or so. The problem is that my clock is 48MHz, so that's proving to be a challenge....

I'm measuring 2.93 Khz on the CCP1 pin of my PIC which agrees with the formula:

f = 48000000/(256 X 16 X4) = 2929 Hz

Code:

#include <18F25K50.h>
#fuses HSH,NOWDT,NOPROTECT,NOLVP,NODEBUG,NOFCMEN,NOPLLEN
#use delay(crystal=16MHz, clock=48MHz, USB_FULL)

//-----< General Program Defines >-----
#define Pwr_LED Pin_A4         // Power LED

//======================================
void main(void)
{
   int8 iIndex;

   // Here we blip the Power LED at power-up to show that the interface is working
   for ( iIndex = 0 ; iIndex < 4 ; iIndex++ )
   {
      output_high(PWR_LED);
      delay_ms(250);
      output_low(PWR_LED);
      delay_ms(250);
   }
      
   // Here we leave the Power LED ON
   output_high(PWR_LED);

   // Here we try some PWM setup code... We are using the full 10 bits of PWM control...
   // This should output a PWM pulse train on CCP1 (Pin 13)...
   setup_ccp1(CCP_PWM_L_L);
   setup_timer_2(T2_DIV_BY_16, 255, 1);

   set_pwm1_duty(128);    // 50% duty cycle

   while(1){} //Stop here!!!
}


My project requires USB, so I believe I'm stuck with the 48MHz clock?

What are my options for a lower PWM frequency with this PIC running USB?

Jack
temtronic



Joined: 01 Jul 2010
Posts: 9249
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Nov 04, 2015 5:59 pm     Reply with quote

That PIC has CPUDIV bits to control the value of the PLL Postscaler as shown in figure 3-1 of the datasheet.
With a 48MHZ input to the USB, the PLL postscaler can be /2,/3,/4 or /6 of the 48MHz. giving you 24,16,12 or 8 MHZ to the clock.
You should be able to say 'clock=8M' and get 8MHz as the PIC clock.

I don't use that PIC but the figure 3-1 is similar to a lot of PICs I have used.

It helps to have a scope confirm the actual PIC speed.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19559

View user's profile Send private message

PostPosted: Thu Nov 05, 2015 2:16 am     Reply with quote

Yes.

Just to explain. The _USB_ has to clock at 48MHz, but your CPU doesn't.

If you look at Table 3-7 in the data sheet, you will see the clock modes compatible with USB listed.
You can run with 12, 16, or 48MHz 'input oscillator frequency', and run the CPU in each case at 8, 16, 24 or 48MHz.
The bottom section (24Mhz in), is only compatible with low speed USB, so you have to use one of the higher three sections.
The PLL has to always be enabled (except for when using the 48MHz external clock).

Code:

#fuses HSH, PLL3X, NOWDT, NOPROTECT, NOLVP, NODEBUG, NOFCMEN
#fuses CPUDIV3
#use delay(clock=16MHz)


Personally I prefer to explicitly set the PLL, and dividers myself. You were setting the PLL 'off', but then the clock settings would have overridden this. A sure way of it not working with some version!...

Here your master clock is multiplied by 3, to give the 48MHz for the USB, and then this is divided by 3 to feed the CPU. This is the sixth line down in the table, 16MHz in, 3*PLL, then /3, giving a CPU clock of 16MHz.

I went for 16MHz, since this is the fastest rate that will give you 1KHz.

If you use PR2=249, you will get exactly 1K, but only have 1000 levels. If you use PR2=255, you will get 976.25Hz.
JAM2014



Joined: 24 Apr 2014
Posts: 138

View user's profile Send private message

PostPosted: Thu Nov 05, 2015 10:05 am     Reply with quote

Hi guys,

Awesome! Thanks for the tips and the clear and concise explanation!

I'm relatively new to these PLL equipped PIC's, and I haven't completely mastered all the nuances! Your help has greatly improved my understanding!!

Thanks,

Jack
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