/***********************************
功能:固定频率spwm波发生器
MCU:dsPIC30F4011
TOOLS:MPLAB X IDE V3.15 + XC16 V1.25
震荡器:外部4M晶振
芯艺设计室 http://www.chipart.cn
2015-12-06
***********************************/
#include "xc.h"
#include <stdint.h>
#define XTFREQ 4000000 // xtal = 4Mhz;
#define PLLMODE 16 // PLLx16
#define FCY XTFREQ*PLLMODE/4 // Instruction Cycle Frequency
#define FPWM 18000 // 18 kHz, 18k/50hz=360
#include <libpic30.h>
#define LED_PORT_INIT TRISBbits.TRISB6=0
#define LED_ON LATBbits.LATB6=1
#define LED_OFF LATBbits.LATB6=0
#define LED_TOOGLE LATBbits.LATB6^=1
_FOSC(CSW_FSCM_OFF & XT_PLL16);
_FWDT(WDT_OFF);
#define DelayMs __delay_ms
unsigned int Phase; // 0..65535
const uint16_t sintable[360]={
444,452,459,467,475,483,490,498,506,513,521,529,536,544,551,559,
566,574,581,589,596,603,610,617,625,632,639,646,652,659,666,673,
679,686,692,699,705,711,717,723,729,735,741,747,752,758,763,769,
774,779,784,789,794,799,803,808,812,816,821,825,829,832,836,840,
843,846,850,853,856,859,861,864,866,869,871,873,875,877,878,880,
881,883,884,885,886,886,887,887,888,888,888,888,888,887,887,886,
886,885,884,883,881,880,878,877,875,873,871,869,866,864,861,859,
856,853,850,846,843,840,836,832,829,825,821,816,812,808,803,799,
794,789,784,779,774,769,763,758,752,747,741,735,729,723,717,711,
705,699,692,686,679,673,666,659,652,646,639,632,625,617,610,603,
596,589,581,574,566,559,551,544,536,529,521,513,506,498,490,483,
475,467,459,452,444,436,429,421,413,405,398,390,382,375,367,359,
352,344,337,329,322,314,307,299,292,285,278,271,263,256,249,242,
236,229,222,215,209,202,196,189,183,177,171,165,159,153,147,141,
136,130,125,119,114,109,104,99,94,89,85,80,76,72,67,63,
59,56,52,48,45,42,38,35,32,29,27,24,22,19,17,15,
13,11,10,8,7,5,4,3,2,2,1,1,0,0,0,0,
0,1,1,2,2,3,4,5,7,8,10,11,13,15,17,19,
22,24,27,29,32,35,38,42,45,48,52,56,59,63,67,72,
76,80,85,89,94,99,104,109,114,119,125,130,136,141,147,153,
159,165,171,177,183,189,196,202,209,215,222,229,236,242,249,256,
263,271,278,285,292,299,307,314,322,329,337,344,352,359,367,375,
382,390,398,405,413,421,429,436
};
/********************************************************************/
void SPWM( unsigned int angle )
{
uint16_t i1,i2,i3;
i1=angle;
i2=angle+120;
if(i2>=360)
i2-=360;
i3=angle+240;
if(i3>=360)
i3-=360;
PDC1=sintable[i1];
PDC2=sintable[i2];
PDC3=sintable[i3];
} // end SPWM()
void __attribute__((interrupt, auto_psv)) _PWMInterrupt (void)
{
IFS2bits.PWMIF = 0; // Clear interrupt flag
if( Phase>=360 )
Phase=0;
Phase%=360;
SPWM(Phase ); // rotate stator
Phase ++; // Increment Phase if CW to generate the
return;
}
//产生SPWM波形
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
}
//初始化电机控制模块
void InitMCPWM( void )
{
TRISE = 0x0100; // PWM pins as outputs, and FLTA as input
PTPER = (( FCY / FPWM)/2) - 1; //设置载波频率
OVDCON = 0x0000; // Disable all PWM outputs.
DTCON1 = 0x001F; // 2us 死区时间
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;
}
int main( void )
{
LED_PORT_INIT;
InitMCPWM();
Run();
while(1)
{
DelayMs(300);
LED_TOOGLE;
}
return 0;
}
|
|