AnsweredAssumed Answered

Timer interrupt

Question asked by Pierre Lebas on Apr 26, 2016
Latest reply on Apr 29, 2016 by Pierre Lebas

Hello,

 

I'm using a MC9S12XF521 microcontroller.

I want to create a function (timer_1_ms) which configure a timer and enable to counte the timer. After the timer has finished to count-down, an interrupt while set a semaphore in order to go out to the timer_1_ms() function. My problem is about using an IRQ. I don't know my code doesn't work.

 

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

/* Timer 1 ms                                                                                                */

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

void timer_1_ms(void)

{

    /* with Clock_Settings(), the bus' speed is 40MHz */

    blink_led(LED5_MASK);   /* LED5 for indicate that we are in the function */

 

    PITCE_PITCE0 = 0;       /* Le timer0 is disable, so doesn't count  */

   

    timer_1_ms_flag = 0;    /* semaphore used to speak between timer_1_ms() function and the IRQ */

   

    PITCSTP &= ~BIT0_MASK;  /* The timer0 wont wait a bit set of PITFLT register to count again after an overflow */

    PITMTLD0 = 200;                   /* Load of the timer0 8bits at 200 => increment of the 16bits timer0 each 5us */

    PITLD0 = 200;                         /* Load of the timer0 16bits at 200 => flag each 1ms */  

    PITTF_PITTF0 = 1;               /* interrupt flag is clear (just in case) */

    PITINTE_PITINTE0 = 1;        /* Interrup of the  timer0 is enable */

    PITCE_PITCE0 = 1;            /* The timer 0 is enable to count */   

    PITCFLMT_PITE = 1;           /* The PIT module si enable */

    

    while (timer_1_ms_flag != 1){

        LED1 = 0;       /* LED1 ON to show that we are in the loop */

    }

    blink_led(LED2_MASK);           /* LED2 ON to show that we are out of the loop */

}          

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

/* Timer Interrupt Service Routine                                                                */

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

#pragma CODE_SEG __NEAR_SEG NON_BANKED

void interrupt Timer_Isr(void)

{  

    blink_led(LED4_MASK);    /* LED4 ON to show that we are in the timer interrupt routine*/

    if(PITTF_PITTF0 == 1){

      PITCE_PITCE0 = 0;     /* Disable the timer0 */

      PITTF_PITTF0 = 1;     /* Clear the flag */

      blink_led(LED3_MASK);  /* LED3 ON to show that we are in the subroutine*/

      timer_1_ms_flag = 1;  /* semaphore at 1 */

    }

}

#pragma CODE_SEG TIMER_ISR_CODE

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

 

Thank you,

Pierre

Outcomes