asaed330
Newbie level 1
- Joined
- Nov 22, 2014
- Messages
- 1
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 11
I am using a Microchip dsPIC30F2020.
I've been trying for the longest time to get some sort of PFM signal for my project. I see in Section 12.4.7 there is the ability to have constant off-time PWM - which is exactly what I want. The only condition is it has to be an external timer reset. After some reading, it seems I can use the comparator output to generate this external timer reset. However, I am getting no PWM output what so ever from the chip.
I have it set it to a programmed period of PTPER=8000 (77.6kHz). I am using CMP1A as the input to the comparator and I am using an external 0.9V voltage source to EXTREF. I have it set to independent output mode already as well. My code is attached below for guidance on what I did wrong.
I tried to model the code for my dsPIC30F2020 from the sample code from Microchip with no success. ()
What detail am I missing or did I overlook? Any help would be appreciated. Thank you.
I've been trying for the longest time to get some sort of PFM signal for my project. I see in Section 12.4.7 there is the ability to have constant off-time PWM - which is exactly what I want. The only condition is it has to be an external timer reset. After some reading, it seems I can use the comparator output to generate this external timer reset. However, I am getting no PWM output what so ever from the chip.
I have it set it to a programmed period of PTPER=8000 (77.6kHz). I am using CMP1A as the input to the comparator and I am using an external 0.9V voltage source to EXTREF. I have it set to independent output mode already as well. My code is attached below for guidance on what I did wrong.
I tried to model the code for my dsPIC30F2020 from the sample code from Microchip with no success. ()
What detail am I missing or did I overlook? Any help would be appreciated. Thank you.
Code:
#include "p30F2020.h"
/* Configuration Bit Settings */
_FOSCSEL(FRC_PLL)
_FOSC(CSW_FSCM_OFF & FRC_HI_RANGE & OSC2_CLKO & HS_EC_DIS)
_FWDT( FWDTEN_OFF & WINDIS_ON & WDTPRE_PR128 & WDTPOST_PS32768 )
_FPOR(PWRT_128)
_FGS( CODE_PROT_OFF & GWRP_OFF )
_FBS(BSS_NO_FLASH)
void init_PWM();
void init_CMP();
int main(void)
{
init_PWM(); /* Initialize the PWM module */
init_CMP(); /* Initialize the Comparator module */
while(1);
}
void init_PWM(void)
{
PTPER = 8000; /* PFM Period = 12.88 usec @ 20 MIPS for 77.6Hz */
/* 1.61nsec for Extended */
/* Refer to PWM section for more details */
MDC = 4255; /* Constant off time of 6.55us */
/* Initialize PWM Generator 1 */
IOCON1bits.PENH = 1; /* PWM Module controls High output */
IOCON1bits.PENL = 0; /* GPIO Module controls Low output */
IOCON1bits.POLH = 1; /* High Output Polarity is active LOW */
IOCON1bits.POLL = 1; /* Low Output Polarity is active LOW */
IOCON1bits.PMOD = 1; /* Independent output mode */
IOCON1bits.OVRENH = 0; /* PWM generator provides data for PWM1H */
IOCON1bits.OVRENL = 1; /* OVRDAT<0> provides data for PWM1L pin */
IOCON1bits.OVRDAT = 0; /* OVRENL = 1, so OVRDAT provides for PWM1L */
PWMCON1bits.FLTSTAT = 0; /* Clear Fault Interrupt flag */
PWMCON1bits.CLSTAT = 0; /* Clear Current Limit Interrupt flag */
PWMCON1bits.TRGSTAT = 0; /* Clear PWM Trigger Interrupt flag */
PWMCON1bits.FLTIEN = 0; /* Disable Fault Interrupt */
PWMCON1bits.CLIEN = 1; /* Enable Current Limit Interrupt */
PWMCON1bits.TRGIEN = 1; /* Enable Trigger Interrupt */
PWMCON1bits.ITB = 1; /* PHASE1 provides time base period for PWM */
PWMCON1bits.MDCS = 1; /* Duty cycle is read from MDC */
PWMCON1bits.DTC = 0; /* DTC=0: Postive dead time actively applied for all output modes */
PWMCON1bits.XPRES = 1; /* Current limit source resets time base for PWM1 */
PWMCON1bits.IUE = 0; /* Disable immediate update to PDC */
FCLCON1bits.CLSRC = 0; /* Current limit control signal source is comparator #1 */
FCLCON1bits.CLMODE = 1; /* Enable current limit mode for PWM1 */
FCLCON1bits.CLPOL = 0; /* Current limit polarity for PWM 1 is active HIGH */
DTR1 = 160; /* Deadtime = DTR1*1.61nsec = 257.6nsec */
ALTDTR1 = 160; /* Deadtime = ALTDTR*1.61nsec = 257.6nsec */
PHASE1 = 0; /* No phase shift */
IFS1bits.PWM1IF = 0; /* Clear PWM1 Interrupt Flag status */
IEC1bits.PWM1IE = 1; /* Comparator #1 interrupt enable bit */
PTCON = 0x8000; /* Enable PWM Module */
}
void init_CMP()
{
/* Intialize the Comparator */
LATBbits.LATB0 = 0; /* Initialize RB0 */
TRISBbits.TRISB0 = 0; /* CMP1A is an output */
LATEbits.LATE6 = 0; /* Initialize RE6 */
TRISEbits.TRISE6 = 1; /* EXTREF is an input */
CMPCON1bits.INSEL = 0; /* CMP1A is selected */
CMPCON1bits.EXTREF = 1; /* External reference for DAC. EXTREF = 0.9V */
CMPCON1bits.CMPPOL = 0; /* Output is not inverted */
CMPCON1bits.CMPON = 1; /* Comparator module is enabled */
}
void __attribute__((interrupt, no_auto_psv)) _PWM1Interrupt()
{
IFS1bits.PWM1IF = 0; /* Clear PWM1 Interrupt Flag status */
}