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 support@ccsinfo.com

How to calculate PWM freq

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



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

How to calculate PWM freq
PostPosted: Fri Aug 27, 2010 1:18 pm     Reply with quote

I know this has been discussed before, but I cannot find the thread. Can someone explain an easy way to calculate the values needed to set a specific freq on the PWM pins? I know it is setup_timer_2, but reading the CCS manual and the pic datasheet does not help alot.
I'm using a 452 or a 4520 at 20Mhz. I want to set up freqs from 1khz to 4khz. What I need is a simple explanation of what each of the three terms really mean. Prescale, period, and postscale.

Thanks
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 27, 2010 1:27 pm     Reply with quote

PWM frequency formula:
http://www.ccsinfo.com/forum/viewtopic.php?t=17993

Duty cycle formula:
http://www.ccsinfo.com/forum/viewtopic.php?t=42417&start=1

PWM postscaler explanation:
http://www.ccsinfo.com/forum/viewtopic.php?t=29786&start=1
Sample code for PWM postscaler:
http://www.ccsinfo.com/forum/viewtopic.php?t=41473&start=3
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Fri Aug 27, 2010 1:45 pm     Reply with quote

thanks guys, that is what I needed.

I made a little 1 line spreadsheet to help me out. Is there any way to post it here?
Ringo
_________________
Ringo Davis
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Aug 27, 2010 1:58 pm     Reply with quote

Someone else has posted a spreadsheet in the Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=42808

Here is a PWM calculator program:
http://www.ccsinfo.com/forum/viewtopic.php?t=40450

I haven't tried them.


Microchip AN539 also has some useful information:
Quote:
Frequency and Resolution Options for PWM Outputs

http://ww1.microchip.com/downloads/en/AppNotes/00539c.pdf
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sat Aug 28, 2010 1:02 pm     Reply with quote

Can someone tell me what is wrong here? I get 1.2khz out, but it never changes to 4 khz. This is too simple to not work. What am I missing? Or can you not change the freq once it is set?

Ringo
Code:

#include <18f452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)

void main()
{
    setup_ccp1(CCP_PWM);
    setup_ccp2(CCP_PWM);
    enable_interrupts(INT_TIMER2); // Timer 2 Overflow
    enable_interrupts(global);     // Enable IRQs
    while(1)
    {
        setup_timer_2(T2_DIV_BY_16,255,1);//1.2khz //
        set_pwm1_duty (498L);
        set_pwm2_duty (498L);
        delay_ms(1000);
        setup_timer_2(T2_DIV_BY_16,77,1);// 4 khz
        set_pwm1_duty (498L);
        set_pwm2_duty (498L);
        delay_ms(1000);
       
    }

}

_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sat Aug 28, 2010 1:21 pm     Reply with quote

I guess it is something with my duty cycle. If I do this
Code:

void main()
{
    int x=0;
    setup_ccp1(CCP_PWM);
    setup_ccp2(CCP_PWM);
    enable_interrupts(INT_TIMER2); // Timer 2 Overflow
    enable_interrupts(global);     // Enable IRQs
    while(1)
    {
       for(x=255;x>70;x--)
       {
            setup_timer_2(T2_DIV_BY_16,x,1);//1.2khz //
            set_pwm1_duty ((128));
            set_pwm2_duty ((128));
            delay_ms(100);
       }
    }
}

Then it starts out at 50% duty cycle, but they duty cycle goes up as the freq goes up until it is just high all the time. What is the calculation for 50% duty cycle at varying freqs?

Ringo
_________________
Ringo Davis
Ringo42



Joined: 07 May 2004
Posts: 263

View user's profile Send private message

PostPosted: Sat Aug 28, 2010 1:22 pm     Reply with quote

nevermind, got it, x/2. Makes perfect sense.
_________________
Ringo Davis
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