標(biāo)題:
DSP2812產(chǎn)生三相SPWM波程序
[打印本頁(yè)]
作者:
kobe2488
時(shí)間:
2019-6-27 15:54
標(biāo)題:
DSP2812產(chǎn)生三相SPWM波程序
DSP2812產(chǎn)生三相SPWM波,有需要的拿走
單片機(jī)源程序如下:
/*
本程序在220V 3400轉(zhuǎn) 5對(duì)級(jí)直流無刷電機(jī)調(diào)試下通過
*/
#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "math.h"
#define C_TIME 20
#define pwm_half_per 3750 /*3750 pwm=20khz*//*sytemclk=150MHz*/
/*1875 pwm=40kHz*/
#define pole 2 /*電機(jī)的級(jí)對(duì)數(shù)*/
#define timer2_per 60000//234375/* timer2 period with a 1/128 timer prescaler and 150MHz CPUCLK*/
#define PI 3.1415926
unsigned int adc_res;
unsigned int dir=1;
unsigned int ldd=0;
unsigned int PWM_DUTY=2000;
unsigned int I_result[2048];
unsigned int i=0;
unsigned int hall=0x000;
unsigned int time_cnt=C_TIME;
unsigned int hallspeed=0;
unsigned int prehall=0;
unsigned int speed;
unsigned int a=0,b=0,c=0,d=0;
unsigned int displaytime=0;
unsigned int displayflag=0;
unsigned int fc=10000;
unsigned int f=50;
float M=0.9;
unsigned int k=0;
// Prototype statements for functions found within this file.
void init_eva(void);
void eva_timer1_isr(void);
//void init_evb(void);
// Global counts used in this example
void main(void)
{
// Step 1. Initialize System Control:
// PLL, WatchDog, enable Peripheral Clocks
// This example function is found in the DSP281x_SysCtrl.c file.
InitSysCtrl();
// Step 2. Initalize GPIO:
// This example function is found in the DSP281x_Gpio.c file and
// illustrates how to set the GPIO to it's default state.
// InitGpio(); // Skipped for this example
// Initialize only GPAMUX and GPBMUX for this test
EALLOW;
// Enable PWM pins
GpioMuxRegs.GPAMUX.all = 0x003F; // EVA PWM 1-6 pins
GpioMuxRegs.GPAQUAL.all=0x0000;
EDIS;
// Step 3. Clear all interrupts and initialize PIE vector table:
// Disable CPU interrupts
DINT;
// Initialize PIE control registers to their default state.
// The default state is all PIE interrupts disabled and flags
// are cleared.
// This function is found in the DSP281x_PieCtrl.c file.
InitPieCtrl();
IER = 0x0000;
IFR = 0x0000;
// Interrupts that are used in this example are re-mapped to
// ISR functions found within this file.
EALLOW; // This is needed to write to EALLOW protected registers
PieVectTable.T1PINT = &eva_timer1_isr;
EDIS;
// Disable CPU interrupts and clear all CPU interrupt flags:
// Initialize the PIE vector table with pointers to the shell Interrupt
// Service Routines (ISR).
// This will populate the entire table, even if the interrupt
// is not used in this example. This is useful for debug purposes.
// The shell ISR routines are found in DSP281x_DefaultIsr.c.
// This function is found in DSP281x_PieVect.c.
InitPieVectTable();
EvaRegs.T1CON.all=0x0000;//disable time1
// Step 4. Initialize all the Device Peripherals:
// This function is found in DSP281x_InitPeripherals.c
// InitPeripherals(); // Not required for this example
init_eva();
// init_evb();
// Step 5. User specific code, enable interrupts:
// Enable PIE group 2 interrupt 4 for T1PINT
PieCtrlRegs.PIEIER2.all = M_INT4;
// Enable CPU INT2 for T1PINT, INT3 for T2PINT, INT4 for T3PINT
// and INT5 for T4PINT:
IER |= M_INT2;
// Enable global Interrupts and higher priority real-time debug events:
EINT; // Enable Global interrupt INTM
ERTM; // Enable Global realtime interrupt DBGM
EvaRegs.EVAIMRA.bit.T1PINT = 1;
EvaRegs.EVAIFRA.bit.T1PINT = 1;
// Just sit and loop forever:
// PWM pins can be observed with a scope.
for(;;)
{
}
}
void init_eva()
{
// EVA Configure T1PWM, T2PWM, PWM1-PWM6
// Initalize the timers
EvaRegs.GPTCONA.bit.TCMPOE = 1;//0
EvaRegs.GPTCONA.bit.T1PIN = 2;//0
// Initalize EVA Timer1
//EvaRegs.T1PR=0.0001;
EvaRegs.T1PR = pwm_half_per; // Timer1 period
EvaRegs.T1CMPR=0;
//EvaRegs.T1CMPR = PWM_DUTY; // Timer1 compare
EvaRegs.T1CNT = 0x0000; // Timer1 counter
EvaRegs.DBTCONA.all=0x0000; //deadband units off
EvaRegs.ACTRA.all =0x0fff; //pwm pin set active high*/
// Enable compare for PWM1-PWM6
EvaRegs.CMPR1=0;
//EvaRegs.CMPR1=PWM_DUTY;
EvaRegs.CMPR2=PWM_DUTY;
EvaRegs.CMPR3=PWM_DUTY;
EvaRegs.COMCONA.all = 0xA600;
EvaRegs.T1CON.all = 0x842;//0x840
// Compare action control. Action that takes place
// on a cmpare event
// output pin 1 CMPR1 - active high
// output pin 2 CMPR1 - active low
// output pin 3 CMPR2 - active high
// output pin 4 CMPR2 - active low
// output pin 5 CMPR3 - active high
// output pin 6 CMPR3 - active low
EvaRegs.DBTCONA.all = 0x1000; // Disable deadband
}
void eva_timer1_isr(void)
{
EvaRegs.EVAIMRA.bit.T1PINT = 1;
EvaRegs.EVAIFRA.all = BIT7;
EvaRegs.ACTRA.all =0x0666;//0x0c02
//EvaRegs.CMPR3=pwm_half_per-200;
//EvaRegs.CMPR1=PWM_DUTY;
//EvaRegs.CMPR2=PWM_DUTY;
EvaRegs.CMPR1=(pwm_half_per/2)*(1+0.9*sin(PI*k/100));
EvaRegs.CMPR2=(pwm_half_per/2)*(1+0.9*sin(PI*k/100+2*PI/3));
EvaRegs.CMPR3=(pwm_half_per/2)*(1+0.9*sin(PI*k/100+4*PI/3));
k++;
if(k==200)
{
k=0;
}
// EvaRegs.CMPR1=PWM_DUTY;
// EvaRegs.CMPR2=PWM_DUTY;
// EvaRegs.CMPR3=PWM_DUTY;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP2;
}
復(fù)制代碼
所有資料51hei提供下載:
75448169dsp2812spwm.zip
(422.74 KB, 下載次數(shù): 63)
2019-6-27 15:53 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1