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.

MSP430G2553: timerA instead of delay function

Part Number: MSP430G2553


Hi

I am working on small lift in my code i used delay cycles but i want to use timerA.And tried something but it didn't work.

#include "msp430g2553.h"


#define SW1    BIT3
#define SW2    BIT4
#define SW3    BIT5
#define SEG_A   BIT0
#define SEG_B   BIT1
#define SEG_C   BIT2
#define SEG_D   BIT3
const unsigned int digits[5] = {SEG_A, SEG_B ,SEG_C, SEG_D};
#define DMASK   ~(SEG_A + SEG_B + SEG_C + SEG_D)
volatile unsigned int x;

int currentposition;
int a;
int b;
int c;
int t;
int language;
int nextposition;
void go_up(void);
void go_down(void);


void main(void)
{
    currentposition = 2;
    WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
     P2DIR = (SEG_A + SEG_B + SEG_C + SEG_D );
     P1DIR = ~SW1 + ~SW2 + ~SW3;                       // Set SW pin -> Input
     P1REN = SW1 + SW2 + SW3;                        // Enable Resistor for SW pin
     P1OUT = SW1 + SW2 + SW3;
     P1IE |= BIT3 + BIT4 + BIT5;
        P1IES |= BIT3 + BIT4 + BIT5;
        P1REN |= BIT3 + BIT4 + BIT5;
        P1OUT |= BIT3;
        P1IFG &= ~BIT3 + ~BIT4 + ~BIT5;
     while(1)                                  //Loop forever, we work with interrupts!
         {_BIS_SR(CPUOFF + GIE);                    }
     // Enter LPM0 w/ interrupt


 }


// The address of Port 1 interrupt service routine is  PORT1_VECTOR
#pragma vector=PORT1_VECTOR
__interrupt void button (void)
{
    if(!(P1IN & SW1))                            // Check if SW is pressed
                    {

              __delay_cycles(10000);
              a = 1;
              nextposition = currentposition - a;

              if(nextposition == 1)
                            {
              language = 1;
              go_dow();
                            }
              if(nextposition == 2)
                                     {
                       language = 4;
                       go_dow();

                                     }
              currentposition = 1;


                    }




  if(!(P1IN & SW2))                            // Check if SW is pressed
                 {
      __delay_cycles(10000);


           b = 2;
           nextposition = currentposition - b;

           if(nextposition == -1)
           {
           language = 2;
           go_dow();
           }
           if(nextposition == 1)
           {
           language = 1;
           go_dow();
           }
           currentposition = 2;
          }

  if(!(P1IN & SW3))                            // Check if SW is pressed
                        {
         c = 3;
         nextposition = currentposition - c ;
         if(nextposition == -2)
                        {

                        language = 3;
                        go_dow();
                        }
         if(nextposition == -1)
                        {
                        language = 2;
                        go_dow();

                        }
         currentposition = 3;
                        }


  P1IFG &= ~BIT3;
  __delay_cycles(1000);


    }

void go_up(void) {


    volatile unsigned int i;
    for(x = 0; x < 100; x++)
     {




    for(i = 0; i < 4; i++)
    {
      P2OUT = (P2OUT & DMASK) + digits[i];

    __delay_cycles(10000);


     }
    __delay_cycles(1000);
   }






       P2OUT = 0;
}

void go_down(void) {

     volatile unsigned int i;

 for(t = 100; t > 0; t--)
  {


     for(i = 4; i > 0; i--)
     {
       P2OUT = (P2OUT & DMASK) + digits[i];

     __delay_cycles(10000);


      }
     __delay_cycles(1000);
    }





        P2OUT = 0;


}

void go_dow(void) {

switch (language) {
     case 1:
     {

      go_down();
      language = 0;
       break;

     }
     case 2:
     {
      go_up();
      language = 0;
      break;
     }
        case 3:
         {
          go_up();
          go_up();
          language = 0;
          break;
         }
        case 4:
               {
                go_down();
                go_down();
                language = 0;
                break;
         }
     }
}

#include "msp430g2553.h"

#define SW1    BIT3
#define SW2    BIT4
#define SW3    BIT5
#define SEG_A   BIT0
#define SEG_B   BIT1
#define SEG_C   BIT2
#define SEG_D   BIT3

#define DMASK   ~(SEG_A + SEG_B + SEG_C + SEG_D)

const unsigned int digits[5] = { SEG_A, SEG_B, SEG_C, SEG_D };
volatile unsigned int x;

int currentposition;
int a;
int b;
int c;
int t;
int language;
int nextposition;
void go_up(void);
void go_down(void);

volatile unsigned char delay;

#pragma vector=TIMER0_A0_VECTOR
__interrupt void TIMER0_A0_ISR(void)
{
    delay = 0;
}

