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

PWM problem

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



Joined: 22 Aug 2010
Posts: 43

View user's profile Send private message

PWM problem
PostPosted: Sat Aug 28, 2010 2:46 am     Reply with quote

Hello,
Am using pic18f2331 to generate multiple pwm outputs. I wrote a test code to see the pwm output, and this is my code:
Code:

#include<18F2331.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT 
#use delay(clock=8000000)

#define POWER_PWM_PERIOD 255  // 1 KHz pwm freq with 8 MHz osc.

//=======================================
void main()
{
setup_power_pwm_pins(PWM_COMPLEMENTARY, PWM_COMPLEMENTARY, PWM_OFF, PWM_OFF);

setup_power_pwm(PWM_FREE_RUN, 1, 0, POWER_PWM_PERIOD, 0, 1,0); 

set_power_pwm0_duty(128); //Duty 0
set_power_pwm2_duty(50); // Duty 1

while(1);
}

When I put pwm period to 255 and pwm0 duty cycle to 128, I predict that I should get 50% duty cycle, but testing this program in Proteus doesn't give so.
How to calculate the value in the duty cycle according to the value in the period?
What is the my problem?
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Aug 28, 2010 2:29 pm     Reply with quote

Use this program as an example. Try to do everything the same way.
Your PIC is the smaller, 28-pin version of the 18F4431, so it doesn't have
pins for PWM6 and PWM7, so you can leave off the last line for PWM6:
http://www.ccsinfo.com/forum/viewtopic.php?t=37206&start=2

Here's an example for a 28-pin PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=30061&start=7


First make it work like those programs, then try to modify it.
At least you will have a working "base" to start from.
hisham.i



Joined: 22 Aug 2010
Posts: 43

View user's profile Send private message

PostPosted: Sun Aug 29, 2010 7:09 am     Reply with quote

Thank you PCM programmer,
I have one more question...
If i put the time base value=0, and the period=3124, with the postscalar =1, does it mean that every time the time base is equal to 3124 an interrupt occur?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Aug 29, 2010 6:25 pm     Reply with quote

You can find the answer by yourself, by making a test program.
For example, the program shown below will output a 50% duty cycle
pwm pulse on pin B3. The interrupt routine will put out a short pulse
on Pin E0 when a PWM Timebase interrupt occurs. What is the frequency
of the signals on pins B3 and E0 ? On my oscillosope they are both about
633 Hz. So, that means the PWM interrupt is occuring at the same rate
as the PWM signal.

Now do the math. Look in the Power PWM section of the 18F4431 data
sheet. Look at the equation below, from the data sheet. It shows that
the PWM period counter runs from the instruction clock (Fosc /4).
Code:

EQUATION 17-1: PWM PERIOD FOR
FREE-RUNNING MODE

         (PTPER + 1) x PTMRPS
TPWM =  ---------------------
              FOSC/4

So with a 8 MHz oscillator, the instruction clock is 2 MHz.

Now do this equation: 2 MHz / (3124 +1) = 640 Hz

I am not sure why there is a difference between the calculated frequency
of 640 Hz and the measured value of 633 Hz. It can't be caused by
interrupt latency because we are not resetting any PWM counters inside
the interrupt routine. The descrepancy amounts to about 35 instruction
cycles. The measured interrupt rate is slower than it should be by that
amount. I'm sure there is a reason, I just don't have time to find out
what it is right now.

But anyway, the answer to your question is yes. You'll get an interrupt
once per PWM period.
Code:

#include<18F4431.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT, NOLVP
#use delay(clock=8000000)

#define POWER_PWM_PERIOD 3124  // For testing

#int_pwmtb
void pwm_isr(void)

// Make a pulse on Pin E0 for the frequency counter.
output_high(PIN_E0);
delay_us(100);
output_low(PIN_E0);
}

//=======================================
void main()
{

// Setup the 4 Power PWM channels as ordinary pwm channels.
setup_power_pwm_pins(PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON, PWM_ODD_ON);

// Mode = Free Run 
// Postscale = 1   (1-16) Timebase output postscaler
// TimeBase = 0   (0-65355) Initial value of PWM Timebase
// Period = 2000  (0-4095) Max value of PWM TimeBase
// Compare = 0     (Timebase value for special event trigger)
// Compare Postscale = 1 (Postscaler for Compare value)
// Dead Time

setup_power_pwm(PWM_FREE_RUN, 1, 0, POWER_PWM_PERIOD, 0, 1,0); 

set_power_pwm0_duty((int16)((POWER_PWM_PERIOD *4) * .5)); // 50% duty cycle

clear_interrupt(INT_PWMTB);
enable_interrupts(INT_PWMTB);
enable_interrupts(GLOBAL);

while(1);
}
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