基于DSP的SPWM程序.docx
《基于DSP的SPWM程序.docx》由会员分享,可在线阅读,更多相关《基于DSP的SPWM程序.docx(16页珍藏版)》请在冰豆网上搜索。
![基于DSP的SPWM程序.docx](https://file1.bdocx.com/fileroot1/2023-2/4/5c7369f0-d921-4910-833e-7b372e001b48/5c7369f0-d921-4910-833e-7b372e001b481.gif)
基于DSP的SPWM程序
#include"DSP280x_Device.h"//DSP280xHeaderfileIncludeFile
#include"DSP280x_Examples.h"//DSP280xExamplesIncludeFile
typedefstruct
{
volatilestructEPWM_REGS*EPwmRegHandle;
Uint16EPwm_CMPA_Direction;
Uint16EPwm_CMPB_Direction;
Uint16EPwmTimerIntCount;
Uint16EPwmMaxCMPA;
Uint16EPwmMinCMPA;
Uint16EPwmMaxCMPB;
Uint16EPwmMinCMPB;
}EPWM_INFO;
//Prototypestatementsforfunctionsfoundwithinthisfile.
voidInitEPwm1Example(void);
voidInitEPwm2Example(void);
voidInitEPwm3Example(void);
interruptvoidepwm1_isr(void);
interruptvoidepwm2_isr(void);
interruptvoidepwm3_isr(void);
voidupdate_compare(EPWM_INFO*);
//Globalvariablesusedinthisexample
EPWM_INFOepwm1_info;
EPWM_INFOepwm2_info;
EPWM_INFOepwm3_info;
//Configuretheperiodforeachtimer
#defineEPWM1_TIMER_TBPRD2000//Periodregister
#defineEPWM1_MAX_CMPA1950
#defineEPWM1_MIN_CMPA50
#defineEPWM1_MAX_CMPB1950
#defineEPWM1_MIN_CMPB50
#defineEPWM2_TIMER_TBPRD2000//Periodregister
#defineEPWM2_MAX_CMPA1950
#defineEPWM2_MIN_CMPA50
#defineEPWM2_MAX_CMPB1950
#defineEPWM2_MIN_CMPB50
#defineEPWM3_TIMER_TBPRD2000//Periodregister
#defineEPWM3_MAX_CMPA950
#defineEPWM3_MIN_CMPA50
#defineEPWM3_MAX_CMPB1950
#defineEPWM3_MIN_CMPB1050
//Tokeeptrackofwhichwaythecomparevalueismoving
#defineEPWM_CMP_UP1
#defineEPWM_CMP_DOWN0
voidmain(void)
{
//Step1.InitializeSystemControl:
//PLL,WatchDog,enablePeripheralClocks
//ThisexamplefunctionisfoundintheDSP280x_SysCtrl.cfile.
InitSysCtrl();
//Step2.InitalizeGPIO:
//ThisexamplefunctionisfoundintheDSP280x_Gpio.cfileand
//illustrateshowtosettheGPIOtoit'sdefaultstate.
//InitGpio();//Skippedforthisexample
EALLOW;
GpioCtrlRegs.GPAMUX1.all=0x0;//GPIOpin
GpioCtrlRegs.GPADIR.all=0xFF;//Outputpin
GpioDataRegs.GPADAT.all=0xFF;//CloseLEDs
EDIS;
//ForthiscasejustinitGPIOpinsforePWM1,ePWM2,ePWM3
//ThesefunctionsareintheDSP280x_EPwm.cfile
InitEPwm1Gpio();
InitEPwm2Gpio();
InitEPwm3Gpio();
EALLOW;
GpioCtrlRegs.GPAMUX1.bit.GPIO11=0;//GPIOpin
GpioCtrlRegs.GPADIR.bit.GPIO11=1;//Outputpin
GpioDataRegs.GPADAT.bit.GPIO11=0;//GPIO11输出拉低是为了使能3245的/OE引脚
EDIS;
//Step3.ClearallinterruptsandinitializePIEvectortable:
//DisableCPUinterrupts
DINT;
//InitializethePIEcontrolregisterstotheirdefaultstate.
//ThedefaultstateisallPIEinterruptsdisabledandflags
//arecleared.
//ThisfunctionisfoundintheDSP280x_PieCtrl.cfile.
InitPieCtrl();
//DisableCPUinterruptsandclearallCPUinterruptflags:
IER=0x0000;
IFR=0x0000;
//InitializethePIEvectortablewithpointerstotheshellInterrupt
//ServiceRoutines(ISR).
//Thiswillpopulatetheentiretable,eveniftheinterrupt
//isnotusedinthisexample.Thisisusefulfordebugpurposes.
//TheshellISRroutinesarefoundinDSP280x_DefaultIsr.c.
//ThisfunctionisfoundinDSP280x_PieVect.c.
InitPieVectTable();
//Interruptsthatareusedinthisexamplearere-mappedto
//ISRfunctionsfoundwithinthisfile.
EALLOW;//ThisisneededtowritetoEALLOWprotectedregisters
PieVectTable.EPWM1_INT=&epwm1_isr;
PieVectTable.EPWM2_INT=&epwm2_isr;
PieVectTable.EPWM3_INT=&epwm3_isr;
EDIS;//ThisisneededtodisablewritetoEALLOWprotectedregisters
//Step4.InitializealltheDevicePeripherals:
//ThisfunctionisfoundinDSP280x_InitPeripherals.c
//InitPeripherals();//Notrequiredforthisexample
//Forthisexample,onlyinitializetheePWM
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC=0;
EDIS;
InitEPwm1Example();
InitEPwm2Example();
InitEPwm3Example();
EALLOW;
SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC=1;
EDIS;
//Step5.Userspecificcode,enableinterrupts:
//EnableCPUINT3whichisconnectedtoEPWM1-3INT:
IER|=M_INT3;
//EnableEPWMINTninthePIE:
Group3interrupt1-3
PieCtrlRegs.PIEIER3.bit.INTx1=1;
PieCtrlRegs.PIEIER3.bit.INTx2=1;
PieCtrlRegs.PIEIER3.bit.INTx3=1;
//EnableglobalInterruptsandhigherpriorityreal-timedebugevents:
EINT;//EnableGlobalinterruptINTM
ERTM;//EnableGlobalrealtimeinterruptDBGM
//Step6.IDLEloop.Justsitandloopforever(optional):
for(;;)
{
asm("NOP");
}
}
interruptvoidepwm1_isr(void)
{
//UpdatetheCMPAandCMPBvalues
update_compare(&epwm1_info);
//ClearINTflagforthistimer
EPwm1Regs.ETCLR.bit.INT=1;
//Acknowledgethisinterrupttoreceivemoreinterruptsfromgroup3
PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;
}
interruptvoidepwm2_isr(void)
{
//UpdatetheCMPAandCMPBvalues
update_compare(&epwm2_info);
//ClearINTflagforthistimer
EPwm2Regs.ETCLR.bit.INT=1;
//Acknowledgethisinterrupttoreceivemoreinterruptsfromgroup3
PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;
}
interruptvoidepwm3_isr(void)
{
//UpdatetheCMPAandCMPBvalues
update_compare(&epwm3_info);
//ClearINTflagforthistimer
EPwm3Regs.ETCLR.bit.INT=1;
//Acknowledgethisinterrupttoreceivemoreinterruptsfromgroup3
PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;
}
voidInitEPwm1Example()
{
//SetupTBCLK
EPwm1Regs.TBPRD=EPWM1_TIMER_TBPRD;//Settimerperiod801TBCLKs
EPwm1Regs.TBPHS.half.TBPHS=0x0000;//Phaseis0
EPwm1Regs.TBCTR=0x0000;//Clearcounter
//SetComparevalues
EPwm1Regs.CMPA.half.CMPA=EPWM1_MIN_CMPA;//SetcompareAvalue
EPwm1Regs.CMPB=EPWM1_MAX_CMPB;//SetCompareBvalue
//Setupcountermode
EPwm1Regs.TBCTL.bit.CTRMODE=TB_COUNT_UPDOWN;//Countup
EPwm1Regs.TBCTL.bit.PHSEN=TB_DISABLE;//Disablephaseloading
EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//ClockratiotoSYSCLKOUT
EPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;
//Setupshadowing
EPwm1Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;
EPwm1Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm1Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO;//LoadonZero
EPwm1Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
//Setactions
EPwm1Regs.AQCTLA.bit.CAU=AQ_SET;//SetPWM1AoneventA,upcount
EPwm1Regs.AQCTLA.bit.CAD=AQ_CLEAR;//ClearPWM1AoneventA,downcount
EPwm1Regs.AQCTLB.bit.CBU=AQ_SET;//SetPWM1BoneventB,upcount
EPwm1Regs.AQCTLB.bit.CBD=AQ_CLEAR;//ClearPWM1BoneventB,downcount
//InterruptwherewewillchangetheCompareValues
EPwm1Regs.ETSEL.bit.INTSEL=ET_CTR_ZERO;//SelectINTonZeroevent
EPwm1Regs.ETSEL.bit.INTEN=1;//EnableINT
EPwm1Regs.ETPS.bit.INTPRD=ET_3RD;//GenerateINTon3rdevent
//Informationthisexampleusestokeeptrack
//ofthedirectiontheCMPA/CMPBvaluesare
//moving,theminandmaxallowedvaluesand
//apointertothecorrectePWMregisters
epwm1_info.EPwm_CMPA_Direction=EPWM_CMP_UP;//StartbyincreasingCMPA&
epwm1_info.EPwm_CMPB_Direction=EPWM_CMP_DOWN;//decreasingCMPB
epwm1_info.EPwmTimerIntCount=0;//Zerotheinterruptcounter
epwm1_info.EPwmRegHandle=&EPwm1Regs;//SetthepointertotheePWMmodule
epwm1_info.EPwmMaxCMPA=EPWM1_MAX_CMPA;//Setupmin/maxCMPA/CMPBvalues
epwm1_info.EPwmMinCMPA=EPWM1_MIN_CMPA;
epwm1_info.EPwmMaxCMPB=EPWM1_MAX_CMPB;
epwm1_info.EPwmMinCMPB=EPWM1_MIN_CMPB;
}
voidInitEPwm2Example()
{
//SetupTBCLK
EPwm2Regs.TBPRD=EPWM2_TIMER_TBPRD;//Settimerperiod801TBCLKs
EPwm2Regs.TBPHS.half.TBPHS=0x0000;//Phaseis0
EPwm2Regs.TBCTR=0x0000;//Clearcounter
//SetComparevalues
EPwm2Regs.CMPA.half.CMPA=EPWM2_MIN_CMPA;//SetcompareAvalue
EPwm2Regs.CMPB=EPWM2_MIN_CMPB;//SetCompareBvalue
//Setupcountermode
EPwm2Regs.TBCTL.bit.CTRMODE=TB_COUNT_UPDOWN;//Countup
EPwm2Regs.TBCTL.bit.PHSEN=TB_DISABLE;//Disablephaseloading
EPwm2Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//ClockratiotoSYSCLKOUT
EPwm2Regs.TBCTL.bit.CLKDIV=TB_DIV1;
//Setupshadowing
EPwm2Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;
EPwm2Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;
EPwm2Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO;//LoadonZero
EPwm2Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;
//Setactions
EPwm2Regs.AQCTLA.bit.CAU=AQ_SET;//SetPWM2AoneventA,upcount
EPwm2Regs.AQCTLA.bit.CBD=AQ_CLEAR;//ClearPWM2AoneventB,downcount
EPwm2Regs.AQCTLB.bit.ZRO=AQ_CLEAR;//ClearPWM2Bonzero
EPwm2Regs.AQCTLB.bit.PRD=AQ_SET;//SetPWM2Bonperiod
//InterruptwherewewillchangetheCompareValues
EPwm2Regs.ETSEL.bit.INTSEL=ET_CTR_ZERO;//SelectINTonZeroevent
EPwm2Regs.ETSEL.bit.INTEN=1;//EnableINT
EPwm2Regs.ETPS.bit.INTPRD=ET_3RD;//GenerateINTon3rdevent
//Informationthisexampleusestokeeptrack
//ofthedirectiontheCMPA/CMPBvaluesare
//moving,theminandmaxallowedvaluesand
//apointertothecorrectePWMregisters
epwm2_info.EPwm_CMPA_Direction=EPWM_CMP_UP;//StartbyincreasingCMPA&
epwm2_info.EPwm_CMPB_Direction=EPWM_CMP_UP;//increasingCMPB
epwm2_info.EPwmTimerIntCount=0;//Zerotheinterruptcounter
epwm2_info.EPwmRegHandle=&EPwm2Regs;//SetthepointertotheePWMmodule
epwm2_info.EPwmMaxCMPA=EPWM2_MAX_CMPA;//Setupmin/maxCMPA/CMPBvalues
epwm2_info.EPwmMinCMPA=EPWM2_MIN_CMPA;
epwm2_info.EPwmMaxCMPB=EPWM2_MAX_CMPB;
epwm2_info.EPwmMinCMPB=EPWM2_MIN_CMPB;
}
voidInitEPwm3Example(void)
{
//SetupTBCLK
EPwm3Regs.TBCTL.bit.CTRMODE=TB_COUNT_UPDOWN;//Countup/down
EPwm3Regs.TBPRD=EPWM3_TIMER_TBPRD;//Settimerperiod
EPwm3Regs.TBCTL.bit.PHSEN=TB_DISABLE;//Disablephaseloading
EPwm3Regs.TBPHS.half.TBPHS=0x0000;//Phaseis0
EPwm3Regs.TBCTR=0x0000;//Clearcounter
EPwm3Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//ClockratiotoSYSCLKOUT
EPwm3Regs.TBCTL.bit.CLKDIV=TB_DIV1;
//Setupshadowreg