各位高手,小弟用dspic30f3010写的spwm波的程序,结果波形不规范,请帮忙看一下,哪里有问题,谢谢
#include "p30fxxxx.h"
#define XTFREQ 5000000 // xtal = 5Mhz;
#define PLLMODE 16 // PLLx16
#define FCY XTFREQ*PLLMODE/4 // Instruction Cycle Frequency
// FCY 20000000 // 20 MIPS
#define FPWM 12800 // 12.8 kHz, so that no audible noise is present.
unsigned int Phase; // 0..65535
int DutyCycle; // 0..28300
void InitMCPWM();
void InitTMR1();
void Run();
const int sintable[64]={0,3212,6393,9512,12539,15446,18204,20787,23170,25329,
27245,28898,30273,31356,32137,32609,32767,32609,32137,31356,30273,28898,
27245,25329,23170,20787,18204,15446,12539,9512,6393,3212,0,-3212,-6393,
-9512,-12539,-15446,-18204,-20787,-23170,-25329,-27245,-28898,-30273,
-31356,-32137,-32609,-32767,-32609,-32137,-31356,-30273,-28898,-27245,
-25329,-23170,-20787,-18204,-15446,-12539,-9512,-6393,-3212 };
/********************************************************************/
void __attribute__((interrupt, no_auto_psv)) _PWMInterrupt (void)
{
IFS2bits.PWMIF = 0; // Clear interrupt flag
if( Phase>64 )
{
Phase=0;
}
Phase=Phase%64;
SPWM( DutyCycle, Phase ); // rotate stator
Phase ++; // Increment Phase if CW to generate the
return;
}
/********************************************************************/
int main( void )
{
InitMCPWM();
CORCONbits.PSV = 1;
DutyCycle = 1; // <------- set duty cycle
Phase = 0;
SPWM( DutyCycle, Phase ); // rotate stator field and wait rotor response
Run();
while( 1 ) /* or use !SWITCH_S2 here for S2 to stop test */
{
}
return 0;
}
void Run void )
{
unsigned int i;
OVDCON = 0x0015; // charge bootstrap cap
for( i = 0; i < 40000; i++ ) Nop();
PWMCON2bits.UDIS = 1; // PWM update lock
// PDC1 = PDC2 = PDC3 = PTPER; // Initialize as 0 voltage
OVDCON = 0x3F00; // Configure PWM0-5 to be governed by PWM module
PWMCON2bits.UDIS = 0; // PWM update unlock
IEC2bits.PWMIE = 1; // Enable PWM interrupts
return;
}
void InitMCPWM( void )
{
TRISE = 0x0100; // PWM pins as outputs, and FLTA as input
PTPER = ( FCY / FPWM - 1 ) >> 1; // Compute Period based on CPU speed and
// required PWM frequency (see defines)
OVDCON = 0x0000; // Disable all PWM outputs.
DTCON1 = 0x001F; // 2us of dead time
PWMCON1 = 0x0077; // Enable PWM output pins and configure them as
// complementary mode
IEC2bits.PWMIE = 0; // Disable PWM interrupts
PDC1 = PTPER; // Initialize as 0 voltage
PDC2 = PTPER; // Initialize as 0 voltage
PDC3 = PTPER; // Initialize as 0 voltage
// PWMCON2 = 0x0F02; // 16 postscale values, for achieving 20 kHz
PWMCON2 = 0x0202;
PTCON = 0x8002; // start PWM as center aligned mode
FLTACON = 0x0000; // Initialize Fault Controls
return;
}
void SPWM( int volts, unsigned int angle )
{
unsigned int angle1, angle2, angle3;
int sin_1, sin_2, sin_3;
angle1=angle;
angle2=(angle1+21)%64;
angle3=(angle2+21)%64;
sin_1 = volts * sintable[angle1]>>6;
sin_2 = volts * sintable[angle2]>>6;
sin_3 = volts * sintable[angle3]>>6;
// 1
if( sin_1<0 )
{
sin_1 = sin_1+PTPER;
}
if(sin_1 >= 780)
{
sin_1 = 780; //限幅
}
// 2
if( sin_2<0 )
{
sin_2=sin_2+PTPER;
}
if(sin_2 >= 780)
{
sin_2 = 780; //限幅
}
// 3
if( sin_3<0 )
{
sin_3=sin_3+PTPER;
}
if(sin_3 >= 780)
{
sin_3 = 780; //限幅
}
PDC1=sin_1;
PDC2=sin_2;
PDC3=sin_3;
} // end SPWM()
500) {this.resized=true; this.width=500; this.alt='这是一张缩略图,点击可放大。\n按住CTRL,滚动鼠标滚轮可自由缩放';this.style.cursor='hand'}" onclick="if(!this.resized) {return true;} else {window.open('http://u.dianyuan.com/bbs/u/78/579731245760030.jpg?x-oss-process=image/watermark,g_center,image_YXJ0aWNsZS9wdWJsaWMvd2F0ZXJtYXJrLnBuZz94LW9zcy1wcm9jZXNzPWltYWdlL3Jlc2l6ZSxQXzQwCg,t_20');}" onmousewheel="return imgzoom(this);">500) {this.resized=true; this.width=500; this.alt='这是一张缩略图,点击可放大。\n按住CTRL,滚动鼠标滚轮可自由缩放';this.style.cursor='hand'}" onclick="if(!this.resized) {return true;} else {window.open('http://u.dianyuan.com/bbs/u/78/579731245760038.jpg?x-oss-process=image/watermark,g_center,image_YXJ0aWNsZS9wdWJsaWMvd2F0ZXJtYXJrLnBuZz94LW9zcy1wcm9jZXNzPWltYWdlL3Jlc2l6ZSxQXzQwCg,t_20');}" onmousewheel="return imgzoom(this);">