dspic33ev sine pwm

Thread Starter

dandrous

Joined Mar 2, 2015
22
hi.I need sine modulated pwm output ,iam able to get the pwm,but not able to get sine modulated pwm
Code:
//////////program.////////
#include <stdio.h>
#include <stdlib.h>
#include "newfile.h"

#define FCY 20000000 // 20 MIPS
#define FPWM 20000 // 20 kHz
#define _DES_FREQ 60



int i=0;
signed long sinetable[64]={0, 3263, 6493, 9658, 12728, 15671, 18458, 21062,
23457, 25618, 27525, 29158, 30502, 31542, 32269, 32675, 32757, 32513,
31945, 31061, 29867, 28377, 26605, 24568, 22287, 19785, 17086, 14217,
11207, 8085, 4884, 1633, -1633, -4884, -8085, -11207, -14217, -17086,
-19785, -22287, -24568, -26605, -28377, -29867, -31061, -31945, -32513,
-32757, -32675, -32269, -31542, -30502, -29158, -27525, -25618, -23457,
-21062, -18458, -15671, -12728, -9658, -6493, -3263, 0};
void PWM_Init() {
   
    PTCON = 0x0000; //Reset configuration register
    PTCON2 = 0x0000;

    PTCON2bits.PCLKDIV = 0b110; //Set divide for PWM generator to 1:64
   
   PTPER = (FCY/FPWM - 1) >> 1;
 
  
  
    MDC = sinetable[i];
    ++i;
    if(i==64)
    {
        i=0;
    }

    PWMCON1 = 0x0000; //Reset configuration register
    PWMCON2 = 0x0000;
   
  
  
    PWMCON1bits.MDCS = PWMCON2bits.MDCS = PWMCON3bits.MDCS = 1;
   
    //MDC provide duty-cycle for PWM generator
   
    PWMCON1bits.DTC = PWMCON2bits.DTC = PWMCON3bits.DTC=0b10; //Death-time disable
    PWMCON1bits.CAM = PWMCON2bits.CAM = PWMCON3bits.CAM = 1;; //Center aligned enable

    TRGCON1 = 0x0000; //Reset configuration register
    TRGCON2 = 0x0000;

    IOCON1 = 0x0000;
    IOCON1bits.PENH = 1; //PWM module control PWM-H pins
    IOCON1bits.PENL = 1;
    IOCON1bits.PMOD = 0b00; //complimentary output

    IOCON2 = 0x0000;
    IOCON2bits.PENH = 1;
    IOCON2bits.PENL = 1;
    IOCON2bits.PMOD = 0b00;
   
    IOCON3 = 0x0000;
    IOCON3bits.PENH = 1;
    IOCON3bits.PENL = 1;
    IOCON3bits.PMOD = 0b00;

    FCLCON1 = 0x0000; //Reset configuration register
    FCLCON1bits.FLTMOD = 0b11; //Fault input disable
  
    PTCONbits.PTEN = 1; //Enable PWM generator
 
}
int main()
{
PWM_Init();

while(1); /* Infinite Loop */
}
 

dannyf

Joined Sep 13, 2015
2,197
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.
 

Thread Starter

dandrous

Joined Mar 2, 2015
22
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.
im getting the pulse train,but im not able to get the sine modulated pwm
 

dannyf

Joined Sep 13, 2015
2,197
im getting the pulse train,but im not able to get the sine modulated pw
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.
 
Top