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.

Data manipulation on msp430g2553

I have configured a the timer A0 to increment a variable every time its interrupt occurs. However the variable has not been increased, even when I do it out of the timer vector routine. This part of the program is described below:

The variable which I would like to increase is the "accddd" one, but it keeps its value on "0".

#include <io430g2553.h>
#include <intrinsics.h>
#include <stdio.h>
#define FCLOCK 1000000L

unsigned int i = 0, flag_rx = 0, vetor_rx[3], received_data = 0, flag_dist = 0, sensor = 0, accddd = 0;

int main(void)
{
 
  // Stop watchdog timer to prevent time out reset
  WDTCTL = WDTPW + WDTHOLD;
  P1DIR |= 0x57;
  P2DIR |= 0x20;
  P1IE |= 0xA8;
  P1IES |= 0xA8;
  P1IFG &= ~0xA8;
  P2IE |= 0x1F;
  P2IES |= 0x1F;
  P2IFG &=~ 0x1F;
 
  TA0CTL = TASSEL1 | MC0 | TAIE;
  TACCR0 = 50000;               //Timer = 50ms for 1MHz of clock
  TA0CCTL0 = CCIE;               //
  TA0CCTL1 = CCIE;               //
 
      led1 = 0;
      led2 = 0;
      RELE = 0;
      
      spi_init();
      
      __enable_interrupt();
      
     // __bis_SR_register(GIE);
      
      program();
}

////////////////////////////////////////////////////////////////////////////////

void program (void)
{
 
  while(1)
  {
      spi_transmit(0x22);
    
      if(flag_rx >= 1)
      {
          switch(vetor_rx[0])
          {
              case 0xAA:    if(vetor_rx[1] == ADDRESS)     
                            {   
                                vetor_rx[2] = received_data;   
                                flag_rx = 0; flag_dist = 0; RELE = 0;
                            }
                            break;
            default:    break;
          }
      }
      
      if(flag_dist >= 1)
      {
          varre_sens();
          __delay_cycles(50000);                //50ms
      }
    
      if((accddd >= 300) & (flag_dist == 0))
      {
        flag_dist = 1;
        //led1 = 1;
      }
      else{
        
          if((accddd >= 600) & (flag_dist == 1))
          {
            RELE = 1;
            accddd = 0;
          }
      }
  }
}

////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////

#pragma vector = TIMER0_A0_VECTOR

__interrupt void TA0_ISR(void)
{
    accddd = accddd + 1;
    //TACCR0 = 50000;               //Timer = 50ms for 1MHz of clock
}

////////////////////////////////////////////////////////////////////////////////

  • 1) Declare accddd as "volatile".

    2) Delete the line "TA0CCTL1 = CCIE;". Your program isn't using TA0CCR1 for anything, and without a corresponding TIMER0_A1_VECTOR your program will just reset over and over.

  • Thanks for your help ! I have changed the program, however I am still facing the same problem. I have changed the name of the variable to "acc". As you can see I expect the flag_dist is set to "1" after 15 seconds(50ms x 300). The program is like this now:

    unsigned int i = 0, flag_rx = 0, vetor_rx[3], received_data = 0, flag_dist = 0, sensor = 0;
    volatile unsigned int acc = 0;

    ////////////////////////////////////////////////////////////////////////////////

    int main(void)
    {
     
      // Stop watchdog timer to prevent time out reset
      WDTCTL = WDTPW + WDTHOLD;
      P1DIR |= 0x57;
      P2DIR |= 0x20;
      P1IE |= 0xA8;
      P1IES |= 0xA8;
      P1IFG &= ~0xA8;
      P2IE |= 0x1F;
      P2IES |= 0x1F;
      P2IFG &=~ 0x1F;
     
      TA0CTL = TASSEL1 | MC0 | TAIE;
      TACCR0 = 50000;               //Timer = 50ms for 1MHz of clock
      TA0CCTL0 = CCIE;              //
     
          led1 = 0;
          led2 = 0;
          RELE = 0;
          
          spi_init();
          
          __enable_interrupt();
          
          program();
    }

    ////////////////////////////////////////////////////////////////////////////////

    void program (void)
    {
     
      while(1)
      {
          spi_transmit(0x22);
        
          if(flag_rx >= 1)
          {
              switch(vetor_rx[0])
              {
                  case 0xAA:    if(vetor_rx[1] == ADDRESS)     
                                {   
                                    vetor_rx[2] = received_data;   
                                    flag_rx = 0; flag_dist = 0; RELE = 0;
                                }
                                break;
                default:    break;
              }
          }
          
          if(flag_dist >= 1)
          {
              varre_sens();
              __delay_cycles(50000);                //50ms
          }
        
          if((acc >= 300) & (flag_dist == 0))
          {
            flag_dist = 1;
            led1 = 1;
          }
          else{
            
              if((acc >= 600) & (flag_dist == 1))
              {
                RELE = 1;
                acc = 0;
              }
          }
      }
    }

    ////////////////////////////////////////////////////////////////////////////////

    ////////////////////////////////////////////////////////////////////////////////

    #pragma vector = TIMER0_A0_VECTOR       //Vetor da interrupção por estouro do timer A0

    __interrupt void TA0_ISR(void)
    {
        acc++;                //Incrementa variável acc
    }

    ////////////////////////////////////////////////////////////////////////////////

  • > TA0CTL = TASSEL1 | MC0 | TAIE;

    Oops, I missed this the first time. Get rid of TAIE (same reason as (2) above).

  • Thank you so much Bruce ! Now it is working as expected.

    Att.

    Jefferson Albuquerque

**Attention** This is a public forum