Sunteți pe pagina 1din 2

// dsPIC30F4011 Push-pull PWM sinewave with deadtime

// Written by Ted Burke


// Last updated 12-10-2012
//
// This example generates sinusoidally modulated PWM
// waveforms on both output compare channels of a
// dsPIC30F4011. The two modulating sine waves are
// both at 25Hz frequency, but are 180 degrees out
// of phase with each other. The PWM frequency is
// 100kHz (seems high to me!). These PWM waveforms
// are intended to drive a pair of MOSFETs in a
// push-pull circuit (e.g. in a power inverter),
// so a short deadtime is included between the end
// of one channel's pulse and the beginning of the
// other channel's pulse.
//

#include <libpic30.h>
#include <p30f4011.h>
#include <math.h>
// Configuration settings
_FOSC(CSW_FSCM_OFF & XT_PLL8); // Fosc=80MHz (10MHz crystal, 8xPLL)
_FWDT(WDT_OFF); // Watchdog timer off
_FBORPOR(MCLR_DIS); // Disable reset pin

int main()
{
// Variables
long Fcy = 20000000; // Fcy = Fosc/4
double Tsin = 0.0167; // Sine wave period in seconds (40ms -> 25Hz)
int deadtime = 10; // Deadtime in instruction cycles
int Tpwm = 200; // PWM period in instruction cycles
double dc = 0.5; // duty cycle of OC1, initially 50%

// Make all port D pins outputs


TRISD = 0;

// Set OC channel 1 & 2 start and stop times

// Set output compare mode for continuous pulses


OC1CONbits.OCM = 0b101;
OC2CONbits.OCM = 0b101;
OC3CONbits.OCM = 0b101;
OC4CONbits.OCM = 0b101;
// Configure timer 2 (default timer for output compare)
PR2 = Tpwm; // 100kHz PWM frequency
T2CONbits.TON = 1; // Enable timer 2

// Move through sine wave with 100 steps


int n = 0, N=100;

// Now vary the duty cycle sinusoidally


while(1)
{
// delay for Tsin/N seconds
__delay32(Tsin * Fcy / N);

// Update duty cycle


n = n + 1;
if (n >= N) n = 0;
dc = 0.5*(1 + sin(n*6.2831853/N));
if (dc < 0) dc = 0;

// Set pulse start and stop times for both OC channels.


// It seems that OCxRS needs to be at least OCxR + 2,to prevent on
simultaneously!!
OC1R = 0;
OC1RS = 2 + dc * (Tpwm - 2 * deadtime);
OC2R = OC1RS + deadtime;
OC2RS = 2 + 2 + Tpwm - deadtime;
OC3R = 0;
OC3RS = 2 + dc * (Tpwm - 2 * deadtime);
OC4R = OC1RS + deadtime;
OC4RS = 2 + 2 + Tpwm - deadtime;
}

return 0;
}

S-ar putea să vă placă și