I'm a beginner with dspic and I'm trying to generate a pwm signal to control a buck. However, observing the output signal, the pin always remains in the same state (LOW). Has anyone ever suffered from this?
PWM.c
void PWM_Initialize (void)
{
// MODSEL Independent Edge; TRGCNT 1; CLKSEL Master clock; ON enabled;
PG1CONL = 0x8;
// MCLKSEL FOSC - System Clock; LOCK disabled; DIVSEL 1:2;
PCLKCON = 0x00;
// FSCL 0;
FSCL = 0x00;
// FSMINPER 0;
FSMINPER = 0x00;
// MPHASE 0;
MPHASE = 0x00;
// MDC 0;
MDC = 0x00;
// MPER 16;
MPER = 0x10;
// LFSR 0;
LFSR = 0x00;
// CTA1EN disabled; CTA2EN disabled; CTA3EN disabled; CTA4EN disabled;
CMBTRIGL = 0x00;
// CTB3EN disabled; CTB2EN disabled; CTB1EN disabled; CTB4EN disabled;
CMBTRIGH = 0x00;
// PWMLFA PWMS1 or PWMS2;; S1APOL Positive logic; S2APOL Positive logic; PWMLFAD No Assignment; PWMS1A PWM1H; PWMS2A PWM1H;
LOGCONA = 0x00;
// PWMLFB PWMS1 | PWMS2; S2BPOL Positive logic; PWMLFBD No Assignment; S1BPOL Positive logic; PWMS2B PWM1H; PWMS1B PWM1H;
LOGCONB = 0x00;
// PWMLFC PWMS1 | PWMS2; PWMLFCD No Assignment; S2CPOL Positive logic; S1CPOL Positive logic; PWMS1C PWM1H; PWMS2C PWM1H;
LOGCONC = 0x00;
// PWMS1D PWM1H; S1DPOL Positive logic; PWMLFD PWMS1 | PWMS2; PWMLFDD No Assignment; S2DPOL Positive logic; PWMS2D PWM1H;
LOGCOND = 0x00;
// PWMS1E PWM1H; PWMS2E PWM1H; S1EPOL Positive logic; PWMLFE PWMS1 | PWMS2; S2EPOL Positive logic; PWMLFED No Assignment;
LOGCONE = 0x00;
// S1FPOL Positive logic; PWMS2F PWM1H; PWMS1F PWM1H; S2FPOL Positive logic; PWMLFFD No Assignment; PWMLFF PWMS1 | PWMS2;
LOGCONF = 0x00;
// EVTASEL PGTRGSEL bits; EVTASYNC Not synchronized; EVTAPOL Active-high; EVTAPGS PG1; EVTASTRD Stretched to 8 PWM clock cycles minimum; EVTAOEN disabled;
PWMEVTA = 0x00;
// EVTBPGS PG1; EVTBSYNC Not synchronized; EVTBPOL Active-high; EVTBSEL PGTRGSEL bits; EVTBSTRD Stretched to 8 PWM clock cycles minimum; EVTBOEN disabled;
PWMEVTB = 0x00;
// EVTCPGS PG1; EVTCPOL Active-high; EVTCSEL PGTRGSEL bits; EVTCSTRD Stretched to 8 PWM clock cycles minimum; EVTCSYNC Not synchronized; EVTCOEN disabled;
PWMEVTC = 0x00;
// EVTDOEN disabled; EVTDSTRD Stretched to 8 PWM clock cycles minimum; EVTDPOL Active-high; EVTDPGS PG1; EVTDSEL PGTRGSEL bits; EVTDSYNC Not synchronized;
PWMEVTD = 0x00;
// EVTEOEN disabled; EVTEPOL Active-high; EVTEPGS PG1; EVTESTRD Stretched to 8 PWM clock cycles minimum; EVTESEL PGTRGSEL bits; EVTESYNC Not synchronized;
PWMEVTE = 0x00;
// EVTFPOL Active-high; EVTFPGS PG1; EVTFSTRD Stretched to 8 PWM clock cycles minimum; EVTFSEL PGTRGSEL bits; EVTFOEN disabled; EVTFSYNC Not synchronized;
PWMEVTF = 0x00;
// MSTEN disabled; TRGMOD Single trigger mode; SOCS Self-trigger; UPDMOD SOC update; MPHSEL disabled; MPERSEL disabled; MDCSEL disabled;
PG1CONH = 0x00;
// TRSET disabled; UPDREQ disabled; CLEVT disabled; TRCLR disabled; CAP disabled; SEVT disabled; FFEVT disabled; UPDATE disabled; FLTEVT disabled;
PG1STAT = 0x00;
// FLTDAT 0; DBDAT 0; SWAP disabled; OVRENH disabled; OVRENL disabled; OSYNC User output overrides are synchronized to the local PWM time base; CLMOD disabled; FFDAT 0; CLDAT 0; OVRDAT 0;
PG1IOCONL = 0x00;
// PENL enabled; DTCMPSEL PCI Sync Logic; PMOD Complementary; POLL Active-high; PENH enabled; CAPSRC Software; POLH Active-high;
PG1IOCONH = 0x0C;
// UPDTRG Manual; ADTR1PS 1:1; PGTRGSEL EOC event; ADTR1EN3 disabled; ADTR1EN1 disabled; ADTR1EN2 disabled;
PG1EVTL = 0x00;
// ADTR2EN1 disabled; IEVTSEL EOC; SIEN disabled; FFIEN disabled; ADTR1OFS None; CLIEN disabled; FLTIEN disabled; ADTR2EN2 disabled; ADTR2EN3 disabled;
PG1EVTH = 0x00;
// PSS Tied to 0; PPS Not inverted; SWTERM disabled; PSYNC disabled; TERM Manual Terminate; AQPS Not inverted; AQSS None; TSYNCDIS PWM EOC;
PG1FPCIL = 0x00;
// TQPS Not inverted; LATMOD disabled; SWPCI Drives '0'; BPEN disabled; TQSS None; SWPCIM PCI acceptance logic; BPSEL PWM Generator 1; ACP Level-sensitive;
PG1FPCIH = 0x00;
// PSS Tied to 0; PPS Not inverted; SWTERM disabled; PSYNC disabled; TERM Manual Terminate; AQPS Not inverted; AQSS None; TSYNCDIS PWM EOC;
PG1CLPCIL = 0x00;
// PCIGT disabled; TQPS Not inverted; SWPCI Drives '0'; BPEN disabled; TQSS None; SWPCIM PCI acceptance logic; BPSEL PWM Generator 1; ACP Level-sensitive;
PG1CLPCIH = 0x00;
// PSS Tied to 0; PPS Not inverted; SWTERM disabled; PSYNC disabled; TERM Manual Terminate; AQPS Not inverted; AQSS None; TSYNCDIS PWM EOC;
PG1FFPCIL = 0x00;
// PCIGT disabled; TQPS Not inverted; SWPCI Drives '0'; BPEN disabled; TQSS None; SWPCIM PCI acceptance logic; BPSEL PWM Generator 1; ACP Level-sensitive;
PG1FFPCIH = 0x00;
// PSS Tied to 0; PPS Not inverted; SWTERM disabled; PSYNC disabled; TERM Manual Terminate; AQPS Not inverted; AQSS None; TSYNCDIS PWM EOC;
PG1SPCIL = 0x00;
// PCIGT disabled; TQPS Not inverted; SWPCI Drives '0'; BPEN disabled; TQSS None; SWPCIM PCI acceptance logic; BPSEL PWM Generator 1; ACP Level-sensitive;
PG1SPCIH = 0x00;
// LEB 0;
PG1LEBL = 0x00;
// PWMPCI PWM1; PLR disabled; PLF disabled; PHR disabled; PHF disabled;
PG1LEBH = 0x00;
// PHASE 0;
PG1PHASE = 0x00;
// DC 18;
PG1DC = 0x12;
// DCA 0;
PG1DCA = 0x00;
// PER 177;
PG1PER = 0xB1;
// TRIGA 0;
PG1TRIGA = 0x00;
// TRIGB 0;
PG1TRIGB = 0x00;
// TRIGC 0;
PG1TRIGC = 0x00;
// DTL 0;
PG1DTL = 0x00;
// DTH 0;
PG1DTH = 0x00;
PG1CONLbits.ON = 1;
}
SYSTEM.C
// Configuration bits: selected in the GUI
// FSEC
#pragma config BWRP = OFF //Boot Segment Write-Protect bit->Boot Segment may be written
#pragma config BSS = DISABLED //Boot Segment Code-Protect Level bits->No Protection (other than BWRP)
#pragma config BSEN = OFF //Boot Segment Control bit->No Boot Segment
#pragma config GWRP = OFF //General Segment Write-Protect bit->General Segment may be written
#pragma config GSS = DISABLED //General Segment Code-Protect Level bits->No Protection (other than GWRP)
#pragma config CWRP = OFF //Configuration Segment Write-Protect bit->Configuration Segment may be written
#pragma config CSS = DISABLED //Configuration Segment Code-Protect Level bits->No Protection (other than CWRP)
#pragma config AIVTDIS = OFF //Alternate Interrupt Vector Table bit->Disabled AIVT
// FBSLIM
#pragma config BSLIM = 8191 //Boot Segment Flash Page Address Limit bits->8191
// FOSCSEL
#pragma config FNOSC = FRCDIVN //Oscillator Source Selection->Internal Fast RC (FRC) Oscillator with postscaler
#pragma config IESO = OFF //Two-speed Oscillator Start-up Enable bit->Start up with user-selected oscillator source
// FOSC
#pragma config POSCMD = NONE //Primary Oscillator Mode Select bits->Primary Oscillator disabled
#pragma config OSCIOFNC = OFF //OSC2 Pin Function bit->OSC2 is clock output
#pragma config FCKSM = CSECMD //Clock Switching Mode bits->Clock switching is enabled,Fail-safe Clock Monitor is disabled
#pragma config PLLKEN = ON //PLL Lock Enable->PLL clock output will be disabled if LOCK is lost
#pragma config XTCFG = G3 //XT Config->24-32 MHz crystals
#pragma config XTBST = ENABLE //XT Boost->Boost the kick-start
// FWDT
#pragma config RWDTPS = PS2147483648 //Run Mode Watchdog Timer Post Scaler select bits->1:2147483648
#pragma config RCLKSEL = LPRC //Watchdog Timer Clock Select bits->Always use LPRC
#pragma config WINDIS = OFF //Watchdog Timer Window Enable bit->Watchdog Timer in Window mode
#pragma config WDTWIN = WIN25 //Watchdog Timer Window Select bits->WDT Window is 25% of WDT period
#pragma config SWDTPS = PS2147483648 //Sleep Mode Watchdog Timer Post Scaler select bits->1:2147483648
#pragma config FWDTEN = ON_SW //Watchdog Timer Enable bit->WDT controlled via SW, use WDTCON.ON bit
// FPOR
#pragma config BISTDIS = DISABLED //Memory BIST Feature Disable->mBIST on reset feature disabled
// FICD
#pragma config ICS = PGD1 //ICD Communication Channel Select bits->Communicate on PGC1 and PGD1
#pragma config JTAGEN = OFF //JTAG Enable bit->JTAG is disabled
// FDMTIVTL
#pragma config DMTIVTL = 0 //Dead Man Timer Interval low word->0
// FDMTIVTH
#pragma config DMTIVTH = 0 //Dead Man Timer Interval high word->0
// FDMTCNTL
#pragma config DMTCNTL = 0 //Lower 16 bits of 32 bit DMT instruction count time-out value (0-0xFFFF)->0
// FDMTCNTH
#pragma config DMTCNTH = 0 //Upper 16 bits of 32 bit DMT instruction count time-out value (0-0xFFFF)->0
// FDMT
#pragma config DMTDIS = OFF //Dead Man Timer Disable bit->Dead Man Timer is Disabled and can be enabled by software
// FDEVOPT
#pragma config ALTI2C1 = OFF //Alternate I2C1 Pin bit->I2C1 mapped to SDA1/SCL1 pins
#pragma config SMB3EN = SMBUS3 //SM Bus Enable->SMBus 3.0 input levels
#pragma config SPI2PIN = PPS //SPI2 Pin Select bit->SPI2 uses I/O remap (PPS) pins
// FALTREG
#pragma config CTXT1 = OFF //Specifies Interrupt Priority Level (IPL) Associated to Alternate Working Register 1 bits->Not Assigned
#pragma config CTXT2 = OFF //Specifies Interrupt Priority Level (IPL) Associated to Alternate Working Register 2 bits->Not Assigned
#pragma config CTXT3 = OFF //Specifies Interrupt Priority Level (IPL) Associated to Alternate Working Register 3 bits->Not Assigned
#pragma config CTXT4 = OFF //Specifies Interrupt Priority Level (IPL) Associated to Alternate Working Register 4 bits->Not Assigned
#include "pin_manager.h"
#include "clock.h"
#include "system.h"
#include "system_types.h"
#include "pwm.h"
#include "interrupt_manager.h"
#include "traps.h"
void SYSTEM_Initialize(void)
{
PIN_MANAGER_Initialize();
INTERRUPT_Initialize();
CLOCK_Initialize();
PWM_Initialize();
INTERRUPT_GlobalDisable();
SYSTEM_CORCONModeOperatingSet(CORCON_MODE_PORVALUES);
}
/**
End of File
*/
main.c
#ifndef FCY
#define FCY CLOCK_InstructionFrequencyGet()
#endif
#include "mcc_generated_files/system.h"
#include "mcc_generated_files/pwm.h"
#include "mcc_generated_files/clock.h"
#include <libpic30.h>
#include <p33CK32MC102.h>
/*
Main application
*/
int main(void)
{
// initialize the device
SYSTEM_Initialize();
PWM_Initialize();
PWM_GeneratorEnable(1);
PWM_Enable();
PWM_PeriodSet(1,0xFFFF);
PWM_GeneratorEventStatusClear(1,PWM_GENERATOR_INTERRUPT_CURRENT_LIMIT);
PWM_GeneratorEventStatusClear(1,PWM_GENERATOR_INTERRUPT_FAULT);
PWM_GeneratorEventStatusClear(1,PWM_GENERATOR_INTERRUPT_FEED_FORWARD);
PWM_GeneratorEventStatusClear(1,PWM_GENERATOR_INTERRUPT_SYNC);
while (1)
{
__delay_ms(5);
PWM_DutyCycleSet(1, 75);
__delay_ms(5);
PWM_DutyCycleSet(1, 15);
}
}
/**
End of File
*/
I searched for information in the official documentation and on forums, but I was unsuccessful in any attempt. There seems to be a locked register