# MSP430 - Long Timer Count

In the previous post the program is set to flash for 10ms every 1 sec. I did this in order to determine the minimum flash duration that would still be very visible.

A pulse width of 10ms and a repetition delay of 1 second is highly visible. This gives a duty cycle of 1:100. If you wish, a repetition delay of 2 seconds giving a duty cycle of 1:200 would also be suitable.

The 16-bit timer register can count to a maximum of 65535, i.e. 128 seconds. Hence we can choose a suitable timer interrupt period of 60 or 120 seconds.

For simplicity sake, let us choose a timer interrupt interval of 60 seconds or 1 minute.

In order to delay for any length of time greater than 1 minute we simply count the number of timer interrupts.

For example, in order to delay for 24 hours, we need to count 1440 minutes. The sky is the minute. We can count for as long as we wish.

Before we get into the various methods of selecting the time delay we will set it for 10 seconds for testing only for the purpose of testing our reset function.

Code:
// Timer example with Low Power Mode
// 2013.04.29 - MrChips

#include "io430.h"

#define ON 1
#define OFF 0

#define LED1 P1OUT_bit.P0
#define LED2 P1OUT_bit.P6

#define ONE_SECOND 512

void delay(unsigned long d)
{
unsigned long i;
for (i = 0; i < d; i++);
}

void init(void)
{

// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;

P1OUT = 0x00;
P1DIR = 0xFF;

// Set up 32768Hz crystal
BCSCTL1 |= DIVA_3;    // divide by 8
BCSCTL3 |= XCAP_3;    // select 12pF caps

// initialize Timer0_A
TA0CCR0 = 10*ONE_SECOND;     // set up terminal count for 10s
TA0CTL = TASSEL_1 + ID_3 + MC_1; // configure and start timer

// enable interrupts
TA0CCTL0_bit.CCIE = 1;   // enable timer interrupts
__enable_interrupt();    // set GIE in SR
LPM3;            // select low power mode 3
}

#pragma vector = TIMER0_A0_VECTOR
__interrupt void myTimerISR(void)
{
TA0CCR0 = ONE_SECOND;
LED1 = ON;
delay(1000);   // flash LED1 for 10ms
LED1 = OFF;
}

void main( void )
{
init();
}
Here are the changes I have made to the program.

1) I have added a new constant ONE_SECOND to represent a timer count of one second.

2) I have set the timer interval to 10 seconds for this test only.

3) In the myTimerISR( ) I change the TA0CCR0 to one second.

The end result is the mcu will remain in low power mode for 10 seconds. After 10 seconds the LED will flash for 10ms at one second intervals while still in low power mode for the major portion (99:100) of each 1-second interval. Average current consumed is about 1μA.

I will add the minute counter later.

I know this seems like dragging out this example but that is the way I do program development. I test one thing at a time which gives me full confidence that every stage of the program works properly.

Author
MrChips
Views
6,042
Last update