MSP430 Seconds Counter

Welcome! Making of Seconds Counter: Using CCStudio 8 and MSP430F5529 for the project.

C language to code the micro controller. Applying Low Power Modes, Timers and Interrupts.The output is displayed via 7 Segment.

Step 1: Insight

Let's Begin!

Initialize the watchdog timer to OFF state using the required password for the watchdog timer (It helps to keep check of infinite loops , keeping the processor safe).

#include

/** * main.c */

int main(void)

{

WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

return 0;

}

Step 2: Port Initialization

{

P3DIR=0xFF; // P3DIR=0x00;

P6DIR=0xFF;

P4DIR |=0x00;

P4REN |=0xFF;

P4OUT |=0xFF;

}

P3DIR |=0x00 tells us that the whole of PORT-3 is initialized to take inputs.

P3DIR |=0xFF tells us that the whole of PORT-3 is initialized to give outputs.

P3DIR |=0x01 only the pin P3.0 is initialized to output in PORT-3. This follows a Hexadecimal Port mapping.

P4REN |=0xFF , this indicates that the pins of PORT-4 have their pull up/down resistors enabled.

To select them between Pull UP or Pull DOWN, the instruction P$OUT |=0xFF is used.

If 0xFF is used they configure as Pull UP resistors and if 0x00 they configure as Pull DOWN.

Step 3: Ultra Low Power

MSP430F5529 allows us to reduce power loss from the processor. This is useful in standalone applications.

This calls for declaration of all pin or Ports to output.

{

P7DIR |= 0xFF;

P6DIR |= 0xFF;

P5DIR |= 0xFF;

P4DIR |= 0xFF;

P3DIR |= 0xFF;

P2DIR |= 0xFF;

P1DIR |= 0xFF;

}

Step 4: TIMER

Usage of timer for Delay generation of one second. This uses the SMCLK of 1MHz , also timer runs in Low power Mode (in the next step, after its count its interrupted from LPM). This process saves power and burden on the processor

TA0CCTL0=CCIE;

TA0CCR0=999;

TA0CTL = TASSEL_2 + MC_1;

Values is 999, as the takes one more count to roll back to zero in the timer register.

Step 5: Low Power Mode

_BIS_SR(LPM0_bits+GIE);

This enables General interrupt Enable (GIE), and puts the CPU to LPM0 , where MCLK that supports the cpu is off , and SMCLK and ACLK run that keeps the timer running. so we can see CPU is turned off , there by saving power.

Step 6: ISR-Timer

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A (void)

{

z++;

if(z>delay)

{

P3OUT=code[x];

P6OUT=code1[y];

x++;

if(x==10)

{

x=0;

y++;

}

if(y==6)

y=0;

z=0;

}

}

pragma vector is for ISR representation in C embd.

code[x] and code1[y] are the arrays that contain output values for the two seven segments, for displaying 60 seconds counter.

Step 7: Hardware Interrupt

P2DIR=0x00;

P2REN=0x02;

P2OUT=0x02;

P2IE |=BIT1 ;

P2IES |=BIT1 ;

P2IFG &= ~BIT1 ;

Here P2.1 is declared as a hardware interrupt, if the button is pressed , the counter resets to the value.

the rest program is written inside the ISR of this interrupt.

Step 8: ISR- Reset/ Push Button

#pragma vector=PORT2_VECTOR

__interrupt void port_2(void)

{

P2IFG &=~BIT1 ;

x=0; y=0;

P3OUT=code[x];

P6OUT=code1[y];

v++;

for(i=0;i

{

P1OUT |= BIT0; //P1.0 = toggle

__delay_cycles(1048576);

P1OUT &=~BIT0; // P1.0 = toggle

__delay_cycles(1048576);

}

This ISR resets the counter , and keeps a count on how many times the rest was pressed.

(Here is display is made via led toggle , can also use another array and timer , to show those values as output in 7 segment).

Step 9: CODE

#include

#define delay 1000

char code[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xE6};

char code1[]={0x7E,0x30,0x6D,0x79,0x33,0x5B};

volatile unsigned int x=0,y=0,z=0;

volatile unsigned int v=0,i=0;

void main()

{

WDTCTL = WDTPW | WDTHOLD; // stop watchdog timer

P7DIR |= 0xFF;

P7OUT |= 0x00;

P8DIR |= 0xFF;

P8OUT |= 0x00;

P4DIR |= 0xFF;

P4OUT |= 0x00;

P5DIR |= 0xFF;

P5OUT |= 0x00;

P1DIR=0xFF;

P3DIR=0xFF;

P6DIR=0xFF;

P2DIR=0x00;

P2REN=0x02;

P2OUT=0x02;

P2IE |=BIT1 ;

P2IES |=BIT1 ;

P2IFG &= ~BIT1 ;

TA0CCTL0=CCIE;

TA0CCR0=999;

TA0CTL = TASSEL_2 + MC_1;

_BIS_SR(LPM0_bits+GIE);

}

// Timer A0 interrupt service routine

#pragma vector=TIMER0_A0_VECTOR

__interrupt void Timer_A (void)

{

z++;

if(z>delay)

{

P3OUT=code[x];

P6OUT=code1[y];

x++;

if(x==10)

{

x=0;

y++;

}

if(y==6)

y=0;

z=0;

}

}

// Hardware interrupt service routine

#pragma vector=PORT2_VECTOR

__interrupt void port_2(void)

{

P2IFG &=~BIT1 ;

x=0;

y=0;

P3OUT=code[x];

P6OUT=code1[y];

v++;

for(i=0;i

{
P1OUT |= BIT0; // P1.0 = toggle

__delay_cycles(1048576);

P1OUT &=~BIT0; // P1.0 = toggle

__delay_cycles(1048576);

}

}

Step 10: Reference Code

Share

    Recommendations

    • Paper Contest

      Paper Contest
    • Remix Contest

      Remix Contest
    • Build a Tool Contest

      Build a Tool Contest

    Discussions

    1
    None
    seamster

    2 months ago

    Thanks for sharing your first instructable, congrats! : )