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

PWD relationship of duty value

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Just a User
Guest







PWD relationship of duty value
PostPosted: Sun May 04, 2008 11:13 am     Reply with quote

I was under the impresion that the period and duty were directly related. For example if I set the following:
int16 period = 99; //
setup_power_pwm(PWM_FREE_RUN,1,0,period,0,1,0);

then to get 100% duty cycle I would run:
int16 duty_cycle2 = 100; //100%
set_power_pwm0_duty(duty_cycle2);

but that seems to not be the case. If fact with the code below, 4x the period seems to be the 100% duty cycle. Could someone explain the relationship between the two if there is one?


#include<18f4331.h>
#device adc=10
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000) //8Mhz

int16 duty_cycle1, duty_cycle2, duty_cycle3, duty_cycle4;
int16 period = 99; //

void main()
{
setup_oscillator(OSC_8MHZ);
setup_power_pwm_pins(PWM_COMPLEMENTARY ,PWM_COMPLEMENTARY, PWM_OFF, PWM_OFF);
setup_power_pwm(PWM_FREE_RUN,1,0,period,0,1,0);

duty_cycle1 = 800; //???%
duty_cycle2 = 100; //100%
duty_cycle3 = 25; //25%

while (true) {
set_power_pwm0_duty(duty_cycle1);
delay_ms(500);

set_power_pwm0_duty(duty_cycle2);
delay_ms(500);

set_power_pwm0_duty(duty_cycle3);
delay_ms(500);

} //while

}//main()
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun May 04, 2008 2:34 pm     Reply with quote

Quote:
If fact with the code below, 4x the period seems to be the 100% duty
cycle. Could someone explain the relationship between the two if there is
one?

The period is based on units of Fosc/4. But the duty cycle is based on
units of Fosc. To see this, change the main loop in your program to this:
Code:

while (true)
 {
set_power_pwm0_duty(1);
delay_ms(500);
}

With a duty cycle of 1, you'll see a 125 ns pulse on your oscilloscope.
If change it to 2, you'll see a 250 ns pulse. It increments in units of
125 ns. That's the period of the 8 MHz oscillator.

But you can only change the period in units of 500 ns. This means the
setting for your duty cycle has 4x higher resolution than the setting
for the period. That's just the way Microchip designed it. They felt
that users wanted high resolution on the duty cycle setting.
Ttelmah
Guest







PostPosted: Sun May 04, 2008 2:37 pm     Reply with quote

They are directly related.
However the PWM, internally uses the master 'instruction' clock, for the period, and the original input clock for the 'width' (*4). This is why you can have period values up to 255 (256), but width values up to 1023. The actual PWM period control register, only accesses the 8 bit counter, that directly relates to the period value. There are though two 'extra' bits, that provide access to two bits corresponding to the higher speed counter.
CCS, 'changes mode', according to the value you use to control the PWM period. If you use an 8bit value (int8), then it only accesses the main PWM control register. If you use a 16bit value, then it automatically takes the top eight bits of this, and puts these into the main register, and the low two bits into the extra control.
Now, if you use a cast like this:

set_power_pwm0_duty((int8)duty_cycle1);

The PWM, will behave as you expect.
With your constants, the compiler automatically switches to using an int16 type, if the constant is over 255, forcing access to the extra control bits.

Best Wishes
Just a User
Guest







Thanks
PostPosted: Sun May 04, 2008 3:16 pm     Reply with quote

Thanks guys..
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