您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 资本运营 > 基于DSP28035的SPWM生成程序
#includeDSP28x_Project.h#includeIQmathLib.h//#includemath.h#definePWM1_INT_ENABLE1#definePWM2_INT_ENABLE1#definePWM3_INT_ENABLE1#definePWM4_INT_ENABLE1voidInitEPwm1Example(void);voidInitEPwm2Example(void);voidInitEPwm3Example(void);interruptvoidepwm1_timer_isr(void);//interruptvoidepwm2_timer_isr(void);//interruptvoidepwm3_timer_isr(void);Uint32EPwm1TimerIntCount;Uint32EPwm2TimerIntCount;Uint32EPwm3TimerIntCount;Uint32EPwm4TimerIntCount;Uint32TonC1,TonC2;Uint16k;float32Tc,N;#definePI3.1415926float32M=0.8;float32fz=20000;float32fr=50;voidmain(void){InitSysCtrl();InitEPwm1Gpio();//InitEPwm2Gpio();//InitEPwm3Gpio();DINT;InitPieCtrl();IER=0x0000;IFR=0x0000;InitPieVectTable();EALLOW;//ThisisneededtowritetoEALLOWprotectedregistersPieVectTable.EPWM1_INT=&epwm1_timer_isr;//PieVectTable.EPWM2_INT=&epwm2_timer_isr;//PieVectTable.EPWM3_INT=&epwm3_timer_isr;EDIS;EALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC=0;//StopalltheTBclocksEDIS;InitEPwm1Example();//InitEPwm2Example();//InitEPwm3Example();EALLOW;SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC=1;EDIS;EPwm1TimerIntCount=0;EPwm2TimerIntCount=0;EPwm3TimerIntCount=0;IER|=M_INT3;PieCtrlRegs.PIEIER3.bit.INTx1=PWM1_INT_ENABLE;//PieCtrlRegs.PIEIER3.bit.INTx2=PWM2_INT_ENABLE;//PieCtrlRegs.PIEIER3.bit.INTx3=PWM3_INT_ENABLE;EINT;//EnableGlobalinterruptINTMERTM;for(;;){asm(NOP);}}voidInitEPwm1Example(){EPwm1Regs.TBCTL.bit.SYNCOSEL=TB_CTR_ZERO;//zhuyiEPwm1Regs.TBPRD=1500;//载波周期=2*1500TBCLKEPwm1Regs.TBPHS.half.TBPHS=0x0000;//Phaseis0EPwm1Regs.TBCTL.bit.PRDLD=TB_SHADOW;EPwm1Regs.TBCTR=0x0000;EPwm1Regs.TBCTL.bit.CTRMODE=TB_COUNT_UPDOWN;//CountupdownEPwm1Regs.TBCTL.bit.PHSEN=TB_DISABLE;//DisablephaseloadingEPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//ClockratiotoSYSCLKOUTEPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;EPwm1Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;//LoadregisterseveryZERO_PRDEPwm1Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;EPwm1Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO_PRD;EPwm1Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO_PRD;EPwm1Regs.CMPA.half.CMPA=1000;EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;//ClearPWM1AonCAUEPwm1Regs.AQCTLA.bit.CAD=AQ_SET;EPwm1Regs.AQCTLB.bit.CAU=AQ_SET;//SETPWM1BonCAUEPwm1Regs.AQCTLB.bit.CAD=AQ_CLEAR;//setdeadbandEPwm1Regs.ETSEL.bit.INTSEL=ET_CTR_PRDZERO;//?SelectINTonZeroPrdeventEPwm1Regs.ETSEL.bit.INTEN=PWM1_INT_ENABLE;//EnableINTEPwm1Regs.ETPS.bit.INTPRD=ET_1ST;}interruptvoidepwm1_timer_isr(void){EPwm1TimerIntCount++;if(EPwm1Regs.TBCTR==1500){k=0;N=fz/fr;Tc=1/fz;TonC1=Tc/4*(1+M*sin(k*PI/N));k+=2;if(k(2*N-2)){k=0;}EPwm1Regs.CMPA.half.CMPA=TonC1;//EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;//ClearPWM1AonCAU//EPwm1Regs.AQCTLA.bit.CAD=AQ_SET;//EPwm1Regs.AQCTLB.bit.CAU=AQ_SET;//SETPWM1BonCAU//EPwm1Regs.AQCTLB.bit.CAD=AQ_CLEAR;}else{k=1;N=fz/fr;Tc=1/fz;TonC2=Tc/4*(1+M*sin(k*PI/N));k+=2;if(k(2*N-1)){k=1;}EPwm1Regs.CMPA.half.CMPA=TonC2;//EPwm1Regs.AQCTLA.bit.CAU=AQ_CLEAR;//ClearPWM1AonCAU//EPwm1Regs.AQCTLA.bit.CAD=AQ_SET;//EPwm1Regs.AQCTLB.bit.CAU=AQ_SET;//SETPWM1BonCAU//EPwm1Regs.AQCTLB.bit.CAD=AQ_CLEAR;}//ClearINTflagforthistimerEPwm1Regs.ETCLR.bit.INT=1;//Acknowledgethisinterrupttoreceivemoreinterruptsfromgroup3PieCtrlRegs.PIEACK.all=PIEACK_GROUP3;}#include\DSP280x_Device.h\//DSP280xHeaderfileIncludeFile#include\DSP280x_Examples.h\//DSP280xExamplesIncludeFilevoidInitePWM(void){EALLOW;EPwm1Regs.TBPRD=600;//Period=601TBCLKcountsEPwm1Regs.CMPA=350;//CompareA=350TBCLKcountsEPwm1Regs.CMPB=200;//CompareB=200TBCLKcountsEPwm1Regs.TBPHS=0;//SetPhaseregistertozeroEPwm1Regs.TBCTR=0;//clearTBcounterEPwm1Regs.TBCTL.bit.CTRMODE=TB_COUNT_UP;EPwm1Regs.TBCTL.bit.PHSEN=TB_DISABLE;//PhaseloadingdisabledEPwm1Regs.TBCTL.bit.PRDLD=TB_SHADOW;EPwm1Regs.TBCTL.bit.SYNCOSEL=TB_SYNC_DISABLE;EPwm1Regs.TBCTL.bit.HSPCLKDIV=TB_DIV1;//TBCLK=SYSCLKEPwm1Regs.TBCTL.bit.CLKDIV=TB_DIV1;EPwm1Regs.CMPCTL.bit.SHDWAMODE=CC_SHADOW;EPwm1Regs.CMPCTL.bit.SHDWBMODE=CC_SHADOW;EPwm1Regs.CMPCTL.bit.LOADAMODE=CC_CTR_ZERO;//loadonCTR=ZeroEPwm1Regs.CMPCTL.bit.LOADBMODE=CC_CTR_ZERO;//loadonCTR=ZeroEPwm1Regs.AQCTLA.bit.PRD=AQ_CLEAR;EPwm1Regs.AQCTLA.bit.CAU=AQ_SET;EPwm1Regs.AQCTLB.bit.PRD=AQ_CLEAR;EPwm1Regs.AQCTLB.bit.CBU=AQ_SET;EPwm1Regs.DBCTL.bit.OUT_MODE=DB_DISABLE;EPwm1Regs.PCCTL.bit.CHPEN=CHP_DISABLE;EPwm1Regs.TZCTL.bit.TZA=TZ_NO_CHANGE;EPwm1Regs.TZCTL.bit.TZB=TZ_NO_CHANGE;EDIS;1.#includeDSP28_Device.h2.#includestdio.h3.#includemath.h4.#includefloat.h5.#defineNX4046.#definePI3.14159257.floatM=0.8;8.intk0,h1,h2;9.doublea[NX];10.externinttp=1250;11.12.voidzkb();13.interruptvoideva_T1UFINT_ISR(void);14.15.voidmain(void){16.17.k0=0;18.h1=0;19.h2=0;20.InitSysCtrl();21.DINT;//禁止全局中断22.IER=0;23.IFR=0;24.EALLOW;25.//EnablePWMpins26.GpioMuxRegs.GPAMUX.all=0x00FF;//EVAPWM1-6pins27.//GpioMuxRegs.GPBMUX.all=0x00FF;//EVBPWM7-12pins28.EDIS;29.InitPieCtrl();30.InitPieVectTable();31.EALLOW;32.PieVectTable.T1UFINT=&eva_T1UFINT_ISR;33.EDIS;34.IER|=M_INT2;35.PieCtrl.PIEIER2.bit.INTx6=1;36.InitEv();37.zkb();38.EvaRegs.T1CON.all=EvaRegs.T1CON.all|0x0040;//启动定时器139.EINT;40.while(1)41.{42.asm(NOP);43.}44.}45.voidzkb()46.{47.unsignedintn=0;48.floatq,l,j;49.while(n=NX)50.{51.q=(n+0.75);//为消除偶
本文标题:基于DSP28035的SPWM生成程序
链接地址:https://www.777doc.com/doc-5213971 .html