計時器

  • Software timer VS Timer_A

Software delay loops are a waste of the processor because it is not available for more useful actions. There are other serious problems too, such as the unpredictability of delays written in C. All microcontrollers therefore have special hardware to act as a timer, often many of them. The heart of a timer is just a counter fed from a clock, so the idea is a little different from the software loop. In particular, a timer has no idea of real time: It just counts clock cycles. It is up to the programmer to ensure that the value held in the counter is meaningful. Also, the recorded time is only as accurate as the frequency of the clock, which therefore needs a crystal if something like a reliable time of day is needed.

  • Software Timer

void delay(void) {

unsigned int count; //DELAY

for (count=0; count<60000; count++);

}

  • Timer_A

you can do math upon the CCR's and the TAR if you want. Simply mess with the CCR1 in this example for longer delays:

//

******************************************************************************

// MSP430F20xx Demo - Timer_A, PWM TA1, Up Mode, 32kHz ACLK

//

// Description: This program generates one PWM output on P1.2 using

// Timer_A configured for up mode. The value in CCR0, 512-1, defines the PWM

// period and the value in CCR1 the PWM duty cycles. Using 32kHz ACLK

// as TACLK, the timer period is 15.6ms with a 75% duty cycle on P1.2.

// Normal operating mode is LPM3.

// ACLK = TACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO.

// //* External watch crystal installed on XIN XOUT is required for ACLK *//

//

// MSP430F20xx

// -------------------

// /|\| XIN|-

// | | | 32kHz

// --|RST XOUT|-

// | |

// | P1.2/TA1|--> CCR1 - 75% PWM

//

// M.Buccini / L. Westlund

// Texas Instruments, Inc

// October 2005

// Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version:3.40A

//

******************************************************************************

#include <msp430x20x3.h>

void main (void)

{

WDTCTL = WDTPW + WDTHOLD; // Stop WDT

P1DIR |= 0x0C; // P1.2 and P1.3 output

P1SEL |= 0x0C; // P1.2 and P1.3 TA1/2 options

CCR0 = 512-1; // PWM Period

CCTL1 = OUTMOD_7; // CCR1 reset/set

CCR1 = 384; // CCR1 PWM duty cycle

TACTL = TASSEL_1 + MC_1; // ACLK, up mode

_BIS_SR(LPM3_bits); // Enter LPM3

}

WDT+

程式的第一句將看門狗關閉,MSP430 的看門狗重定後的預設狀態是打開的,如果不使用就必須關閉,否則會產生PUC 信號,從而導致系統重定。

http://justinstech.org/2010/08/msp430-basic-codingprograming-part-2-wdt/