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

Setting up for PWM

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



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 06, 2004 6:13 pm     Reply with quote

The following test program will give you a 50 KHz pwm frequency,
with 400 steps available. This gives you better than 8-bit resolution.

The key is to use a 16-bit variable as the parameter in the
set_pwm1_duty() function. Notice that I'm using a variable, below.
If you use a constant, be sure to declare it as a "long" by putting
a "L" on the end. Example:

set_pwm1_duty(200L);

When you specify it as a 16-bit value, this tells the compiler
to use 10-bit resolution mode when it compiles the code.

Code:
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use Delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

main()
{
int16 pwm_duty;

output_low(PIN_C2);    // Set CCP1 output low

setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_1, 99, 1);         

pwm_duty = 200;      // Set for 50% duty cycle

set_pwm1_duty(pwm_duty);                   

while(1);
}
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Fri Feb 06, 2004 9:19 pm     Reply with quote

PCM programmer wrote:
The following test program will give you a 50 KHz pwm frequency,
with 400 steps available. This gives you better than 8-bit resolution.

The key is to use a 16-bit variable as the parameter in the
set_pwm1_duty() function. Notice that I'm using a variable, below.
If you use a constant, be sure to declare it as a "long" by putting
a "L" on the end. Example:

set_pwm1_duty(200L);

When you specify it as a 16-bit value, this tells the compiler
to use 10-bit resolution mode when it compiles the code.

Code:
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT,NOLVP
#use Delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

main()
{
int16 pwm_duty;

output_low(PIN_C2);    // Set CCP1 output low

setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

setup_timer_2(T2_DIV_BY_1, 99, 1);         

pwm_duty = 200;      // Set for 50% duty cycle

set_pwm1_duty(pwm_duty);                   

while(1);
}


Is that going to toggle the output on quarter cycles of the instruction clock?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Feb 06, 2004 9:56 pm     Reply with quote

Quote:
Is that going to toggle the output on quarter cycles of the instruction clock?

Yes. Microchip doesn't come right out and say it as plainly as they
should, but yes, it will.

In AN594, Using the CCP Module, on page 2, Microchip says:
"This gives a maximum accuracy of Tosc (50 ns when the device
is operated at 20 MHz)".

In the 16F877 data sheet they say that in 10-bit mode, the two
LSB's come from the pre-scaler for Timer2. That pre-scaler
is clocked at Fosc, so that means that 2-bit counter will count
at a 50 ns rate. They have this small note at the bottom of
the PWM block diagram:

"Note 1: The 8-bit timer is concatenated with 2-bit internal Q
clock, or 2 bits of the prescaler, to create 10-bit time-base".
davt



Joined: 07 Oct 2003
Posts: 66
Location: England

View user's profile Send private message

setting up for PWM
PostPosted: Sat Feb 07, 2004 1:48 pm     Reply with quote

Very Happy
Many thanks for your help.
Have a good weekend!

Dave
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