Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
84 views

Code

This program initializes PWM modules 1 and 2 on a microcontroller to generate sine waves using lookup table (LUT) values. It configures the PWM time base, duty cycles, phases, triggers, and enables an interrupt on PWM1 match using values pre-calculated and loaded from an Excel LUT. The main routine sets up ports and clocks, initializes PWM, and enables the PWM and interrupt modules. The PWM interrupt service routine simply clears the interrupt flag and sets a variable.

Uploaded by

sagarscorpion
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
84 views

Code

This program initializes PWM modules 1 and 2 on a microcontroller to generate sine waves using lookup table (LUT) values. It configures the PWM time base, duty cycles, phases, triggers, and enables an interrupt on PWM1 match using values pre-calculated and loaded from an Excel LUT. The main routine sets up ports and clocks, initializes PWM, and enables the PWM and interrupt modules. The PWM interrupt service routine simply clears the interrupt flag and sets a variable.

Uploaded by

sagarscorpion
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

/***************************************************************************** Program: For Sine-Const using PWMSp Event Match Interrupt interrupt suing 400 L UT values.

All calcultions done outside the program and then loaded from the LUT excel. ******************************************************************************/ #include "p33fj16gs504.h" /* Configuration Bit Settings */ _FOSCSEL(FNOSC_FRCPLL) _FOSC(OSCIOFNC_OFF & POSCMD_NONE) _FWDT(FWDTEN_OFF) _FPOR(FPWRT_PWR128) _FICD(ICS_PGD2 & JTAGEN_OFF) int i,a; void __attribute__((__interrupt__ , no_auto_psv)) _PWM1Interrupt(void); void init_PWM(); /******************************************************************************/ /* MAIN ROUTINE */ /******************************************************************************/ int main(void) { TRISAbits.TRISA4 = 0; /* Set RA4 as a digital output */ TRISAbits.TRISA3 = 0; /* Set RA3 as a digital output */ TRISBbits.TRISB13 = 0; /* Set RB13 as a digital output */ TRISBbits.TRISB14 = 0; /* Set RB14 as a digital output */ TRISBbits.TRISB11 = 0; /* Set RB11 as a digital output */ TRISBbits.TRISB12 = 0; /* Set RB12 as a digital output */ // // // // // // LATEbits.LATE0 = 0; LATEbits.LATE1 = 0; LATEbits.LATE2 = 0; LATEbits.LATE3 = 0; LATEbits.LATE4 = 0; LATEbits.LATE5 = 0; /* FRC provides /* Auxiliary Osc /* Divide Auxili /* Enable Auxili

ACLKCONbits.FRCSEL = 1; input for Auxiliary PLL (x16) */ ACLKCONbits.SELACLK = 1; illator provides clock source for PWM & ADC */ ACLKCONbits.APSTSCLR = 7; ary clock by 1 */ ACLKCONbits.ENAPLL = 1; ary PLL */ init_PWM(); /* PWM Time Base Control Register */ PTCONbits.PTEN = 1; /* Enable the PWM Module */ while(1); } /* Infinite Loop */

/*****************************************************************************/ /* STANDARD PWM INITIALIZATION CODE */ /*****************************************************************************/ void init_PWM() { PTCON = 0x0C00; /* PWM module is disabled. Operating in idle mo de,Period Register updated immediately */ PTCON2 = 0x0000; /*~~~~~~~~~~~~~~~~~~~~~~~ PWM1 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* PWM1 I/O Control IOCON1bits.PENH IOCON1bits.PENL IOCON1bits.PMOD IOCON1bits.POLH Register register */ = 1; /* PWM1H is controlled by PWM module */ = 1; /* PWM1L is controlled by PWM module */ = 0; /* Select Independent Output PWM mode */ = 0; /*PWM1H is high active*/

/* Set PWM1 Control register */ PWMCON1 = 0x0000; /* Fault interrupt disabled,current limit interrupt diabled,trigger interrupt enabled */ /* Primary time base provides timing,DC1 provides duty cycle,negative dead time,PDC update immediate */ PDC1= 0x3E80; /*initialize PDC1,50% duty*/

/* Load PDTR1 and ALTDTR2 register with preset dead time value */ DTR1 = 0x0000; /* Deadtime setting */ ALTDTR1 = 0x0000; /* Deadtime setting */ /* PHASE1 register */ PHASE1 = 0x0000; /* 90* phase shift */

/* Current Limit and Fault Inputs */ FCLCON1 =0x0003; /* Disable current limit and faults inputs */ /* PWM trigger control */ TRGCON1 =0x0000; /* Trigger output for evry trigger event */ /* PWM Trigger value compare register */ TRIG1= 0x0008; /*~~~~~~~~~~~~~~~~~~~~~~~ PWM2 Configuration ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* PWM2 I/O Control Register register */ IOCON2bits.PENH = 1; /* PWM2H is controlled by PWM module */ IOCON2bits.PENL = 1; /* PWM2L is controlled by PWM module */ IOCON2bits.PMOD = 0; /* Select Independent Output PWM mode */ IOCON2bits.POLH = 0; /*PWM1H is high active*/ /* Set PWM2 Control register */ PWMCON2 = 0x0000; /* Fault interrupt disabled,current limit interrupt diabled,trigger interrupt disabled */ /* Primary time base provides timing,DC1 provides duty cycle,negative dead time,PDC update enbaled */ PDC2= 0x3E80; /*Initialize PDC2,50% duty*/

/* Load PDTR2 and ALTDTR2 register with preset dead time value */

DTR2 = 0x0000; ALTDTR2 = 0x0000; /* PHASE2 register */ PHASE2 = 0;

/* Deadtime setting */ /* Deadtime setting */ /* No phase shift */

/* Current Limit and Fault Inputs */ FCLCON2 =0x0003; /* Disable current limit and faults inputs */ /* PWM trigger control */ TRGCON2 =0x0000; /* Trigger output for evry trigger event */ /* PWM Trigger value compare register */ TRIG2= 0x0008; /*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ /* Interrupt configuration */ IFS5bits.PWM1IF=0; IFS5bits.PWM2IF=0; IFS3bits.PSEMIF=0; IEC5bits.PWM1IE=0; IEC5bits.PWM2IE=0; IEC3bits.PSEMIE=0; /* Clear PWM1 interrupt flag */ /* Clear PWM2 interrupt flag */ /* Clear PSEM interrupt flag */ /* PWM1 Interrupt request disabled */ /* PWM2 Interrupt request disabled */ /* PSEM Interrupt request enabled */

/* Configure PTPER register to produce 20kHz PWM frequency */ PTPER = 0x7D00; /* PWM Period @30 MIPS, 20kHz */ /*Configure Special Event trigger */ SEVTCMP = 0x7D00; /* Special Event interrupt generated at PTMR re set */ } void __attribute__((__interrupt__ , no_auto_psv)) _PWM1Interrupt(void) { IFS5bits.PWM1IF = 0; a=3; }

You might also like