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.

Time Based Processing and bitfield

Hello, i trying to this process in my firmware. But doesn't work. Look my code:

#include "msp430g2553.h"


#define LED_0 BIT0
#define LED_1 BIT6
#define LED_OUT P1OUT
#define LED_DIR P1DIR
#define TRUE 1
#define FALSE 0

unsigned int ms=0;
unsigned int ms10=0;
unsigned int ms100=0;
unsigned int segundo=0;

struct time_flags{
    unsigned time_125us : 1;
    unsigned time_1ms :1;
    unsigned time_10ms :1;
    unsigned time_100ms :1;
    unsigned time_1s :1;
}time_flags;

void main_125us_routine (void)
{
    //toggle Red LED
}
void main_1ms_routine (void)
{
    //toggle Red LED
}
void main_10ms_routine (void)
{
    //toggle Red LED
}
void main_100ms_routine (void)
{
    //toggle Red LED
    P1OUT ^= BIT0;
}

void main_1s_routine (void)
{
    //toggle Green LED
    P1OUT ^= BIT6;
}

int main(void) {
    WDTCTL = WDTPW + WDTHOLD; // Desliga Watchdog timer
    //Velocidade de processamento processador
    BCSCTL1 = CALBC1_16MHZ;
    DCOCTL = CALDCO_16MHZ;
    
    //Configuração de pinos I/O
    P1DIR = BIT0 + BIT6; // Define pinos 1.0 e 1.6 como saída (0100 0001)
    P1REN = 0x08; // Habilita pullup/pulldown do pino 1.3 (0000 1000)
    P1OUT = 0x08; // Define pullup para o pino 1.3 (0000 1000)
    P1IE  = 0X08;
    CCTL0 = CCIE; // Habilita interrupção de comparação do timer A
    
    //Configuração do timer
    TACTL = TASSEL_2+MC_3+ID_3; // SMCLK = 16 MHz, SMCLK/8 = 2 MHZ (0.5 us)
    CCR0 = 125;
    // para zero, portanto cada interrupção acontece// 2 * 125 * 0.5us = 0.000125 seconds
    
    //Habilita interrupção
    _BIS_SR(GIE); //enable global interrupts
    
    //Laço principal
    while(1) //idle loop
    {
        if(time_flags.time_125us == TRUE)
        {
            main_125us_routine();
            time_flags.time_125us = FALSE;
        }
        if(time_flags.time_1ms == TRUE)
        {
            main_1ms_routine();
            time_flags.time_1ms = FALSE;
        }
        if(time_flags.time_10ms == TRUE)
        {
            main_10ms_routine();
            time_flags.time_10ms = FALSE;
        }
        if(time_flags.time_100ms == TRUE)
        {
            main_100ms_routine();
            time_flags.time_100ms = FALSE;
        }
        if(time_flags.time_1s == TRUE)
        {
            main_1s_routine();
            time_flags.time_1s = FALSE;
        }
        return 0;
    }
}

#pragma vector=PORT1_VECTOR
__interrupt void Port_1(void)
{
    return;
}

// Timer A0 interrupt service routine
#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0(void) {
    ms++;
    ms10++;
    ms100++;
    segundo++;
    //125us
    time_flags.time_125us = TRUE;
    
    if(ms==8){
        //1ms
        time_flags.time_1ms = TRUE;
        ms=0;
    }
    if(ms10==80){
        //10ms
        time_flags.time_10ms = TRUE;
        ms10=0;
    }
    if(ms100==800){
        //100ms
        time_flags.time_100ms = TRUE;
        ms100=0;
    }
    if(segundo==8000){
      //1sec
        time_flags.time_1s = TRUE;
        segundo=0;
    }
    return;
}

  • In what way does it "not work"? Is there a compile error, or does it not do what you were expecting at runtime? If the latter, what do you expect to happen and what did you observe happening when you ran the code?

  • - Your 'return 0;' statement inside while(1) loop looks suspicious. Is your intent to run loop contents once only or to do it periodically?

    - Your ms, ms10, ms100, segundo, time_flags variables must be declared as 'volatile' as they get modified inside TIMER_A0 ISR.

  • You need to remove the return 0; statement. In addition, inside the while(1){...} loop, I would place as the first statement code to place the CPU into LPM1. Then in ISR at end a statement to exit LPM1. (you can search how to do that, plenty of examples on forum/google).

**Attention** This is a public forum