PWM for PIC18F2550

Status
Not open for further replies.
in the documentation of the compiler C18 we can find

Code:
/******************************************************************
* NOTES:
* Code uses the Peripheral library support available with MCC18 Compiler
* Code Tested on:
* PicDem2+ demo board with PIC18F4685 controller
* PWM output is obtained on CCP1 pin. duty cycle is gievn by
*
*Formula for Period and Duty cycle calculatio
*
*        PWM period =   [(period  ) + 1] x 4 x Tosc x TMR2 prescaler
*
*        PWM x Duty cycle = (DCx<9:0>) x Tosc
*
*        Resolution (bits) = log(Fosc/Fpwm) / log(2)
**********************************************************************/

#define USE_OR_MASKS
#include <p18cxxx.h>
#include "pwm.h"

void main(void)
{
  char period=0x00;
  unsigned char outputconfig=0,outputmode=0,config=0;
  unsigned int duty_cycle=0;

//----Configure pwm ----
    period = 0xFF;
    OpenPWM1( period);            //Configure PWM module and initialize PWM period

//-----set duty cycle----
        duty_cycle = 0x0F00;
        SetDCPWM1(duty_cycle);        //set the duty cycle

//----set pwm output----
    outputconfig = FULL_OUT_FWD ;
    outputmode = PWM_MODE_1;
    SetOutputPWM1( outputconfig, outputmode);    //output PWM in respective modes

    while(1);                    //observe output on CCP1 pin

//-----close pwm----
  ClosePWM1();

}

Now... I am unexperienced on PWM, so I have a hard time understanding this.

For example, lets see the duty cycle

Code:
duty_cycle = 0x0F00;
        SetDCPWM1(duty_cycle);


Isn't it supposed that the duty cycle is 10 bits??
but 0x0F00= 1111 0000 0000 right? so 12 bits....
in this example, how much percentage (%) are we putting...?




(as an aside note, I tried to compile it for my PIC18F2550 and it didnt work. It didnt recognize the macros FULL_OUT_FWD and PWM_MODE_1)

Can anybody help me on this???
 
No one answered so here's my take:
Looking at pw1setdc.c we see:
Code:
#include <htc.h>
#include <peripheral/pwm.h>
#include <peripheral/pwm_io.h>

#if defined (_PWM_V1) || defined (_PWM_V2) || defined (_PWM_V3) || defined (_PWM_V4) || defined (_PWM_V5) || defined (_PWM_V6) || defined (_PWM_V7) || defined (_EPWM_V7) || defined (_PWM_V8)  || defined (_PWM_V9) ||  defined (_PWM_V10) ||  defined (_PWM_V11) || defined (_PWM_V13)

void SetDCPWM1(unsigned int dutycycle)
{
  // Write the high byte into CCPR1L
	CCPR1L=(dutycycle>>8)& 0xFF;
  // Write the low byte into CCP1CON5:4
	DC1B0=(dutycycle&0x40)?1:0;
	DC1B1=(dutycycle&0x80)?1:0;
// 2 bittest-bitset/clear instructions would be better than below method 
// provided it's ok to modify CCP1CON twice instead of in one go
//CCP1CON = (CCP1CON & 0xCF) | ((dutycycle >> 2) & 0x30);


/*  union PWMDC DCycle;

  // Save the dutycycle value in the union
  DCycle.lpwm = dutycycle << 6;

  // Write the high byte into CCPR1L
  CCPR1L = DCycle.bpwm[1];

  // Write the low byte into CCP1CON5:4
  CCP1CON = (CCP1CON & 0xCF) | ((DCycle.bpwm[0] >> 2) & 0x30);
*/
}

#endif

So it only takes the first 10 bits of duty_cycle into account:
11 1100 0000
 
I think in the first line you may need a " 1" at the end, both FULL_OUT_FWD and PWM_MODE_1 are defined in pwm.h when USE_OR_MASKS is defined
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…