2. EPWM模块配置
//##################################################################
//
// Build Date: 2021.03.13
// Description: This file configures the ePWM module
//
//##################################################################
- void EPWMConfig()
- {
- EALLOW;
- SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 0;
- EDIS;
- EPWM1_Config();
- EPWM2_Config();
- //EPWM3_Config();
- EALLOW;
- SysCtrlRegs.PCLKCR0.bit.TBCLKSYNC = 1;
- EDIS;
- }
- void EPWM1_Config()
- {
- EALLOW;
- //========================================================================
- // Time-Base (TB) Submodule
- //========================================================================
- EPwm1Regs.TBPRD = EPWM1_TBPRD; // the period of the time-base counter
- EPwm1Regs.TBPHS.half.TBPHS = 0; // the time-base counter phase
- EPwm1Regs.TBCTR = 0; // the current time-base counter value
- EPwm1Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Counter Mode
- EPwm1Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Counter Register Load From Phase Register Enable
- EPwm1Regs.TBCTL.bit.PRDLD = TB_SHADOW; // Active Period Register Load From Shadow Register
- EPwm1Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE; // Synchronization Output Select
- EPwm1Regs.TBCTL.bit.HSPCLKDIV = TB_HSPCLK_DIV1; // High Speed Time-base Clock Prescale
- EPwm1Regs.TBCTL.bit.CLKDIV = TB_CLK_DIV1; // Time-base Clock Prescale
- EPwm1Regs.TBCTL.bit.PHSDIR = TB_UP; // Phase Direction
- EPwm1Regs.TBCTL.bit.FREE_SOFT = TB_FREE_RUN; // Emulation Mode Run Mode
- //========================================================================
- // Counter-Compare (CC) Submodule
- //========================================================================
- EPwm1Regs.CMPA.half.CMPA = Pwm1CMPA; // Set compare A value
- //========================================================================
- // Action-Qualifier (AQ) Submodule
- //========================================================================
- EPwm1Regs.AQCTLA.bit.PRD = AQ_CLEAR; // the counter equals the period.
- EPwm1Regs.AQCTLA.bit.ZRO = AQ_SET; // the counter equals the zero.
- EPwm1Regs.AQCTLB.bit.CAD = AQ_CLEAR; // equals CMPA and decrementing.
- EPwm1Regs.AQCTLB.bit.CAU = AQ_SET; // equals CMPA and incrementing.
- EPwm1Regs.AQSFRC.bit.RLDCSF = AQ_IMMEDI;
- //========================================================================
- // PWM-Chopper (PC) Submodule
- //========================================================================
- EPwm1Regs.PCCTL.bit.CHPEN = CHP_DISABLE; // PWM-chopping Enable
- //========================================================================
- // Trip-Zone (TZ) Submodule
- //========================================================================
- EPwm1Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // a trip event occurs output EPWMxA action
- EPwm1Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // a trip event occurs output EPWMxB action
- EDIS;
- }
- void EPWM2_Config()
- {
- EALLOW;
- //========================================================================
- // Time-Base (TB) Submodule
- //========================================================================
- EPwm2Regs.TBPRD = EPWM2_TBPRD; // the period of the time-base counter
- EPwm2Regs.TBPHS.half.TBPHS = 0; // the time-base counter phase
- EPwm2Regs.TBCTR = 0; // the current time-base counter value
- EPwm2Regs.TBCTL.bit.CTRMODE = TB_COUNT_UPDOWN; // Counter Mode
- EPwm2Regs.TBCTL.bit.PHSEN = TB_DISABLE; // Counter Register Load From Phase Register Enable
- EPwm2Regs.TBCTL.bit.PRDLD = TB_SHADOW; // Active Period Register Load From Shadow Register
- EPwm2Regs.TBCTL.bit.SYNCOSEL = TB_SYNC_DISABLE; // Synchronization Output Select
- EPwm2Regs.TBCTL.bit.HSPCLKDIV = TB_CLK_DIV128; // High Speed Time-base Clock Prescale
- EPwm2Regs.TBCTL.bit.CLKDIV = TB_HSPCLK_DIV10; // Time-base Clock Prescale
- EPwm2Regs.TBCTL.bit.PHSDIR = TB_UP; // Phase Direction
- EPwm2Regs.TBCTL.bit.FREE_SOFT = TB_FREE_RUN; // Emulation Mode Run Mode
- //========================================================================
- // Counter-Compare (CC) Submodule
- //========================================================================
- EPwm2Regs.CMPA.half.CMPA = Pwm2CMPA; // Set compare A value
- //========================================================================
- // Action-Qualifier (AQ) Submodule
- //========================================================================
- EPwm2Regs.AQCTLA.bit.CAU = AQ_CLEAR; // equals CMPA and incrementing.
- EPwm2Regs.AQCTLA.bit.ZRO = AQ_SET; // the counter equals the zero.
- EPwm2Regs.AQSFRC.bit.RLDCSF = AQ_IMMEDI;
- //========================================================================
- // Trip-Zone (TZ) Submodule
- //========================================================================
- EPwm2Regs.TZCTL.bit.TZA = TZ_FORCE_LO; // a trip event occurs output EPWMxA action
- EPwm2Regs.TZCTL.bit.TZB = TZ_FORCE_LO; // a trip event occurs output EPWMxB action
- EDIS;
- }
复制代码
定时器0配置:
//##################################################################
// FILE: TimerFunctions.c
// Build Date: 2021.03.14
// Description: Timer0,Timer1,Timer2
//
//##################################################################
- void Timer0Config(void)
- {
- EALLOW;
- CpuTimer0Regs.TCR.bit.TSS = 1;
- CpuTimer0Regs.TCR.bit.TIF = 1;
- CpuTimer0Regs.PRD.all = SysFreq*100; // 10k for 12MHz
- CpuTimer0Regs.TIM.all = 0;
- CpuTimer0Regs.TCR.bit.TIE = 1;
- CpuTimer0Regs.TCR.bit.TSS = 0;
- EDIS;
- }
复制代码
EQEP模块配置:
//##################################################################
// FILE: EQEPFunctions.c
// Author: daienqiang
// Build Date: 2021.03.14
// Description: eQEP module
//
//##################################################################
- void EQEPConfig()
- {
- #ifdef PulseCap
- EALLOW;
- EQep1Regs.QPOSMAX = 0xFFFFFFFF;
- EQep1Regs.QUPRD = SysFreq*100;
- EQep1Regs.QDECCTL.bit.QSRC = 1; // 0-Quadrature count mode, 1-Direction-count mode
- EQep1Regs.QDECCTL.bit.XCR = 1; // 0-2x resolution, 1-1x resolution
- EQep1Regs.QEPCTL.bit.FREE_SOFT = 3;
- EQep1Regs.QEPCTL.bit.PCRM = 3; // Position counter reset on a unit time event
- EQep1Regs.QEPCTL.bit.QPEN = 1; // eQEP position counter is enabled
- EQep1Regs.QEPCTL.bit.QCLM = 1; // Latch on unit time out
- EQep1Regs.QEPCTL.bit.UTE = 1; // Enable unit timer
- EDIS;
- #endif
- #ifdef MSpeed
- EALLOW;
- EQep1Regs.QPOSMAX = 0xFFFFFFFF;
- EQep1Regs.QUPRD = SysFreq*100;
- EQep1Regs.QDECCTL.bit.QSRC = 0; // 0-Quadrature count mode, 1-Direction-count mode
- EQep1Regs.QDECCTL.bit.XCR = 0; // 0-2x resolution, 1-1x resolution
- EQep1Regs.QEPCTL.bit.FREE_SOFT = 3;
- EQep1Regs.QEPCTL.bit.PCRM = 3; // Position counter reset on a unit time event
- EQep1Regs.QEPCTL.bit.QPEN = 1; // eQEP position counter is enabled
- EQep1Regs.QEPCTL.bit.QCLM = 1; // Latch on unit time out
- EQep1Regs.QEPCTL.bit.UTE = 1; // Enable unit timer
- EDIS;
- #endif
- #ifdef TSpeed_Angle
- EALLOW;
- EQep1Regs.QPOSMAX = 4000;
- EQep1Regs.QUPRD = 6000;
- //EQep1Regs.QPOSCMP = 1;
- EQep1Regs.QDECCTL.bit.QSRC = 0; // 0-Quadrature count mode, 1-Direction-count mode
- EQep1Regs.QDECCTL.bit.XCR = 0; // 0-2x resolution, 1-1x resolution
- EQep1Regs.QEPCTL.bit.FREE_SOFT = 3;
- EQep1Regs.QEPCTL.bit.PCRM = 1; // 0-Position counter reset on an index event, 1-the maximum position 3-on a unit time event
- EQep1Regs.QEPCTL.bit.QPEN = 1; // 0-eQEP position counter is disabled , 1-eQEP position counter is enabled
- EQep1Regs.QEPCTL.bit.QCLM = 1; // 0- Latch on position counter read by CPU, 1-Latch on unit time out
- EQep1Regs.QEPCTL.bit.UTE = 1; // 0-disable unit timer, 1-Enable unit timer
- EQep1Regs.QCAPCTL.bit.UPPS = 0; // QCLK/1
- EQep1Regs.QCAPCTL.bit.CCPS = 0; // SYSCLKOUT/1
- EQep1Regs.QCAPCTL.bit.CEN = 1; // 1-eQEP capture unit is enabled
- EDIS;
- #endif
- }
复制代码
Timer0中断服务程序:在该服务程序中对PWM脉冲进行对应的计算
- void INTERRUPT timer0_isr(void)
- {
- Timer0Counter++;
- #ifdef PulseCap
- PulseNum = EQep1Regs.QPOSLAT;
- PulseDir = EQep1Regs.QEPSTS.bit.QDF;
- #endif
- #ifdef MSpeed
- PulseNum = EQep1Regs.QPOSLAT;
- PulseDir = EQep1Regs.QEPSTS.bit.QDF;
- if(PulseDir==1)
- {
- MotorDir = CLKWISE;
- }
- else{
- MotorDir = ANTICLKWISE;
- PulseNum = 0xFFFFFFFF - PulseNum;
- }
- //MotorSpeed = _IQ17(MotorDir*PulseNum*10000*60/4000); // Q17
- MotorSpeed = MotorDir*PulseNum*10000*60/4000; //
- #endif
- #ifdef TSpeed_Angle
- PulseNum = EQep1Regs.QPOSLAT;
- PulseDir = EQep1Regs.QEPSTS.bit.QDF;
- CapNum = EQep1Regs.QCPRDLAT;
- if(PulseDir==1)
- {
- MotorDir = CLKWISE;
- }
- else{
- MotorDir = ANTICLKWISE;
- }
- MotorSpeed = MotorDir*SysFreq*1000*60/(4*CapNum);
- MotorAngle = PulseNum*360/4000;
- #endif
- CpuTimer0Regs.TCR.bit.TIF = 1;
- PieCtrlRegs.PIEACK.all = PIEACK_GROUP1;
- }
复制代码
3. main函数
- int main(void)
- {
- //
- // Step 1. Initialize System Control:
- //
- InitSysCtrl(); //12MHz
- //
- // Step 2. Initialize GPIO:
- //
- SysGpioInit();
- //
- // Step 3. Clear all interrupts and initialize PIE vector table:
- //
- DINT;
- InitPieCtrl();
- IER_DISABLE(0xffff);
- IFR_DISABLE(0xffff);
- InitPieVectTable();
- EALLOW;
- PieVectTable.TINT0 = &timer0_isr;
- EDIS;
- //
- // Step 4. Initialize all the Device Peripherals:
- //
- // ePWM
- Pwm1CMPA = SysFreq*15000/SetSpeed;
- Pwm2CMPA = 3;
- EPWMConfig();
- // Timer
- Timer0Config();
- //eQEP
- EQEPConfig();
- //
- // Step 5. User specific code, enable interrupts
- //
- Timer0Counter = 0;
- PulseNum = 0;
- PulseDir = 0;
- MotorSpeed = 0;
- PieCtrlRegs.PIEIER1.bit.INTx7 = 1;
- IER_ENABLE(M_INT1);
- EINT; // Enable Global interrupt INTM
- ERTM; // Enable Global realtime interrupt DBGM
- for(;;)
- {
- __asm(" NOP");
- }
- return 0;
- }
复制代码
4. 点击编译、编译完成后点击下载
5. ①PulseCap(单位时间100um内的脉冲个数)测算法
接线 :GPIO0(P203)连接GPIO12(P203)
System.h文件中如下图打开PulseCap,设置SetSpeed为6000
运行结果如下图所示:PulseNum=10,PulseDir=1(正向)
②Mspeed(以分钟为单位的速度)测算法
接线:GPIO0(P203)连接GPIO12(P203);GPIO1(P203)连接GPIO34(P200)
System.h文件中如下图打开Mspeed,设置SetSpeed为6000
运行结果如下图所示:MotorSpeed=6000,MotorDir=1(正向)
③Tspeed_Angle测算法
接线:GPIO0(P203)连接GPIO12(P203);GPIO1(P203)连接GPIO34(P200)
System.h文件中如下图打开Tspeed_Angle,设置SetSpeed为60
运行结果:MotorSpeed=60,MotorDir=1(正向)
如果想要反向的结果,可将GPIO0(P203)连接GPIO34(P200) ,GPIO1(P203)连接GPIO12(P203),那么MotorSpeed = -60,MotorDir = -1。