dspic33ev sine pwm

Discussion in 'Embedded Systems and Microcontrollers' started by dandrous, Oct 5, 2016.

  1. dandrous

    Thread Starter New Member

    Mar 2, 2015
    22
    0
    hi.I need sine modulated pwm output ,iam able to get the pwm,but not able to get sine modulated pwm
    Code (Text):
    1. //////////program.////////
    2. #include <stdio.h>
    3. #include <stdlib.h>
    4. #include "newfile.h"
    5.  
    6. #define FCY 20000000 // 20 MIPS
    7. #define FPWM 20000 // 20 kHz
    8. #define _DES_FREQ 60
    9.  
    10.  
    11.  
    12. int i=0;
    13. signed long sinetable[64]={0, 3263, 6493, 9658, 12728, 15671, 18458, 21062,
    14. 23457, 25618, 27525, 29158, 30502, 31542, 32269, 32675, 32757, 32513,
    15. 31945, 31061, 29867, 28377, 26605, 24568, 22287, 19785, 17086, 14217,
    16. 11207, 8085, 4884, 1633, -1633, -4884, -8085, -11207, -14217, -17086,
    17. -19785, -22287, -24568, -26605, -28377, -29867, -31061, -31945, -32513,
    18. -32757, -32675, -32269, -31542, -30502, -29158, -27525, -25618, -23457,
    19. -21062, -18458, -15671, -12728, -9658, -6493, -3263, 0};
    20. void PWM_Init() {
    21.    
    22.     PTCON = 0x0000; //Reset configuration register
    23.     PTCON2 = 0x0000;
    24.  
    25.     PTCON2bits.PCLKDIV = 0b110; //Set divide for PWM generator to 1:64
    26.    
    27.    PTPER = (FCY/FPWM - 1) >> 1;
    28.  
    29.  
    30.  
    31.     MDC = sinetable[i];
    32.     ++i;
    33.     if(i==64)
    34.     {
    35.         i=0;
    36.     }
    37.  
    38.     PWMCON1 = 0x0000; //Reset configuration register
    39.     PWMCON2 = 0x0000;
    40.    
    41.  
    42.  
    43.     PWMCON1bits.MDCS = PWMCON2bits.MDCS = PWMCON3bits.MDCS = 1;
    44.    
    45.     //MDC provide duty-cycle for PWM generator
    46.    
    47.     PWMCON1bits.DTC = PWMCON2bits.DTC = PWMCON3bits.DTC=0b10; //Death-time disable
    48.     PWMCON1bits.CAM = PWMCON2bits.CAM = PWMCON3bits.CAM = 1;; //Center aligned enable
    49.  
    50.     TRGCON1 = 0x0000; //Reset configuration register
    51.     TRGCON2 = 0x0000;
    52.  
    53.     IOCON1 = 0x0000;
    54.     IOCON1bits.PENH = 1; //PWM module control PWM-H pins
    55.     IOCON1bits.PENL = 1;
    56.     IOCON1bits.PMOD = 0b00; //complimentary output
    57.  
    58.     IOCON2 = 0x0000;
    59.     IOCON2bits.PENH = 1;
    60.     IOCON2bits.PENL = 1;
    61.     IOCON2bits.PMOD = 0b00;
    62.    
    63.     IOCON3 = 0x0000;
    64.     IOCON3bits.PENH = 1;
    65.     IOCON3bits.PENL = 1;
    66.     IOCON3bits.PMOD = 0b00;
    67.  
    68.     FCLCON1 = 0x0000; //Reset configuration register
    69.     FCLCON1bits.FLTMOD = 0b11; //Fault input disable
    70.  
    71.     PTCONbits.PTEN = 1; //Enable PWM generator
    72.  
    73. }
    74. int main()
    75. {
    76. PWM_Init();
    77.  
    78. while(1); /* Infinite Loop */
    79. }
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    362
    Start with something simple: can you generate a pulse train with fixed duty cycle?

    Once there, generate a pulse train with variable duty cycle.

    And then a pulse train with a duty cycle pattern that gives you the sine wave.

    You aare trying to run before you can walk.
     
  3. dandrous

    Thread Starter New Member

    Mar 2, 2015
    22
    0
    im getting the pulse train,but im not able to get the sine modulated pwm
     
  4. jayanthd

    Member

    Jul 4, 2015
    274
    29
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,819
    362
    Simple then: set duty cycle, wait a fixed period, set duty cycle to the next value, wait a fixed period, set duty cycle to the next value, .... So on and so forth.

    It is so simple that even as caveman can do it.
     
Loading...