View previous topic :: View next topic |
Author |
Message |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Aug 16, 2009 1:54 pm |
|
|
Quote: | please would you give to us sample code for 18f1330 or 1230 |
I don't have either of those two PICs to test in hardware, but the following
program might work. The 18F1330 doesn't have the two ordinary PWM
channels that the 18F4431 has. Also, it only has 3 Power PWM channels
instead of the 4 channels that the 18F4431 has. So, I just removed the
lines of code that are not supported in the 18F1330 to create the program
below. Note that the setup_power_pwm_pins() function requires 4
parameters, even though the 18F1330 only supports 3 channels. But
that's not a problem. Try this program and see if it works. If it doesn't,
then post your compiler version.
Code: |
#include<18F1330.h>
#fuses INTRC_IO, NOWDT, NOPROTECT, NOBROWNOUT, PUT
#use delay(clock=8000000)
#define POWER_PWM_PERIOD 1999 // 1 KHz pwm freq with 8 MHz osc.
//=======================================
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) * .1)); // 10%
set_power_pwm2_duty((int16)((POWER_PWM_PERIOD *4) * .4)); // 40%
set_power_pwm4_duty((int16)((POWER_PWM_PERIOD *4) * .6)); // 60%
while(1);
} |
|
|
|
hisham.i
Joined: 22 Aug 2010 Posts: 43
|
|
Posted: Wed Oct 27, 2010 1:41 pm |
|
|
It is an old post, but i want to ask about the duty calculation.
why we multiply the pwm_period by 4? |
|
|
mcr1981
Joined: 27 Oct 2010 Posts: 28
|
|
Posted: Wed Oct 27, 2010 2:31 pm |
|
|
hisham.i wrote: | It is an old post, but i want to ask about the duty calculation.
why we multiply the pwm_period by 4? |
To make use of the full 10 bit resolution, that's why they are forcing the 16 bit convesion. Maybe I'm wrong but I think that's why. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Oct 27, 2010 2:59 pm |
|
|
Download the 18F1330 data sheet.
http://ww1.microchip.com/downloads/en/DeviceDoc/39758D.pdf
Look in this section. In a gray box on the right side, it says:
Quote: |
14.6 PWM Duty Cycle
Note:
To get the correct PWM duty cycle, always
multiply the calculated PWM duty cycle
value by four before writing it to the PWM
Duty Cycle registers. This is due to the two
additional LSBs in the PWM Duty Cycle
registers which are compared against the
internal Q clock for the PWM duty cycle
match.
|
Always download the PIC data sheet and read the section on the
peripheral module that you want to use. |
|
|
hisham.i
Joined: 22 Aug 2010 Posts: 43
|
|
Posted: Mon Nov 01, 2010 12:54 am |
|
|
is it applied also to pic18f2331? |
|
|
mcr1981
Joined: 27 Oct 2010 Posts: 28
|
|
Posted: Mon Nov 01, 2010 10:35 am |
|
|
hisham.i wrote: | is it applied also to pic18f2331? |
To every PIC micro that has the 2 lsb in the middle of the CCPxCON.
You can discard those two bits and "play with an 8 bit PWM", only modify CCPxRL. |
|
|
|