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: Up-Down Counter Code using Timer A

Part Number: MSP430G2553

Hello all,

I need to write code which counts up/down and blinks led in order as shown in table.

UP MODE (1-2-...-7-0-1-...)  / DOWN MODE (7-6-5-....-0-7..)

The count mode is required to be toggled by a switch connected to the pin P1.5. Between each count, specific amount of delay must be configured using TIMERA.

I wrote a code to count up but how can I toggle the count mode? 

Here is my code :

#include<msp430.h>

int counter=0;

int main(void)

{

WDTCTL = WDTPW | WDTHOLD; // Stop watchdog timer

P1DIR |= 0x04; // Set P1.2 to output direction

P1DIR |= 0x02; // Set P1.1 to output direction

P1DIR |= 0x01; // Set P1.0 to output direction

P1OUT |= 0X20; // Configure P1.5 as pull-up

P1OUT = P1OUT & ~BIT0; // ... When program starts turn off LED 1

P1OUT = P1OUT & ~BIT1; // ... When program starts turn off LED 2

P1OUT = P1OUT & ~BIT2; // ... When program starts turn off LED 3

BCSCTL1=CALBC1_1MHZ; //Callibrate the frequency of timer as 1 MHZ

DCOCTL=CALDCO_1MHZ; //Callibrate the frequency of timer as 1 MHZ

CCTL0=CCIE; //CCR0 interrupt enabled

TACTL = TASSEL_2 + ID_0 + MC_1; //Select SMCLK, SMCLK/1, Up Mode

CCR0 = 50000; //Set the period to 50000*(1/1Mhz)=50 miliseconds

_BIS_SR(LPM0_bits + GIE); // Global enable interrupt and low power mode 0 (SMCLK available)

}

//Timer A0 interrupt service routine

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A(void)

{

counter++; //Increment counter for each press

switch((counter/20)%8)  //...1 sec delay

{

case 1:

P1OUT = P1OUT | BIT0; // ... turn on LED 1

P1OUT = P1OUT & ~BIT1; // ... turn off LED 2

P1OUT = P1OUT & ~BIT2; // ... turn off LED 3

break;

 

case 2:

P1OUT = P1OUT & ~BIT0; // ... turn off LED 1

P1OUT = P1OUT | BIT1; // ... turn on LED 2

P1OUT = P1OUT & ~BIT2; // ... turn off LED 3

break;

 

case 3:

P1OUT = P1OUT | BIT0; // ... turn on LED 1

P1OUT = P1OUT | BIT1; // ... turn on LED 2

P1OUT = P1OUT & ~BIT2; // ... turn off LED 3

break;

 

case 4:

P1OUT = P1OUT & ~BIT0; // ... turn on LED 1

P1OUT = P1OUT & ~BIT1; // ... turn on LED 2

P1OUT = P1OUT | BIT2; // ... turn off LED 3

break;

 

case 5:

P1OUT = P1OUT | BIT0; // ... turn on LED 1

P1OUT = P1OUT & ~BIT1; // ... turn on LED 2

P1OUT = P1OUT | BIT2; // ... turn off LED 3

break;

 

case 6:

P1OUT = P1OUT & ~BIT0; // ... turn on LED 1

P1OUT = P1OUT | BIT1; // ... turn on LED 2

P1OUT = P1OUT | BIT2; // ... turn off LED 3

break;

 

case 7:

P1OUT = P1OUT | BIT0; // ... turn on LED 1

P1OUT = P1OUT | BIT1; // ... turn on LED 2

P1OUT = P1OUT | BIT2; // ... turn off LED 3

break;

 

default:

P1OUT = P1OUT & ~BIT0; // ... turn off LED 1

P1OUT = P1OUT & ~BIT1; // ... turn off LED 2

P1OUT = P1OUT & ~BIT2; // ... turn off LED 3

break;

}

}

 

Any advice would be appreciated.

Thanks and Regards,

-Can.

  • Hello,

    You can define another variable which represents the display mode rather than directly use (counter/20)%8 to determine the display mode.

    This variable will count up/down  based on the counter/20.

**Attention** This is a public forum