本文來(lái)源電子發(fā)燒友社區(qū),作者:jf_55236000, 帖子地址:https://bbs.elecfans.com/jishu_2292646_1_1.html
從官方對(duì)DSC28034芯片的定位可以看出,該芯片是比較適合應(yīng)用于電力電子電源設(shè)備的主控芯片的,下面就針對(duì)電力電子應(yīng)用的主要外設(shè)EPWM和ADC的配置進(jìn)行簡(jiǎn)單的說(shuō)明,首先介紹PWM的配置,配置EPWM1模塊的兩路互補(bǔ)帶死區(qū)PWM輸出EPWM1A和EPWM1B作為單相H4橋的L橋臂兩管驅(qū)動(dòng),配置EPWM2模塊的兩路互補(bǔ)帶死區(qū)PWM輸出EPWM2A和EPWM2B作為單相H4橋的N橋臂兩管驅(qū)動(dòng),同時(shí)在EPWM1計(jì)數(shù)器為0時(shí)觸發(fā)AD采樣觸發(fā)信號(hào)EPWM1_SOCA,以實(shí)現(xiàn)電感電流上升沿中心點(diǎn)采樣,設(shè)置在計(jì)數(shù)器在AD采樣觸發(fā)后5us觸發(fā)EPWM1中斷,然后在中斷中讀取AD采樣值,然后就可以執(zhí)行閉環(huán)算法了。
具體PWM初始化代碼如下:
/******************************************************************
*函數(shù)名:void InitEPWM(void) *參 數(shù) :無(wú) *返回值:無(wú) *作 用 :初始化EPWM ******************************************************************/ void InitEPWM(void) { EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0; /每個(gè)啟用的ePWM模塊中的TBCLK(時(shí)基時(shí)鐘)均已停止。/ EDIS;
//=============EPwm1 Init For INV-L===============// EALLOW; EPwm1Regs.TZCTL.bit.TZA = TZ_FORCE_LO; /*當(dāng)外部觸發(fā)事件發(fā)生時(shí),強(qiáng)制EPWMxA為低電平*/ EPwm1Regs.TZCTL.bit.TZB = TZ_FORCE_LO; /*當(dāng)外部觸發(fā)事件發(fā)生時(shí),強(qiáng)制EPWMxA為低電平*/ EPwm1Regs.TZFRC.bit.OST = 1; /*強(qiáng)制單次觸發(fā)事件產(chǎn)生*/ EDIS;
EPwm1Regs.TBPRD = EPWM1_TIMER_TBPRD; /* Period = 4000*TBCLK counts*/ EPwm1Regs.CMPA.half.CMPA = EPWM1_TIMER_HALFPRD; /*設(shè)置CMPA值為EPWM1_TIMER_HALFPRD*/ EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; /*增減計(jì)數(shù)模式*/ EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; /*禁止TBCTR加載相位寄存器TBPHS中的值*/ EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW; /*TBPHS寄存器工作模式選擇:映射模式*/ EPwm1Regs.TBCTL.bit.PHSDIR = TB_UP; /*相位同步后計(jì)數(shù)器方向?yàn)橄蛏嫌?jì)數(shù)*/ EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_CTR_ZERO; /*計(jì)數(shù)器CTR = Zero時(shí)發(fā)出同步信號(hào)*/ EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; /*高速時(shí)鐘分頻 1倍分頻*/ EPwm1Regs.TBCTL.bit.CLKDIV = TB_DIV1; /*時(shí)基時(shí)鐘分頻 1倍分頻*/ EPwm1Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW; /*CMPA寄存器工作模式選擇:映射模式*/ EPwm1Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW; /*CMPB寄存器工作模式選擇:映射模式*/ EPwm1Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; /*當(dāng)TBCTR=0x0000時(shí),CMPA主寄存器從映射寄存器中加載數(shù)據(jù)*/ EPwm1Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; /*當(dāng)TBCTR=0x0000時(shí),CMPB主寄存器從映射寄存器中加載數(shù)據(jù)*/ EPwm1Regs.AQCTLA.bit.CAU = AQ_CLEAR; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPA的值,且正在增計(jì)數(shù)時(shí),使EPWMxA輸出低電平*/ EPwm1Regs.AQCTLA.bit.CAD = AQ_SET; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPA的值,且正在減計(jì)數(shù)時(shí),使EPWMxA輸出高電平*/ EPwm1Regs.AQCTLB.bit.CBU = AQ_SET; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPB的值,且正在增計(jì)數(shù)時(shí),使EPWMxB輸出高電平*/ EPwm1Regs.AQCTLB.bit.CBD = AQ_CLEAR; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPB的值,且正在減計(jì)數(shù)時(shí),使EPWMxB輸出低電平*/ EPwm1Regs.ETSEL.bit.SOCASEL = ET_CTR_ZERO; /*選擇EPWMx_SOCA產(chǎn)生的條件:TBCTR=0x0000時(shí)產(chǎn)生*/ EPwm1Regs.ETPS.bit.SOCAPRD = ET_1ST; /*ePWM觸發(fā)采樣(EPWMx_SOCA)周期選擇:每發(fā)生1次事件產(chǎn)生中斷信號(hào)EPWMx_SOCA*/ EPwm1Regs.ETSEL.bit.SOCAEN = 1; /*使能產(chǎn)生觸發(fā)采樣信號(hào)EPWMx_SOCA*/ EPwm1Regs.CMPB = EPWM1_CMPB_INT_CNT; /*設(shè)置CMPB值為EPWM1_CMPB_INT_CNT*/ EPwm1Regs.ETSEL.bit.INTSEL = ET_CTRU_CMPB; /*選擇EPWMx_INT產(chǎn)生的條件:TBCTR=CMPB時(shí)產(chǎn)生*/ EPwm1Regs.ETPS.bit.INTPRD = ET_1ST; /*ePWM中斷(EPWMx_INT)周期選擇:每發(fā)生1次事件產(chǎn)生中斷信號(hào)EPWMx_INT*/ EPwm1Regs.ETSEL.bit.INTEN = 0; /*使能產(chǎn)生中斷信號(hào)EPWMx_INT*/ EPwm1Regs.AQSFRC.bit.RLDCSF = 0; /*當(dāng)計(jì)數(shù)器TBCTR=0x0000時(shí)加載強(qiáng)制事件*/ EPwm1Regs.AQCSFRC.bit.CSFA = AQ_NO_ACTION; /*軟件強(qiáng)制觸發(fā)不動(dòng)作*/ EPwm1Regs.AQCSFRC.bit.CSFB = AQ_NO_ACTION; EPwm1Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; /*使能上升沿和下降沿延時(shí)信號(hào)*/ EPwm1Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; /*主低模式:EPWMxA和EPWMxB都反轉(zhuǎn)極性*/ EPwm1Regs.DBCTL.bit.IN_MODE = DBA_ALL; /*EPWMxA作為上升沿和下降沿時(shí)的信號(hào)源*/ EPwm1Regs.DBRED = EPWM1_TIMER_DB; /*死區(qū)上升沿延時(shí)計(jì)數(shù)器*/ EPwm1Regs.DBFED = EPWM1_TIMER_DB; /*死區(qū)下降沿延時(shí)計(jì)數(shù)器*/
//=============EPwm2 Init For INV-N===============// EALLOW; EPwm2Regs.TZCTL.bit.TZA = TZ_FORCE_LO; /*當(dāng)外部觸發(fā)事件發(fā)生時(shí),強(qiáng)制EPWMxA為低電平*/ EPwm2Regs.TZCTL.bit.TZB = TZ_FORCE_LO; /*當(dāng)外部觸發(fā)事件發(fā)生時(shí),強(qiáng)制EPWMxA為低電平*/ EPwm2Regs.TZFRC.bit.OST = 1; /*強(qiáng)制單次觸發(fā)事件產(chǎn)生*/ EDIS;
EPwm2Regs.TBPRD = EPWM1_TIMER_TBPRD; /* Period = 4000*TBCLK counts*/ EPwm2Regs.CMPA.half.CMPA = EPWM1_TIMER_HALFPRD; /*設(shè)置CMPA值為EPWM1_TIMER_HALFPRD*/ EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; /*增減計(jì)數(shù)模式*/ EPwm2Regs.TBCTL.bit.PHSEN = TB_ENABLE; /*使能TBCTR加載相位寄存器TBPHS中的值*/ EPwm2Regs.TBCTL.bit.PRDLD = TB_SHADOW; /*TBPHS寄存器工作模式選擇:映射模式*/ EPwm2Regs.TBCTL.bit.PHSDIR = TB_UP; /*相位同步后計(jì)數(shù)器方向?yàn)橄蛏嫌?jì)數(shù)*/ EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_IN; /*將輸入同步信號(hào)作為同步信號(hào)輸出*/ EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_DIV1; /*高速時(shí)鐘分頻 1倍分頻*/ EPwm2Regs.TBCTL.bit.CLKDIV = TB_DIV1; /*時(shí)基時(shí)鐘分頻 1倍分頻*/ EPwm2Regs.CMPCTL.bit.SHDWAMODE = CC_SHADOW; /*CMPA寄存器工作模式選擇:映射模式*/ EPwm2Regs.CMPCTL.bit.SHDWBMODE = CC_SHADOW; /*CMPB寄存器工作模式選擇:映射模式*/ EPwm2Regs.CMPCTL.bit.LOADAMODE = CC_CTR_ZERO; /*當(dāng)TBCTR=0x0000時(shí),CMPA主寄存器從映射寄存器中加載數(shù)據(jù)*/ EPwm2Regs.CMPCTL.bit.LOADBMODE = CC_CTR_ZERO; /*當(dāng)TBCTR=0x0000時(shí),CMPB主寄存器從映射寄存器中加載數(shù)據(jù)*/ EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPA的值,且正在增計(jì)數(shù)時(shí),使EPWMxA輸出低電平*/ EPwm2Regs.AQCTLA.bit.CAD = AQ_SET; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPA的值,且正在減計(jì)數(shù)時(shí),使EPWMxA輸出高電平*/ EPwm2Regs.AQCTLB.bit.CBU = AQ_SET; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPB的值,且正在增計(jì)數(shù)時(shí),使EPWMxB輸出高電平*/ EPwm2Regs.AQCTLB.bit.CBD = AQ_CLEAR; /*當(dāng)時(shí)間基準(zhǔn)計(jì)數(shù)器的值等于CMPB的值,且正在減計(jì)數(shù)時(shí),使EPWMxB輸出低電平*/ EPwm2Regs.AQSFRC.bit.RLDCSF = 0; /*當(dāng)計(jì)數(shù)器TBCTR=0x0000時(shí)加載強(qiáng)制事件*/ EPwm2Regs.AQCSFRC.bit.CSFA = AQ_NO_ACTION; /*軟件強(qiáng)制觸發(fā)不動(dòng)作*/ EPwm2Regs.AQCSFRC.bit.CSFB = AQ_NO_ACTION; EPwm2Regs.DBCTL.bit.OUT_MODE = DB_FULL_ENABLE; /*使能上升沿和下降沿延時(shí)信號(hào)*/ EPwm2Regs.DBCTL.bit.POLSEL = DB_ACTV_HIC; /*主低模式:EPWMxA和EPWMxB都反轉(zhuǎn)極性*/ EPwm2Regs.DBCTL.bit.IN_MODE = DBA_ALL; /*EPWMxA作為上升沿和下降沿時(shí)的信號(hào)源*/ EPwm2Regs.DBRED = EPWM1_TIMER_DB; /*死區(qū)上升沿延時(shí)計(jì)數(shù)器*/ EPwm2Regs.DBFED = EPWM1_TIMER_DB; /*死區(qū)下降沿延時(shí)計(jì)數(shù)器*/ // 設(shè)置EPWM同步相位值 EPwm1Regs.TBPHS.half.TBPHS = 0; /*epwm時(shí)基計(jì)數(shù)器相位=0*/ EPwm2Regs.TBPHS.half.TBPHS = 0; /*epwm時(shí)基計(jì)數(shù)器相位=0*/ // 設(shè)置EPWM計(jì)數(shù)寄存器初值 EPwm1Regs.TBCTR = 0x0000; /*當(dāng)時(shí)基計(jì)數(shù)器等于零(TBCTR = 0x0000)時(shí),影子寄存器的內(nèi)容將傳輸?shù)交顒?dòng)寄存器TBPRD(活 動(dòng))←TBPRD(影子)*/ EPwm2Regs.TBCTR = 0x0000; EALLOW; SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1; /*所有使能的ePWM模塊同步使用TBCLK*/ EDIS;
}
以下是ADC采樣初始化:設(shè)置16路AD采樣通道,所有通道采樣觸發(fā)為EPWM1_SOCA
/******************************************************************
*函數(shù)名:void InitAdcSample(void) *參 數(shù) :無(wú) *返回值:無(wú) *作 用 :初始化AD采樣 *****************************************************************/ void InitAdcSample(void) { EALLOW; AdcRegs.ADCCTL1.bit.ADCENABLE = 1; / 使能ADC,并給ADC上電,寫(xiě)0無(wú)效,寫(xiě)1使能ADC / AdcRegs.ADCCTL2.bit.CLKDIV2EN = 1; / 當(dāng)系統(tǒng)時(shí)鐘是120M的時(shí)候,配置為1,為SYSCLK/4, 當(dāng)系統(tǒng)時(shí)鐘是60M的時(shí)候,保持默認(rèn)值0,為SYSCLK/2 / while(AdcRegs.ADCCTL1.bit.ADCRDY != 1) / ADC 復(fù)位完成,當(dāng)ADCRDY = 1表示復(fù)位完成,0表示復(fù)位未完成 */ {
} AdcRegs.INTSEL1N2.bit.INT1SEL = 15; /* ADCINT1 EOC 源選擇 EOC15 作為 ADCINT1作為觸發(fā)源 */ AdcRegs.INTSEL1N2.bit.INT1E = 0; /* ADCINT1中斷屏蔽 */ AdcRegs.INTSEL1N2.bit.INT1CONT = 0; /* 沒(méi)有進(jìn)一步的中斷脈沖產(chǎn)生直到中斷信號(hào)標(biāo)志位被清除 */ AdcRegs.ADCSOC0CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC0CTL.bit.CHSEL = 0; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA0 */ AdcRegs.ADCSOC0CTL.bit.TRIGSEL = 0x5; /* SOC0的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC1CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC1CTL.bit.CHSEL = 1; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA1 */ AdcRegs.ADCSOC1CTL.bit.TRIGSEL = 0x5; /* SOC1的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC2CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC2CTL.bit.CHSEL = 2; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA2 */ AdcRegs.ADCSOC2CTL.bit.TRIGSEL = 0x5; /* SOC2的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC3CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC3CTL.bit.CHSEL = 3; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA3 */ AdcRegs.ADCSOC3CTL.bit.TRIGSEL = 0x5; /* SOC3的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC4CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC4CTL.bit.CHSEL = 4; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA4 */ AdcRegs.ADCSOC4CTL.bit.TRIGSEL = 0x5; /* SOC4的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC5CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC5CTL.bit.CHSEL = 5; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA5 */ AdcRegs.ADCSOC5CTL.bit.TRIGSEL = 0x5; /* SOC5的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC6CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC6CTL.bit.CHSEL = 6; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA6 */ AdcRegs.ADCSOC6CTL.bit.TRIGSEL = 0x5; /* SOC6的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC7CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC7CTL.bit.CHSEL = 7; /* 選擇轉(zhuǎn)換的輸入通道是ADCINA7 */ AdcRegs.ADCSOC7CTL.bit.TRIGSEL = 0x5; /* SOC7的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC8CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC8CTL.bit.CHSEL = 8; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB0 */ AdcRegs.ADCSOC8CTL.bit.TRIGSEL = 0x5; /* SOC8的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC9CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC9CTL.bit.CHSEL =9; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB1 */ AdcRegs.ADCSOC9CTL.bit.TRIGSEL = 0x5; /* SOC9的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC10CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC10CTL.bit.CHSEL = 10; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB2 */ AdcRegs.ADCSOC10CTL.bit.TRIGSEL = 0x5; /* SOC10的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC11CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC11CTL.bit.CHSEL = 11; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB3 */ AdcRegs.ADCSOC11CTL.bit.TRIGSEL = 0x5; /* SOC11的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC12CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC12CTL.bit.CHSEL = 12; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB4 */ AdcRegs.ADCSOC12CTL.bit.TRIGSEL = 0x5; /* SOC12的觸發(fā)源是epwm1的ADCSOCA */ AdcRegs.ADCSOC13CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC13CTL.bit.CHSEL = 13; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB5 */ AdcRegs.ADCSOC13CTL.bit.TRIGSEL = 0x5; /* SOC13的觸發(fā)源是epwm4的ADCSOCA */ AdcRegs.ADCSOC14CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC14CTL.bit.CHSEL = 14; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB6 */ AdcRegs.ADCSOC14CTL.bit.TRIGSEL = 0x5; /* SOC14的觸發(fā)源是epwm5的ADCSOCA */ AdcRegs.ADCSOC15CTL.bit.ACQPS = 6; /* 窗口的采樣周期是2n+6個(gè)clock cycles */ AdcRegs.ADCSOC15CTL.bit.CHSEL = 15; /* 選擇轉(zhuǎn)換的輸入通道是ADCINB7 */ AdcRegs.ADCSOC15CTL.bit.TRIGSEL = 0x5; /* SOC15的觸發(fā)源是epwm6的ADCSOCA */ AdcRegs.ADCINTSOCSEL1.all = 0x0000; /* No ADCInterrupt will trigger SOCx*/ AdcRegs.ADCINTSOCSEL2.all = 0x0000; AdcRegs.SOCPRICTL.bit.SOCPRIORITY = 0x00; /* SOC priority is handled in round robin mode for all channels.*/ AdcRegs.SOCPRICTL.bit.RRPOINTER = 0x0F; /* SOC15是最后一個(gè)轉(zhuǎn)換,SOC0是最高的round robin優(yōu)先 */ AdcRegs.SOCPRICTL.bit.ONESHOT = 0; /* One shot 模式不使能 */ EDIS;
}
然后配置PWM輸出的GPIO:
``
/******************************************************************
*函數(shù)名:void InitGPIO(void)
*參 數(shù) :無(wú)
*返回值:無(wú)
*作 用 :初始化GPIO
******************************************************************/
void InitGPIO(void)
{
EALLOW;
GpioCtrlRegs.GPAPUD.bit.GPIO0 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO0 = 1; // GPIO0 = PWM1A-->OPWM1A/GPIO0
GpioCtrlRegs.GPAPUD.bit.GPIO1 = 0;
GpioCtrlRegs.GPAMUX1.bit.GPIO1 = 1; // GPIO1 = PWM1B-->OPWM1B/GPIO1
GpioCtrlRegs.GPAPUD.bit.GPIO2 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO2 = 1; // GPIO2 = PWM2A-->OPWM2A/GPIO2
GpioCtrlRegs.GPAPUD.bit.GPIO3 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO3 = 1; // GPIO3 = PWM2B-->OPWM2B/GPIO3
GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 1; // GPIO4 = PWM3A-->OPWM3A/GPIO4
GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 1; // GPIO5 = PWM3B-->OPWM3B/GPIO5
GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 1; // GPIO6 = PWM4A-->OPWM4A/GPIO6
GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 1; // GPIO7 = PWM4B-->OPWM4B/GPIO7
GpioCtrlRegs.GPAPUD.bit.GPIO8 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 1; // GPIO8 = PWM5A-->OPWM5A/GPIO8
GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 1; // GPIO9 = PWM5B-->OPWM5B/GPIO9
GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 1; // GPIO10 = PWM6A-->OPWM6A/GPIO10
GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0; GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 1; // GPIO11 = PWM6B-->OPWM6B/GPIO11 EDIS; }
配置好PWM中斷使能
/******************************************************************
*函數(shù)名:void EnableInt(void)
*參 數(shù) :無(wú)
*返回值:無(wú)
*作 用 :使能外設(shè)中斷
******************************************************************/
void EnableInt(void)
{
EALLOW; /允許訪問(wèn)受保護(hù)的空間/
PieVectTable.EPWM1_INT = &Epwm1_ISR; /將Epwm1_ISR入口地址賦給EPWM1_INT/
EDIS; /禁止訪問(wèn)受保護(hù)的空間/
IER |= M_INT3; /*使能CPU中斷組3*/
PieCtrlRegs.PIEIER3.bit.INTx1 = 1; /使能PIE3.1的中斷/
// EINT;
}
中斷服務(wù)函數(shù):在中斷服務(wù)函數(shù)中取得AD采樣值
/******************************************************************
*函數(shù)名:void INTERRUPT Epwm1_ISR(void)
*參 數(shù):無(wú)
*返回值:無(wú)
*作 用:中斷服務(wù)函數(shù)改變pwm死區(qū)
******************************************************************/
void INTERRUPT Epwm1_ISR(void)
{
EPwm1Regs.ETCLR.bit.INT = 1;
PieCtrlRegs.PIEACK.all = PIEACK_GROUP3;
IntADResultGet();
}
/************************************************************************************
// IntADResultGet
// 功能描述:AD采樣結(jié)果獲取
************************************************************************************/
Uint16 AdcSample[16];
void CODE_SECTION("ramfuncs") IntADResultGet(void)
{
Uint16 iAdRltA0_0, iAdRltA1_0, iAdRltA2_0, iAdRltA3_0, iAdRltA4_0, iAdRltA5_0, iAdRltA6_0, iAdRltA7_0;
Uint16 iAdRltB0_0, iAdRltB1_0, iAdRltB2_0, iAdRltB3_0, iAdRltB4_0, iAdRltB5_0, iAdRltB6_0, iAdRltB7_0;
= 1)
=
{ // 等待AD采樣結(jié)束 }
iAdRltA0_0 = AdcResult.ADCRESULT0; iAdRltA1_0 = AdcResult.ADCRESULT1; iAdRltA2_0 = AdcResult.ADCRESULT2; iAdRltA3_0 = AdcResult.ADCRESULT3; iAdRltA4_0 = AdcResult.ADCRESULT4; iAdRltA5_0 = AdcResult.ADCRESULT5; iAdRltA6_0 = AdcResult.ADCRESULT6; iAdRltA7_0 = AdcResult.ADCRESULT7; iAdRltB0_0 = AdcResult.ADCRESULT8; iAdRltB1_0 = AdcResult.ADCRESULT9; iAdRltB2_0 = AdcResult.ADCRESULT10; iAdRltB3_0 = AdcResult.ADCRESULT11; iAdRltB4_0 = AdcResult.ADCRESULT12; iAdRltB5_0 = AdcResult.ADCRESULT13; iAdRltB6_0 = AdcResult.ADCRESULT14; iAdRltB7_0 = AdcResult.ADCRESULT15;
iAdRltA0_0; = iAdRltA1_0; = iAdRltA2_0; = iAdRltA3_0; = iAdRltA4_0; = iAdRltA5_0; = iAdRltA6_0; = iAdRltA7_0;
=
AdcSample[8] = iAdRltB0_0; AdcSample[9] = iAdRltB1_0; AdcSample[10] = iAdRltB2_0; AdcSample[11] = iAdRltB3_0; AdcSample[12] = iAdRltB4_0; AdcSample[13] = iAdRltB5_0; AdcSample[14] = iAdRltB6_0; AdcSample[15] = iAdRltB7_0; }
主函數(shù)中打開(kāi)總中斷和看門(mén)狗喂狗等
int main(void)
{ SystemInit(); // 片上外設(shè)初始化
EnableDog(); // 使能看門(mén)狗 EPwm1Regs.ETCLR.bit.INT = 1; // 使能中斷 EPwm1Regs.ETSEL.bit.INTEN = 1; EINT; while(1) { ServiceDog(); // 看門(mén)狗喂狗 SysTimeBase(); // 系統(tǒng)時(shí)基 StateLEDCtrl(); // 系統(tǒng)狀態(tài)燈控制 } return 0;
}
仿真讀取的AD采樣值:
只有第ADC_A4通道接著3.3V,所以它的采樣值為4096,其他都為0
另外說(shuō)明一個(gè)小問(wèn)題,在仿真時(shí)是沒(méi)辦法實(shí)時(shí)看變量的,IDE不支持,這個(gè)很影響調(diào)試
同時(shí),手冊(cè)上說(shuō)的參考2.5.8,實(shí)際是沒(méi)有這個(gè)章節(jié)的
-
中科昊芯
+關(guān)注
關(guān)注
5文章
80瀏覽量
5190 -
開(kāi)發(fā)板試用
+關(guān)注
關(guān)注
3文章
301瀏覽量
2121
發(fā)布評(píng)論請(qǐng)先 登錄
相關(guān)推薦
評(píng)論