Sine PWM

Discussion in 'Programmer's Corner' started by FBorges22, Apr 3, 2012.

  1. FBorges22

    Thread Starter Active Member

    Sep 11, 2008
    104
    0
    Greetings,

    I am working in a project that I need to develop a SPWM Inverter for an induction motor or an UPS.

    Few days ago, I started to develop a small program that uses a sine lookup table and the PWM peripheral to generate the SPWM pulses in the PWML1 and PWMH1 pins of an dsPIC33FJ32MC202.

    The sine lookup table comparison are not yet implemented. I am currently using this code to just test the PWM pins of the dsPIC.

    However, when I simulated my code in the Proteus ISIS nothing happens and unfortunately I am quite new to the dsPICs. Here is my current code.

    Code ( (Unknown Language)):
    1. #include <p33FJ32MC202.h>
    2.  
    3. _FOSCSEL(FNOSC_FRCPLL)                // configures the fast oscillator with PLL
    4. _FOSC(FCKSM_CSECMD & OSCIOFNC_ON)    // enables only clock switching and digital I/O
    5. _FWDT(FWDTEN_OFF)                    // disables the watchdog timer
    6. _FPOR(FPWRT_PWR1)                    // disables the power on reset
    7. _FICD(ICS_PGD2 & JTAGEN_OFF)        // disables the JTAG and the ICS
    8.  
    9. #define OC5_IO    22            // define OC5 output function number for PPS
    10.  
    11. unsigned char sine[42]={0,9,19,28,38,47,57,66,75,84,93,102,111,120,128,136,144,152,160,167,174,181,188,194,200,206,211,217,221,226,230,234,238,241,244,246,249,251,252,253,254,254};
    12. unsigned char pwmFHtimes;    // PWM frequency hopping times, it is based on PWM cycle
    13.  
    14. void MCU_init(void)
    15. {
    16.     TRISB=0x00;                // PORT B as output
    17.     RCONbits.SWDTEN=0;        // Disable Watch Dog Timer
    18.     pwmFHtimes=0;
    19.     OC1CONbits.OCM=0;         // Output compare channel is disabled
    20.     OC1R= 0x0099;            // Initialize Compare Register1 with 50% duty cycle
    21.     OC1RS= 0x0099;            // Initialize Secondary Compare Register1 with 50% duty cycle
    22.     OC1CONbits.OCSIDL= 0;    // Output capture will continue to operate in CPU Idle mode
    23.     OC1CONbits.OCFLT= 0;    // No PWM Fault condition has occurred (this bit is only used when OCM<2:0> = 111)
    24.     OC1CONbits.OCTSEL= 0;    // Timer2 is the clock source for output Compare
    25.     OC1CONbits.OCM= 0x6;    // PWM mode on OC, Fault pin disabled
    26.     PR2= 0x0132;            // Initialize PR2 with 0x0132 = 0d306 as PWM cycle
    27.     IFS0bits.T2IF=0;        // Clear Output Compare interrupt flag
    28.     IEC0bits.T2IE=1;        // Enable Output Compare interrupts
    29.     T2CONbits.TON=1;        // Start Timer2 with assumed settings
    30. }
    31.  
    32. int main (void)
    33. {
    34.     while(1)
    35.     {
    36.         Idle();
    37.     }
    38.     return 0;
    39. }
    40.  
    41. void __attribute__ ((__interrupt__, no_auto_psv)) _T2Interrupt(void)
    42. {
    43.     IFS0bits.T2IF = 0;
    44.    
    45.     if (++pwmFHtimes == 5)
    46.     {    
    47.         // every 5 cycles for update
    48.         pwmFHtimes = 0;
    49.         // update the duty cycle for PWM
    50.         if (OC1RS == 0x0099)
    51.         {
    52.             OC1RS = 0x00E5;    // 75% duty cycle
    53.         }
    54.         else
    55.         {
    56.             OC1RS = 0x0099;    // 50% duty cycle
    57.         }
    58.     }
    59. }
    60.  
    Any ideas what is wrong, please? The PWMH1 and PWML2 pins should be pulsing...

    Thanks,
    FBorges22
     
  2. shortbus

    AAC Fanatic!

    Sep 30, 2009
    4,004
    1,524
    If your going to use the inverter to run a motor. What your trying to do is usually done by comparing a sine wave with a triangle wave. the result is then used to switch a pair of mosfets or IGBT's, one for the positive going and one for the negative going current. It will only approximate a sine wave. This is done because the switching devices can't make a true sine wave and live.
     
Loading...