How to preform correct delay system using TIMER_A on the TEXAS MSP432?

68 Views Asked by At

I am trying to make a delay system based in timer on msp432, to exclude the many loops I have in my system. Now I am facing a trouble, in my system i need to inplement function to, second's, millisecond's and microsecond's. I started by define a clock sytem to 24Mhz, divided by to for the SMCLK, which gives a 12Mhz clock. In the "TIMER_Initialize" function i give the needed configuration, at the end enable a NVIC interruption.

For handler on this interruption TIMER I creat a switch where using the desired "enum type def" ,i can use each case, called by the proprelly function. This way I can get inside de desired case in the interruption propriety. Once i use a 16bit timer I want each time the TIMER reachs the overflow, it increments until get the desired delay, after that it clear the counter and the used funtion. Below I share the essential part of the code with some hope someone can help me.


#include "msp432p401r.h"
#include <stdio.h>
#include "cs.h"
#include "gpio.h"


/*DEFINE ENUMERATION TIMER*/
typedef enum{ _TASK_uMICROSECOND = 0, _TASK_MILLISECONDS, _TASK_SECONDS} TIMER_;
volatile TIMER_ delayTime = _TASK_uMICROSECOND;

volatile uint32_t timerOverflow = 0;

void init_LED(void);
void TIMER_Initialize(void);
/*************SIMPLE CLOCK AND DELAY USING TIMMER**************
* Timer Period = (Clock Period * ID * IDEX * Timer Counts) - 1*
* SMCLK = 12MHz/4  = 3MHz                                    *
* CLOCK PERIOD = 1 / CLOCK SYSTEM => CLOCK PERIOD = 166,67ns  *
* Timer Period = (Clock Freq / Prescaler) / D. Interrupt Freq *
* Timer OVERFLOW < COUNT REGISTER (2^16 - 1 = 65535)          s*
*
*/
/*SMCLK = 12MHz/4  = 3MHz*/
/*T = 1/F=> 1/1MHz = 1us*/
/*TIMERClock T=4/6MHz = 666.7ns */

void  delaySeconds(int sec);
void  delayMiliseconds(int msec);
void  delayMicroseconds(int usec);

void main(void){
    WDTCTL = WDTPW + WDTHOLD;

    init_LED();

    GPIOInterface_initGPIO();

    GPIOInterface_initClocks();                           /*CLOCK SOURCE*/

    TIMER_Initialize();                                   /*TIMER INITIALIZATION FUNCTION*/

    __enable_irq();

    while(1){

        delaySeconds(5);
    }
}


void TIMER_Initialize(void){

    TIMER_A0->CCTL[0]&= ~TIMER_A_CTL_CLR;                  /*TIMER A CLEAR */

    TIMER_A0->CTL     = TIMER_A_CTL_MC__CONTINUOUS  |      /*TIMER COUNTINUOUS*/
                        TIMER_A_CTL_SSEL__SMCLK     |      /*SMCLK*/
                        TIMER_A_CTL_ID_2            ;      /*TIMER A DIVISION BY 4 (12 000 000/4 = 3 000 000)*/

    TIMER_A0->EX0    |= TIMER_A_EX0_TAIDEX_2;              /* 3 000 000/3 = 1 000 000*/

    TIMER_A0->CCTL[0] = TIMER_A_CCTLN_CCIE ;               /*LOCAL ENABLE OVERFLOW IN TIMER A0*/

    /*SET NVIC INTERRUPTION*/
    NVIC->ISER[0] = 1 << ((TA0_0_IRQn) & 31);
}

/*TIMER A0 INTERRUPT SERVICE ROUTINE*/
void TA0_0_IRQHandler(void){
    timerOverflow++;


    switch(delayTime){
        /*uMICROSECOND*/
        case 0:
        break;

        /*MILLISECOND*/
        case 1:
        break;

        /*SECOND*/
        case 2:
            P1->OUT ^= BIT0;                               /*TOOGLE LED TO SHOW */
        break;
    }

    TIMER_A0->CCTL[0] &= ~(TIMER_A_CCTLN_CCIFG);           /*CLEAR FLAG*/

}


void init_LED(void){

    P1->DIR |= BIT0;                       /*SET P1.0 TO OUTPUT*/
    P2->DIR |= BIT1;                       /*SET P2.1 TO OUTPUT*/
    P2->DIR |= BIT2;                       /*SET P2.2 TO OUTPUT*/
    P2->DIR |= BIT3;                       /*SET P2.3 TO OUTPUT*/

    P1->OUT &= ~(BIT0);                    /*SET P1.0 TO LOW*/
    P2->OUT &= ~(BIT1);                    /*SET P2.1 TO LOW*/
    P2->OUT &= ~(BIT2);                    /*SET P2.2 TO LOW*/
    P2->OUT &= ~(BIT3);                    /*SET P2.3 TO LOW*/
}

    void  delaySeconds(int sec){}
    void  delayMiliseconds(int msec){}
    void  delayMicroseconds(int usec){}

Thanks in advance

I try to implement a code, I expecting some help to change the while loops by delay functions

0

There are 0 best solutions below