eto po yung code ko para sa paginitialize ng timer at pwmmc
#include <p30f4011.h>
_FOSC(CSW_FSCM_OFF & XT);
_FWDT( WDT_OFF );
_FBORPOR( PBOR_OFF & BORV_45 & MCLR_EN );
_FGS( CODE_PROT_OFF );
#define Fosc 8000000
#define Fcy Fosc/4
#define Fpwm 640
#define Deadtime 2
//pwmmc variables
int index1 = 0, index2 = 11, index3 = 21;
static float sine_table[32] = { 1.00,0.80,0.62,0.44,0.30,0.16,0.08,0.02,
0.00,0.02,0.08,0.16,0.30,0.44,0.62,0.80,
1.00,1.20,1.38,1.56,1.70,1.84,1.92,1.98,
2.00,1.98,1.92,1.84,1.70,1.56,1.38,1.20 };
void __attribute__((__interrupt__)) _PWMInterrupt( void ){
//increment indeces
index1++;
index2++;
index3++;
//reset indeces
if(index1 == 32) index1 = 0;
if(index2 == 32) index2 = 0;
if(index3 == 32) index3 = 0;
//update PWM duty cycles
PDC1 = (sine_table[index1]*PTPER);
PDC2 = (sine_table[index2]*PTPER);
PDC3 = (sine_table[index3]*PTPER);
IFS2bits.PWMIF = 0;
}
void pwmmc_init(void){
TRISE = 0x0100; // PWM pins as outputs, and FLTA as input
PTPER = (Fcy/Fpwm - 1) >> 1; // Compute Period for desired frequency
OVDCON = 0x0000; // Disable all PWM outputs.
DTCON1 = Deadtime; // ~2 us of dead time @ 20 MIPS and 1:1 Prescaler
PWMCON1 = 0x0077; // Enable PWM output pins and enable complementary mode
PDC1 = (sine_table[index1]*PTPER);
PDC2 = (sine_table[index2]*PTPER);
PDC3 = (sine_table[index3]*PTPER);
IFS2bits.PWMIF = 0; // Clear PWM Interrupt flag
IEC2bits.PWMIE = 1; // Enable PWM Interrupts
OVDCON = 0x3F00; // PWM outputs are controller by PWM module
PTCONbits.PTMOD = 2; // Center aligned PWM operation
PTCONbits.PTEN = 1; // Start PWM
}
void __attribute__((__interrupt__)) _T1Interrupt( void ){
LATBbits.LATB3 = ~LATBbits.LATB3;
IFS0bits.T1IF = 0; // Clear timer 1 interrupt flag
}
void timer1_init(void){
TMR1 = 0; // Reset timer counter
T1CONbits.TON = 0; // Turn off timer 1
T1CONbits.TSIDL = 0; // Continue operation during sleep
T1CONbits.TGATE = 0; // Gated timer accumulation disabled
T1CONbits.TCS = 0; // use Tcy as source clock
T1CONbits.TCKPS = 0;
PR1 = 500;
IFS0bits.T1IF = 0; // Clear timer 1 interrupt flag
IEC0bits.T1IE = 1; // Enable timer 1 interrupts
T1CONbits.TON = 1; // Turn on timer 1
}
int main(void){
pwmmc_init();
ADPCFG = 0xFFFF;
TRISBbits.TRISB3 = 0;
LATBbits.LATB3 = 0;
timer1_init();
while(1);
return 0;
}