GPIO_setPullUp(myGpio, GPIO_Number_4, GPIO_PullUp_Disable);
GPIO_setPullUp(myGpio, GPIO_Number_5, GPIO_PullUp_Disable);
GPIO_setMode(myGpio, GPIO_Number_4, GPIO_4_Mode_EPWM3A);
GPIO_setMode(myGpio, GPIO_Number_5, GPIO_5_Mode_EPWM3B);
// Setup a debug vector table and enable the PIE
PIE_setDebugIntVectorTable(myPie);
PIE_enable(myPie);
// Register interrupt handlers in the PIE vector table
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_3, PIE_SubGroupNumber_4, (intVec_t)&epwm4_isr);
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_3, PIE_SubGroupNumber_1, (intVec_t)&epwm1_isr);
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_3, PIE_SubGroupNumber_2, (intVec_t)&epwm2_isr);
PIE_registerPieIntHandler(myPie, PIE_GroupNumber_3, PIE_SubGroupNumber_3, (intVec_t)&epwm3_isr);
CLK_disableTbClockSync(myClk);
InitEPwm4Example();
InitEPwm1Example();
InitEPwm2Example();
InitEPwm3Example();
CLK_enableTbClockSync(myClk);
// Enable CPU INT3 which is connected to EPWM1-3 INT:
CPU_enableInt(myCpu, CPU_IntNumber_3);
// Enable EPWM INTn in the PIE: Group 3 interrupt 1-3
PIE_enablePwmInt(myPie, PWM_Number_4);
PIE_enablePwmInt(myPie, PWM_Number_1);
PIE_enablePwmInt(myPie, PWM_Number_2);
PIE_enablePwmInt(myPie, PWM_Number_3);
// Enable global Interrupts and higher priority real-time debug events:
CPU_enableGlobalInts(myCpu);
CPU_enableDebugInt(myCpu);
for(;;) {
asm(" NOP");
}
}
interrupt void epwm4_isr(void)
{
// Update the CMPA and CMPB values
update_compare(&epwm4_info);
// Clear INT flag for this timer
PWM_clearIntFlag(myPwm4);
// Acknowledge this interrupt to receive more interrupts from group 3
PIE_clearInt(myPie, PIE_GroupNumber_3);
}
interrupt void epwm1_isr(void)
{
count1++;
if(count1%6==0)
{
count1 = 0;
// Update the CMPA and CMPB values
update_compare(&epwm1_info);
}
// Clear INT flag for this timer
PWM_clearIntFlag(myPwm1);
// Acknowledge this interrupt to receive more interrupts from group 3
PIE_clearInt(myPie, PIE_GroupNumber_3);
}
interrupt void epwm2_isr(void)
{
count2 ++;
if(count2%3==0)
{
count2 = 0;
// Update the CMPA and CMPB values
update_compare(&epwm2_info);
}
// Clear INT flag for this timer
PWM_clearIntFlag(myPwm2);
// Acknowledge this interrupt to receive more interrupts from group 3
PIE_clearInt(myPie, PIE_GroupNumber_3);
}
interrupt void epwm3_isr(void)
{
count3 ++;
if(count3%10==0)
{
count3 = 0;
// Update the CMPA and CMPB values
update_compare(&epwm3_info);
}
// Clear INT flag for this timer
PWM_clearIntFlag(myPwm3);
// Acknowledge this interrupt to receive more interrupts from group 3
PIE_clearInt(myPie, PIE_GroupNumber_3);
}
// Setup TBCLK
PWM_setPeriod(myPwm1, EPWM1_TIMER_TBPRD); // Set timer period 801 TBCLKs
PWM_setPhase(myPwm1, 0x0000); // Phase is 0
PWM_setCount(myPwm1, 0x0000); // Clear counter
// Set Compare values
PWM_setCmpA(myPwm1, EPWM1_MIN_CMPA); // Set compare A value
PWM_setCmpB(myPwm1, EPWM1_MIN_CMPB); // Set Compare B value
// Setup counter mode
PWM_setCounterMode(myPwm1, PWM_CounterMode_UpDown); // Count up
PWM_disableCounterLoad(myPwm1); // Disable phase loading
PWM_setHighSpeedClkDiv(myPwm1, PWM_HspClkDiv_by_1); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm1, PWM_ClkDiv_by_1);
// Setup shadowing
PWM_setShadowMode_CmpA(myPwm1, PWM_ShadowMode_Shadow);
PWM_setShadowMode_CmpB(myPwm1, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm1, PWM_LoadMode_Zero);
PWM_setLoadMode_CmpB(myPwm1, PWM_LoadMode_Zero);
// Set actions
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm1, PWM_ActionQual_Set); // Set PWM1A on event A, up count
PWM_setActionQual_CntDown_CmpA_PwmA(myPwm1, PWM_ActionQual_Clear); // Clear PWM1A on event A, down count
PWM_setActionQual_CntUp_CmpB_PwmB(myPwm1, PWM_ActionQual_Set); // Set PWM1B on event B, up count
PWM_setActionQual_CntDown_CmpB_PwmB(myPwm1, PWM_ActionQual_Clear); // Clear PWM1B on event B, down count
// Interrupt where we will change the Compare Values
PWM_setIntMode(myPwm1, PWM_IntMode_CounterEqualZero); // Select INT on Zero event
PWM_enableInt(myPwm1); // Enable INT
PWM_setIntPeriod(myPwm1, PWM_IntPeriod_ThirdEvent); // Generate INT on 3rd event
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
epwm1_info.EPwm_CMPA_Direction = EPWM_CMP_UP; // Start by increasing CMPA &
epwm1_info.EPwm_CMPB_Direction = EPWM_CMP_DOWN; // decreasing CMPB
epwm1_info.EPwmTimerIntCount = 0; // Zero the interrupt counter
epwm1_info.myPwmHandle = myPwm1; // Set the pointer to the ePWM module
epwm1_info.EPwmMaxCMPA = EPWM1_MAX_CMPA; // Setup min/max CMPA/CMPB values
epwm1_info.EPwmMinCMPA = EPWM1_MIN_CMPA;
epwm1_info.EPwmMaxCMPB = EPWM1_MAX_CMPB;
epwm1_info.EPwmMinCMPB = EPWM1_MIN_CMPB;
}
void InitEPwm2Example()
{
CLK_enablePwmClock(myClk, PWM_Number_2);
// Setup TBCLK
PWM_setPeriod(myPwm2, EPWM2_TIMER_TBPRD); // Set timer period 801 TBCLKs
PWM_setPhase(myPwm2, 0x0000); // Phase is 0
PWM_setCount(myPwm2, 0x0000); // Clear counter
// Set Compare values
PWM_setCmpA(myPwm2, EPWM2_MIN_CMPA); // Set compare A value
PWM_setCmpB(myPwm2, EPWM2_MIN_CMPB); // Set Compare B value
// Setup counter mode
PWM_setCounterMode(myPwm2, PWM_CounterMode_UpDown); // Count up
PWM_disableCounterLoad(myPwm2); // Disable phase loading
PWM_setHighSpeedClkDiv(myPwm2, PWM_HspClkDiv_by_1); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm2, PWM_ClkDiv_by_1);
// Setup shadowing
PWM_setShadowMode_CmpA(myPwm2, PWM_ShadowMode_Shadow);
PWM_setShadowMode_CmpB(myPwm2, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm2, PWM_LoadMode_Zero);
PWM_setLoadMode_CmpB(myPwm2, PWM_LoadMode_Zero);
// Set actions
PWM_setActionQual_CntUp_CmpA_PwmA(myPwm2, PWM_ActionQual_Set); // Set PWM2A on event A, up count
PWM_setActionQual_CntDown_CmpB_PwmA(myPwm2, PWM_ActionQual_Clear); // Clear PWM2A on event B, down count
PWM_setActionQual_Zero_PwmB(myPwm2, PWM_ActionQual_Set); // Clear PWM2B on zero
PWM_setActionQual_Period_PwmB(myPwm2, PWM_ActionQual_Clear); // Set PWM2B on period
// Interrupt where we will change the Compare Values
PWM_setIntMode(myPwm2, PWM_IntMode_CounterEqualZero); // Select INT on Zero event
PWM_enableInt(myPwm2); // Enable INT
PWM_setIntPeriod(myPwm2, PWM_IntPeriod_ThirdEvent); // Generate INT on 3rd event
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
epwm2_info.EPwm_CMPA_Direction = EPWM_CMP_UP; // Start by increasing CMPA &
epwm2_info.EPwm_CMPB_Direction = EPWM_CMP_UP; // increasing CMPB
epwm2_info.EPwmTimerIntCount = 0; // Zero the interrupt counter
epwm2_info.myPwmHandle = myPwm2; // Set the pointer to the ePWM module
epwm2_info.EPwmMaxCMPA = EPWM2_MAX_CMPA; // Setup min/max CMPA/CMPB values
epwm2_info.EPwmMinCMPA = EPWM2_MIN_CMPA;
epwm2_info.EPwmMaxCMPB = EPWM2_MAX_CMPB;
epwm2_info.EPwmMinCMPB = EPWM2_MIN_CMPB;
}
void InitEPwm3Example(void)
{
CLK_enablePwmClock(myClk, PWM_Number_3);
// Setup TBCLK
PWM_setCounterMode(myPwm3, PWM_CounterMode_UpDown); // Count up/down
PWM_setPeriod(myPwm3, EPWM3_TIMER_TBPRD); // Set timer period
PWM_disableCounterLoad(myPwm3); // Disable phase loading
PWM_setPhase(myPwm3, 0x0000); // Phase is 0
PWM_setCount(myPwm3, 0x0000); // Clear counter
PWM_setHighSpeedClkDiv(myPwm3, PWM_HspClkDiv_by_1); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm3, PWM_ClkDiv_by_1);
// Setup shadow register load on ZERO
PWM_setShadowMode_CmpA(myPwm3, PWM_ShadowMode_Shadow);
PWM_setShadowMode_CmpB(myPwm3, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm3, PWM_LoadMode_Zero);
PWM_setLoadMode_CmpB(myPwm3, PWM_LoadMode_Zero);
// Set Compare values
PWM_setCmpA(myPwm3, EPWM3_MIN_CMPA); // Set compare A value
PWM_setCmpB(myPwm3, EPWM3_MIN_CMPB); // Set Compare B value
// Set Actions
PWM_setActionQual_Period_PwmA(myPwm3, PWM_ActionQual_Set); // Set PWM3A on period
PWM_setActionQual_CntDown_CmpB_PwmA(myPwm3, PWM_ActionQual_Clear); // Clear PWM3A on event B, down count
PWM_setActionQual_Period_PwmB(myPwm3, PWM_ActionQual_Clear); // Clear PWM3A on period
PWM_setActionQual_CntUp_CmpA_PwmB(myPwm3, PWM_ActionQual_Set); // Set PWM3A on event A, up count
// Interrupt where we will change the Compare Values
PWM_setIntMode(myPwm3, PWM_IntMode_CounterEqualZero); // Select INT on Zero event
PWM_enableInt(myPwm3); // Enable INT
PWM_setIntPeriod(myPwm3, PWM_IntPeriod_ThirdEvent); // Generate INT on 3rd event
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
epwm3_info.EPwm_CMPA_Direction = EPWM_CMP_UP; // Start by increasing CMPA &
epwm3_info.EPwm_CMPB_Direction = EPWM_CMP_DOWN; // decreasing CMPB
epwm3_info.EPwmTimerIntCount = 0; // Zero the interrupt counter
epwm3_info.myPwmHandle = myPwm3; // Set the pointer to the ePWM module
epwm3_info.EPwmMaxCMPA = EPWM3_MAX_CMPA; // Setup min/max CMPA/CMPB values
epwm3_info.EPwmMinCMPA = EPWM3_MIN_CMPA;
epwm3_info.EPwmMaxCMPB = EPWM3_MAX_CMPB;
epwm3_info.EPwmMinCMPB = EPWM3_MIN_CMPB;
}
void InitEPwm4Example(void)
{
CLK_enablePwmClock(myClk, PWM_Number_4);
// Setup TBCLK
PWM_setCounterMode(myPwm4, PWM_CounterMode_UpDown); // Count up/down
PWM_setPeriod(myPwm4, EPWM4_TIMER_TBPRD); // Set timer period
PWM_disableCounterLoad(myPwm4); // Disable phase loading
PWM_setPhase(myPwm4, 0x0000); // Phase is 0
PWM_setCount(myPwm4, 0x0000); // Clear counter
PWM_setHighSpeedClkDiv(myPwm4, PWM_HspClkDiv_by_1); // Clock ratio to SYSCLKOUT
PWM_setClkDiv(myPwm4, PWM_ClkDiv_by_1);
// Setup shadow register load on ZERO
PWM_setShadowMode_CmpA(myPwm4, PWM_ShadowMode_Shadow);
PWM_setShadowMode_CmpB(myPwm4, PWM_ShadowMode_Shadow);
PWM_setLoadMode_CmpA(myPwm4, PWM_LoadMode_Zero);
PWM_setLoadMode_CmpB(myPwm4, PWM_LoadMode_Zero);
// Set Compare values
PWM_setCmpA(myPwm4, EPWM4_MIN_CMPA); // Set compare A value
PWM_setCmpB(myPwm4, EPWM4_MIN_CMPB); // Set Compare B value
// Set Actions
PWM_setActionQual_Period_PwmA(myPwm4, PWM_ActionQual_Set); // Set PWM3A on period
PWM_setActionQual_CntDown_CmpB_PwmA(myPwm4, PWM_ActionQual_Clear); // Clear PWM3A on event B, down count
PWM_setActionQual_Period_PwmB(myPwm4, PWM_ActionQual_Clear); // Clear PWM3A on period
PWM_setActionQual_CntUp_CmpA_PwmB(myPwm4, PWM_ActionQual_Set); // Set PWM3A on event A, up count
// Interrupt where we will change the Compare Values
PWM_setIntMode(myPwm4, PWM_IntMode_CounterEqualZero); // Select INT on Zero event
PWM_enableInt(myPwm4); // Enable INT
PWM_setIntPeriod(myPwm4, PWM_IntPeriod_ThirdEvent); // Generate INT on 3rd event
// Information this example uses to keep track
// of the direction the CMPA/CMPB values are
// moving, the min and max allowed values and
// a pointer to the correct ePWM registers
epwm4_info.EPwm_CMPA_Direction = EPWM_CMP_UP; // Start by increasing CMPA &
epwm4_info.EPwm_CMPB_Direction = EPWM_CMP_DOWN; // decreasing CMPB
epwm4_info.EPwmTimerIntCount = 0; // Zero the interrupt counter
epwm4_info.myPwmHandle = myPwm4; // Set the pointer to the ePWM module
epwm4_info.EPwmMaxCMPA = EPWM4_MAX_CMPA; // Setup min/max CMPA/CMPB values
epwm4_info.EPwmMinCMPA = EPWM4_MIN_CMPA;
epwm4_info.EPwmMaxCMPB = EPWM4_MAX_CMPB;
epwm4_info.EPwmMinCMPB = EPWM4_MIN_CMPB;
}
void update_compare(EPWM_INFO *epwm_info)
{
// Every 10'th interrupt, change the CMPA/CMPB values
if(epwm_info->EPwmTimerIntCount == 10) {
epwm_info->EPwmTimerIntCount = 0;
// If we were increasing CMPA, check to see if
// we reached the max value. If not, increase CMPA
// else, change directions and decrease CMPA
if(epwm_info->EPwm_CMPA_Direction == EPWM_CMP_UP) {
if(PWM_getCmpA(epwm_info->myPwmHandle) < epwm_info->EPwmMaxCMPA) {
PWM_setCmpA(epwm_info->myPwmHandle, PWM_getCmpA(epwm_info->myPwmHandle) + 1);
}
else {
epwm_info->EPwm_CMPA_Direction = EPWM_CMP_DOWN;
PWM_setCmpA(epwm_info->myPwmHandle, PWM_getCmpA(epwm_info->myPwmHandle) - 1);
}
}
// If we were decreasing CMPA, check to see if
// we reached the min value. If not, decrease CMPA
// else, change directions and increase CMPA
else {
if(PWM_getCmpA(epwm_info->myPwmHandle) == epwm_info->EPwmMinCMPA) {
epwm_info->EPwm_CMPA_Direction = EPWM_CMP_UP;
PWM_setCmpA(epwm_info->myPwmHandle, PWM_getCmpA(epwm_info->myPwmHandle) + 1);
}
else {
PWM_setCmpA(epwm_info->myPwmHandle, PWM_getCmpA(epwm_info->myPwmHandle) - 1);
}
}
// If we were increasing CMPB, check to see if
// we reached the max value. If not, increase CMPB
// else, change directions and decrease CMPB
if(epwm_info->EPwm_CMPB_Direction == EPWM_CMP_UP) {
if(PWM_getCmpB(epwm_info->myPwmHandle) < epwm_info->EPwmMaxCMPB) {
PWM_setCmpB(epwm_info->myPwmHandle, PWM_getCmpB(epwm_info->myPwmHandle) + 1);
}
else {
epwm_info->EPwm_CMPB_Direction = EPWM_CMP_DOWN;
PWM_setCmpB(epwm_info->myPwmHandle, PWM_getCmpB(epwm_info->myPwmHandle) - 1);
}
}
// If we were decreasing CMPB, check to see if
// we reached the min value. If not, decrease CMPB
// else, change directions and increase CMPB
else {
if(PWM_getCmpB(epwm_info->myPwmHandle) == epwm_info->EPwmMinCMPB) {
epwm_info->EPwm_CMPB_Direction = EPWM_CMP_UP;
PWM_setCmpB(epwm_info->myPwmHandle, PWM_getCmpB(epwm_info->myPwmHandle) + 1);
}
else {
PWM_setCmpB(epwm_info->myPwmHandle, PWM_getCmpB(epwm_info->myPwmHandle) - 1);
}
}
}
else {
epwm_info->EPwmTimerIntCount++;
}
return;
}