void timer_delay(unsigned int msec)
{
    delay = 1;
    if(msec > 174) 
        msec = 174;

    TACCR0 = msec*375 + TA0R;
    TACCTL0 = CCIE;
    while(delay>0);
    TACCTL0 &= ~CCIE;
}


void timer_init(void)
{
    BCSCTL2 |= DIVS_3; 
    TACTL |= TASSEL_2 | ID_2; 
    TACTL |= TACLR | MC_2;  
}


void main(void)
{
    currentposition = 2;
    WDTCTL = WDTPW | WDTHOLD;   // stop watchdog timer
    P2DIR |= (SEG_A + SEG_B + SEG_C + SEG_D);
    P1DIR &= ~(SW1 + SW2 + SW3);                       // Set SW pin -> Input
    P1REN |= (SW1 + SW2 + SW3);                       // Enable Resistor for SW pin
    P1OUT |= (SW1 + SW2 + SW3);
    P1IE |= BIT3 + BIT4 + BIT5;
    P1IES |= BIT3 + BIT4 + BIT5;
    P1REN |= BIT3 + BIT4 + BIT5;
    P1OUT |= BIT3;
    P1IFG &= ~(BIT3 + BIT4 + BIT5);
    timer_init();

    while (1)                           //Loop forever, we work with interrupts!
    {
        _BIS_SR(CPUOFF + GIE);
    }
    // Enter LPM0 w/ interrupt
}

void go_dow(void);

// The address of Port 1 interrupt service routine is  PORT1_VECTOR
#pragma vector=PORT1_VECTOR
__interrupt void button(void)
{
    if (!(P1IN & SW1))                            // Check if SW is pressed
    {
        timer_delay(8);
//        
        a = 1;
        nextposition = currentposition - a;

        if (nextposition == 1)
        {
            language = 1;
            go_dow();
        }
        if (nextposition == 2)
        {
            language = 4;
            go_dow();
        }
        currentposition = 1;
    }

    if (!(P1IN & SW2))                            // Check if SW is pressed
    {
        timer_delay(8);
//       
        b = 2;
        nextposition = currentposition - b;

        if (nextposition == -1)
        {
            language = 2;
            go_dow();
        }
        if (nextposition == 1)
        {
            language = 1;
            go_dow();
        }
        currentposition = 2;
    }

    if (!(P1IN & SW3))                            // Check if SW is pressed
    {
        c = 3;
        nextposition = currentposition - c;
        if (nextposition == -2)
        {
            language = 3;
            go_dow();
        }
        if (nextposition == -1)
        {
            language = 2;
            go_dow();
        }
        currentposition = 3;
    }

    P1IFG &= ~BIT3;
    timer_delay(1);

}

void go_up(void)
{
    volatile unsigned int i;
    for (x = 0; x < 100; x++)
    {

        for (i = 0; i < 4; i++)
        {
            P2OUT = (P2OUT & DMASK) + digits[i];

            timer_delay(8);
//          

        }
        timer_delay(1);
//    
    }

    P2OUT = 0;
}

void go_down(void)
{

    volatile unsigned int i;

    for (t = 100; t > 0; t--)
    {

        for (i = 4; i > 0; i--)
        {
            P2OUT = (P2OUT & DMASK) + digits[i];

            timer_delay(8);
//         

        }
        timer_delay(1);
//        
    }

    P2OUT = 0;
}

void go_dow(void)
{
    switch (language)
    {
    case 1:
    {
        go_down();
        language = 0;
        break;
    }
    case 2:
    {
        go_up();
        language = 0;
        break;
    }
    case 3:
    {
        go_up();
        go_up();
        language = 0;
        break;
    }
    case 4:
    {
        go_down();
        go_down();
        language = 0;
        break;
    }
    }
}


  •     while(delay>0);

    This is waiting for the TIMER0_A0_VECTOR to be called, but it's (only) called from an ISR, so interrupts are disabled, so that will never happen.

    A simple change here would be:

    >        TA0CCTL0 = (0*CCIFG);     // Clear IFG initially

    >        while ((TA0CCTL0 & CCIFG) == 0) /*EMPTY*/;   // Wait for timer expiration

    to explicitly wait for the IFG. In this case there's no need for  CCIE nor the ISR. (In fact they'll just get in the way.)

    Unsolicited: It looks like your PORT1 ISR never clears the IFGs for SW2/SW3, so (after the first push) your program will just loop through the ISR. This probably ends up doing the right thing due to the explicit P1IN checks, but you may get some odd effects.

    [Edit: Re-worded for clarity.]

  • Hi,

    How about your issue?

    Best Regards

    Johnson

  • it works thanks a lot

**Attention** This is a public forum