This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

PWM with interrupt not working

I need to interrupt every 10ms when the PWM counter is empty (I'm counting down). I have Tiva c series TM4C123G. Can not get to the interrupt handler.

Thank you for your solution.

Here is the code:

#define PWM_FREQUENCY 100

unsigned int arg_sinus_dt=0;

void PWM1IntHandler(void)

{

  PWMGenIntClear(PWM1_BASE,PWM_GEN_0,PWM_INT_CNT_ZERO);

  arg_sinus_dt=arg_sinus_dt+1;

}

 

int main(void) {

volatile unsigned int max;

volatile unsigned char compare;

compare = 83;


SysCtlClockSet (SYSCTL_SYSDIV_5 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN| SYSCTL_XTAL_16MHZ);

SysCtlPWMClockSet(SYSCTL_PWMDIV_64);

SysCtlPeripheralEnable(SYSCTL_PERIPH_PWM1);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOD);

GPIOPinTypePWM(GPIO_PORTD_BASE, GPIO_PIN_0);

GPIOPinConfigure(GPIO_PD0_M1PWM0);   

PWMClock =sysCtlClockGet() / 64;

max = (PWMClock / PWM_FREQUENCY) - 1;

PWMGenConfigure(PWM1_BASE, PWM_GEN_0, PWM_GEN_MODE_DOWN); 

PWMGenPeriodSet(PWM1_BASE, PWM_GEN_0, max);

PWMPulseWidthSet(PWM1_BASE, PWM_OUT_0, compare * max / 1000);

PWMOutputState(PWM1_BASE, PWM_OUT_0_BIT, true);

PWMGenEnable(PWM1_BASE, PWM_GEN_0);

 

is this correct?

PWMIntEnable(PWM1_BASE, PWM_GEN_0);

PWMGenIntTrigEnable(PWM1_BASE, PWM_GEN_0, PWM_INT_CNT_ZERO);

while(1){}

}

The've defined the vector table in startup_ccs.c PWM1IntHandler (PWM1 Generator 0)and also the function extern void PWM1IntHandler (void).

 

  • pavel T���borsk��� said:

    PWMIntEnable(PWM1_BASE, PWM_GEN_0);

    After your viewer/helper rods/cones restore - answer is No!  (unwise to "camouflage" that which requires analysis - you must have noted the horror of your highlight...)

    Proper (both code and viewer wise) is: PWMIntEnable(PWM1_BASE, PWM_INT_GEN_0);

    You then add PWMGenIntTrigEnable() - but give no clue as to your motivation/intent.  This function is not strictly required to achieve your sought interrupt - superfluous to issue at hand.

    If we assume your start-up file properly includes your PWM1 interrupt handler - both by its declaration and then inclusion w/in the long, vector table entry space - you should be, "good to go." 

    The selection of PWM1 - rather than far more "vanilla" PWM0 - requires extra vigilance both in set-up/config and while debugging.   PWM1 appears "deeper" (further down) in Register reporting - and often enough the earlier appearing PWM0 will "grab" your attention...  (not to ask - "how I know?...") 

  •  

    thank you but it still does not interrupt. I used just PWMIntEnable (PWM1_BASE, PWM_INT_GEN_0);

     

    in my start-up:

     

    // External declaration for the reset handler that is to be called when the

    // processor is started

    //

    //*****************************************************************************

    extern void _c_int00(void);

    extern void PWM1IntHandler(void);  //Here I add this function. Ok ?

     

    // The vector table.

    #pragma DATA_SECTION(g_pfnVectors, ".intvecs")

    void (* const g_pfnVectors[])(void) =

    {

    (void (*)(void))((unsigned long)&__STACK_TOP),

     ResetISR,                             // The reset handler

    .

    .

    .

     IntDefaultHandler,                     // PWM Generator 0 // is  here  for PWM0

     IntDefaultHandler,                     // PWM Generator 1

     IntDefaultHandler,                     // PWM Generator 2

    .

    .

    .

    PWM1IntHandler,                 // PWM 1 Generator 0  Here I rewrote value. is this ok ?

    IntDefaultHandler,                // PWM 1 Generator 1

    IntDefaultHandler,                // PWM 1 Generator 2

    IntDefaultHandler,                // PWM 1 Generator 3

    IntDefaultHandler                  // PWM 1 Fault

    };

    PWM1 and PWM0 are not the same PWM modules ?

     

  • pavel T���borsk��� said:

    PWM1 and PWM0 are not the same PWM modules ?

    No they are not - and your use of PWM1 w/out compelling reason complicates.  (as I've earlier stated)

    Your declaration & addition w/in vector table look, "good for gov't work."

    Look @ our successful PWM code:  (note - I'm too jammed to change to your PWM1)

        ROM_PWMIntEnable(PWM0_BASE, PWM_INT_GEN_0);    
        
        ROM_IntEnable(INT_PWM0_0);   

    Appears you've "missed" the (proper i.e. readable) highlight, above.  Both PWMIntEnable() & IntEnable() are required!

    Your interrupt handler should be correct - if indeed placed as you present - and not "duplicated" at/around PWM 0...

  • Thank you it works. It is necessary to have another function.

     

    PWMIntEnable(PWM0_BASE, PWM_INT_GEN_0);

    IntEnable(INT_PWM0_0);

    PWMGenIntTrigEnable(PWM0_BASE, PWM_GEN_0, PWM_INT_CNT_ZERO);

    is call interruption when the counter is zero? or can be set to different value (PWM_INT_CNT_LOAD or WM_INT_CNT_AU...)

     

  • pavel T���borsk��� said:
    Thank you it works.

    Mon ami - it is "normal/customary" for requesting posters (you)  to tick, "Verify Answer" found atop responding poster's correct diagnosis.  (such facilitates, "time/effort saving for valued vendor personnel" - and serves as "guide" to new posters - searching for similar info)   

    And now you bring, "new/further issue" to this quest...

    Yes - you may trigger such PWM interrupt via other means - SW-DRL-UGxxxx and your MCU manual provide great detail - surely, "PWM_INT_CNT_LOAD" will satisfy...