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

12f1822 PWM basic question

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



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

12f1822 PWM basic question
PostPosted: Sat May 05, 2012 4:28 pm     Reply with quote

I'm trying to set up a pwm output of roughly 38Khz and I'm just a little confused about how to set up timer2.

In the data sheet, it says to set up and enable timer2 in order to start the PWM output. I've configured the PWM so this should be the last step.

My question is, will setting up timer2 values have any effect on the PWM output speed? Can I just use the following:

Code:
void main()
{
   setup_timer2(T2_DIV_BY_1, 127, 1); //
   set_timer2(0); //reset/initiate the timer???
}



here's my code:

Code:


#include <12f1822.h>
#use delay(clock=32000000)
#Fuses INTRC_IO,NOWDT,NOPROTECT,NOMCLR
#byte APFCON = 0x01 //CCP on RA5
#byte PR2 = 0xD2 // PR2
#bit T2CON = 1.0 // prescaler is 1 (1)
#bit T2CON = 1.1 // prescaler is 1 (1)
#use rs232(xmit=pin_a4,baud=4800,invert)

void main()
{
   setup_oscillator(OSC_8MHZ | OSC_PLL_ON);
   setup_ccp1(CCP_PWM);                                        // Configure CCP1 as a PWM
   set_pwm1_duty (50);     
   
   setup_timer_2(T2_DIV_BY_1, 127, 1); //
   set_timer2(0); //reset/initiate the timer???
   
   set_tris_a    (0x2f);       //  101111  (0=output)      5 4 3 2 1 0
   PORT_a_PULLUPS(0x00);       //  000000  (1=enabled)     5 4 3 2 1 0
   
   while(1)
   {
      //test pwm output
   }
}


This is my first PWM attempt so don't be too harsh :P
_________________
Vinnie Ryan
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat May 05, 2012 11:19 pm     Reply with quote

What's your compiler version ?
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Sun May 06, 2012 3:17 am     Reply with quote

Like PCM programmer says, read the forum guide and supply ALL the relevant information.

Generally speaking, your code
Code:

   setup_timer2(T2_DIV_BY_1, 127, 1); //


Will generate a PWM frequency given by:-

PWM frequency = ( Fosc / 4) / (mode * period )

In your case that's

32MHz / 4 / 1 / (127 + 1) = 62.5 kHz

So, the simple answer is yes. Timer2 setup does affect PWM frequency.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sun May 06, 2012 3:44 am     Reply with quote

Also, remember that as well as affecting the speed, it'll affect the range of numbers useable for the duty cycle.

Best Wishes
vinniewryan



Joined: 29 Jul 2009
Posts: 154
Location: at work

View user's profile Send private message MSN Messenger

PostPosted: Sun May 06, 2012 12:48 pm     Reply with quote

Perfect, thanks. Using your equation I figure 'mode' will need to be 210 in order to achieve as close to 38Khz as possible.

(32000000 / 4) / 1 / (210 + 1) = 37914.7
_________________
Vinnie Ryan
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