yokel
Junior Member level 3
Dear all,
I need to generate PWM signal with dsPIC33FJ64GS610 for generating PWM signal. I am using the following code but failed to get any PWM output. It would be nice if anyone come forward with any advice.
I need to generate PWM signal with dsPIC33FJ64GS610 for generating PWM signal. I am using the following code but failed to get any PWM output. It would be nice if anyone come forward with any advice.
Code:
#include "p33FJ64GS610.h"
/* Configuration Bit Settings */
_FOSCSEL(FNOSC_FRC)
_FOSC(FCKSM_CSECMD & OSCIOFNC_ON)
_FWDT(FWDTEN_OFF)
_FPOR(FPWRT_PWR128)
_FICD(ICS_PGD2 & JTAGEN_OFF)
void init_PWM(void);
int main()
{
/* Configure Oscillator to operate the device at 40Mhz
Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
Fosc= 7.37*(43)/(2*2)=80Mhz for Fosc, Fcy = 40Mhz */
/* Configure PLL prescaler, PLL postscaler, PLL divisor */
PLLFBD=41; /* M = PLLFBD + 2 */
CLKDIVbits.PLLPOST=0; /* N1 = 2 */
CLKDIVbits.PLLPRE=0; /* N2 = 2 */
__builtin_write_OSCCONH(0x01); /* New Oscillator FRC w/ PLL */
__builtin_write_OSCCONL(0x01); /* Enable Switch */
while(OSCCONbits.COSC != 0b001); /* Wait for new Oscillator to become FRC w/ PLL */
while(OSCCONbits.LOCK != 1); /* Wait for Pll to Lock */
/* Now setup the ADC and PWM clock for 120MHz
((FRC * 16) / APSTSCLR ) = (7.37 * 16) / 1 = ~ 120MHz*/
ACLKCONbits.FRCSEL = 1; /* FRC provides input for Auxiliary PLL (x16) */
ACLKCONbits.SELACLK = 1; /* Auxiliary Oscillator provides clock source for PWM & ADC */
ACLKCONbits.APSTSCLR = 7; /* Divide Auxiliary clock by 1 */
ACLKCONbits.ENAPLL = 1; /* Enable Auxiliary PLL */
while(ACLKCONbits.APLLCK != 1); /* Wait for Auxiliary PLL to Lock */
TRISE = 0; /* Set as a digital output */
LATE = 1;
init_PWM();
while(1); /* Infinite Loop */
}
void init_PWM()
{
PTPER = 2404; /* PTPER = ((1 / 400kHz) / 1.04ns) = 2404, where 400kHz
is the desired switching frequency and 1.04ns is PWM resolution. */
/*~~~~~~~~~~~~~~~~~~~~~~~ PWM1 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
IOCON1bits.PENH = 0; /* PWM1H is controlled by GPIO module */
IOCON1bits.PENL = 1; /* PWM1L is controlled by PWM module */
IOCON1bits.PMOD = 1; /* Select Independent Output PWM mode */
PDC1 = 640; /* Initial Duty cycle */
DTR1 = 64; /* Deadtime setting */
ALTDTR1 = 64; /* Deadtime setting */
PHASE1 = 0; /* No phase shift */
/*~~~~~~~~~~~~~~~~~~~~~~~ PWM2 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
IOCON2bits.PENH = 0; /* PWM2H is controlled by GPIO module */
IOCON2bits.PENL = 1; /* PWM2L is controlled by PWM module */
IOCON2bits.PMOD = 1; /* Select Independent Output PWM mode */
PDC2 = 640; /* Initial Duty cycle */
DTR2 = 64; /* Deadtime setting */
ALTDTR2 = 64; /* Deadtime setting */
PHASE2 = 240; /* approximately 250ns phase shift */
PTCONbits.PTEN = 1; /* Enable the PWM Module */
}
Last edited: