搜档网
当前位置:搜档网 › DSP2812中的SPWM程序

DSP2812中的SPWM程序

#include "DSP281x_Device.h" // DSP281x Headerfile Include File
#include "DSP281x_Examples.h" // DSP281x Examples Include File
#include "float.h"
#include "math.h"
volatile unsigned int *Dataram_copy, *Dataram=(unsigned int*)0x100000;//RAM 起始地址为0X100000
volatile double sindata,angel,quzheng; /*k代表角度*/
volatile unsigned int dotval1,half_period,currfreq,currampl,quad_period,flag,sinzh;
volatile unsigned long numdiv;
volatile unsigned long i,angel_step=0;

//Gpio 初始化
void Init_Gpio(void)
{EALLOW;
GpioMuxRegs.GPAMUX.all=0x0003;//pwm0 1
GpioMuxRegs.GPADIR.all=0x0003;//OUT
GpioMuxRegs.GPDMUX.all=0x0000;//state
GpioMuxRegs.GPADIR.all=0x0000;
EDIS;
}
//EVA 初始化
void Init_EVA(void)
{/*T1 time 比较输出*/
EvaRegs.GPTCONA.all=0xe065;
EvaRegs.T1CNT=0;
EvaRegs.T1PR=4095;
EvaRegs.ACTRA.all=0x0556;//比较方式控制
EvaRegs.DBTCONA.all=0x0f35;//死区1us
EvaRegs.CMPR1=2048;
https://www.sodocs.net/doc/7f18555952.html,CONA.all=0x4b00;
https://www.sodocs.net/doc/7f18555952.html,CONA.all=0xcb00;//使能比较

//EvaRegs.T1CON.all=0x104a;
/*T4 time 周期采样*/
EvbRegs.GPTCONB.all=0x0005;
EvbRegs.T4PR=299;
EvbRegs.T4CNT=0;
EvbRegs.EVBIFRB.all=0x00ff;//清除标志
//EvbRegs.T4CON.all=0x1042;//start T4
}


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();
EALLOW;
SysCtrlRegs.HISPCP.all = 0x0000;
SysCtrlRegs.PCLKCR.bit.EVAENCLK=1;
EDIS;

// 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.
Init_Gpio(); // Skipped for this example

// 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();

// Disable CPU interrupts and clear all CPU interrupt flags:
IER = 0x0000;
IFR = 0x0000;

// 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();

for(i=0;i<=250000;i++)// Dataram<(Uint16*)0x107a13)//256K
{angel=90.0/250000;//四分之一周期
angel=angel*angel_step;
angel=3.1415926*angel;
angel=angel/180;/*数据类型优先级,sin弧度计算,弧度值=角度值*3.1415926/180*/
sindata=sin(angel);
sindata=sindata*1024;
sindata=sindata+1024;
sindata=sindata*32;
sinzh=sindata;
quzheng=sindata-sinzh;
if(quzheng>=0.5) //值为

负,加1变正
sindata=sinzh+1;
else
sindata=sinzh;

if(sindata>=65536)
{*Dataram++=65535;
angel_step++;
}
else
{*Dataram++=sindata;
angel_step++;
}
}
Init_EVA();
EvbRegs.T4CON.all=0x1042;//start T4
EvaRegs.T1CON.all=0x1048;
asm(" clrc intm");

//PWM循环输出
Dataram=(unsigned int*)0x100000;
dotval1=32768;
half_period=0; //0为起点,1则过半周期
currfreq=400;
currampl=600;
quad_period=0;// 0为起点,1则过1/4周期
flag=1;//1:正前1/4周期;2:正后1/4周期; 3:负前1/4周期 ;4:负后1/4周期
CALVIB:
//EvaRegs.CMPR1=0x0;
switch(flag)

{case 1:quad_period=0;half_period=0;break; //没过1/4周期,没过1/2周期
case 2:quad_period=1;half_period=0;break; //过1/4周期,没过1/2周期
case 3:quad_period=0;half_period=1;break; //没过3/4周期,过1/2周期
case 4:quad_period=1;half_period=1;break; //过3/4周期,过1/2周期
default: quad_period=0;half_period=0;break;
}

dotval1=*Dataram;
dotval1=dotval1>>4; //右移1位除以2,右移n位,除以2的n次方
if(dotval1<0x800)//最小保护
dotval1=0x800;
dotval1=dotval1-0x800;
numdiv=currampl*dotval1;
dotval1=numdiv>>10;
if(dotval1>0x7ff)//最大保护
dotval1=0x7ff;
dotval1=dotval1+0x800; //和T1PR=2048比较输出
if(half_period!=0)
{ dotval1=0x1000-dotval1;
dotval1++;
}
WAITT4:
if(!(EvbRegs.EVBIFRB.all&0x1))
goto WAITT4;
EvaRegs.CMPR1=dotval1;

if(quad_period==1)
Dataram=Dataram-currfreq;

else
Dataram=Dataram+currfreq;

Dataram_copy=Dataram;

EvbRegs.EVBIMRB.all=0x0; //禁止周期中断


if((Dataram>=(unsigned int*)0x13d090)&&(half_period==0)&&(quad_period==0))//半周期,250000=224K
{flag=2;//过1/4周期
Dataram=(unsigned int*)0x13d090;
}
if((Dataram<=(unsigned int*)0x100000)&&(half_period==0)&&(quad_period==1))
{flag=3;
Dataram=(unsigned int*)0x100000;
}
if((Dataram>=(unsigned int*)0x13d090)&&(half_period==1)&&(quad_period==0))
{flag=4;
Dataram=(unsigned int*)0x13d090;
}
if((Dataram<=(unsigned int*)0x100000)&&(quad_period==1)&&(half_period==1))
{flag=1;
Dataram=(unsigned int*)0x100000;
}


//一点结束

EvbRegs.EVBIFRB.all=0x0ff;
EvbRegs.EVBIMRB.all=0x0ff;//start
goto CALVIB;
}
//===========================================================================
// No more.

//=====

相关主题