O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

192 6. TIMER SYSTEMS
//Version: 3.41A
//***********************************************************************
#include <msp430xG46x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop WDT
FLL_CTL0 |= XCAP18PF; //Set load cap for 32k xtal
P5DIR |= 0x02; //Set P5.1 as output
BTCTL = BTDIV + BT_fCLK2_DIV16; //ACLK/(256*16)
IE2 |= BTIE; //Enable BT interrupt
_BIS_SR(LPM3_bits + GIE); //Enter LPM3, en interrupts
}
// Basic Timer Interrupt Service Routine
#pragma vector=BASICTIMER_VECTOR
__interrupt void basic_timer_ISR(void)
{
P5OUT ˆ= 0x02; //Toggle P5.1
}
//***********************************************************************
6.9.1 REAL TIME CLOCK
The real time clock (RTC) feature of the MSP430 provides a clock based on seconds, minutes, hours,
etc.The real time clock is configured using the Real Time Clock Control Register (RTCCTL).The
interrupt vector is shared with the interrupt vector of the real timer. Figure 6.14 provides the RTC
block diagram and the RTCCTL register contents.
The RTCBCD bit selects either binary or binary coded decimal (BCD) numbers, used for
the display purposes with bit 0 (binary) and bit 1 (BCD), respectively. The RTCHOLD bit is used
to turn on (0) or off (1) the real time clock. The RTCMODEx bits are used to choose the operating
mode for the real time clock as shown below.
00 - 32 bit counter/use ACLK
01 - 32 bit counter/use BTCNT2
10 - 32 bit counter/use SMCLK
11 - calendar mode/use BTCNT2
6.9. BASIC TIMER 1 193
76 5 4 3 2 1 0
Real-Time Clock Control Register (RTCCTL)
RTCBCD
RTCHOLD
RTCMODEx
RTCTEVxx
RTCIE
rw-(0) rw-(1) rw-(0) rw-(0) rw-(0) rw-(0) rw-0 rw-0
RTCFG
Figure 6.14: Real Time Clock and Control Register. Adapted from [slau056j].
In the calendar mode, the second, minute, hour, day of the week, month, year, and century
are stored in the RTCSEC, RTCMIN, RTCHOUR, RTCDOW, RTCDAY, RTCMON, RT-
CYEARL, and RTCYEARH registers, respectively. The RTCTEVx bits (bits 3 and 2) select the
interrupt interval based on the following selections and set the RTCFG flag.
00 - 8-bit overflow (counter mode), minute changed (calendar mode)
01 - 16-bit overflow (counter mode), hour changed (calendar mode)
10 - 24-bit overflow (counter mode), every day at midnight (calendar mode)
11 - 32-bit overflow (counter mode), every day at noon (calendar mode)
194 6. TIMER SYSTEMS
The RTCIE bit is the local interrupt enable bit. When it is set, both BTIFG and RTCFG flags are
set. To disable the interrupt, this bit needs to be cleared.
Example: As in the previous example, we use the MSP-EXP430FG4618 Experimenters
Board introduced in Chapter A.5. This board hosts the MSP430FG4618 microcontroller. This
example was adapted from sample code provided by Texas Instruments [slac118d]. In this specific
example, the Real Time Clock (RTC) is used to toggle an LED on P5.1. Both Assembly and C
Language examples are provided.
;************************************************************************
;MSP430xG461x Demo - Real Time Clock, Toggle P5.1 inside an interrupt
;service routine (ISR), 32kHz ACLK.
;
;Description: This program toggles P5.1 by xor’ing P5.1 inside of
;a Real Time Clock ISR. The Real Time Clock ISR is called once a
;minute using the Alarm function provided by the RTC. The ACLK used as
;the clock source for the basic timer. (ACLK = LFXT1 = 32768Hz,
;MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz)
;
;An external watch crystal between XIN & XOUT is required for the
;ACLK.
;
;JL Bile
;Texas Instruments Inc.
;June 2008
;Built Code Composer Essentials: v3 FET
;************************************************************************
.cdecls C,LIST, "msp430xG46x.h"
;------------------------------------------------------------------------
.text ;Program Start
;------------------------------------------------------------------------
RESET mov.w #900,SP ;Initialize stackpointer
StopWDT mov.w #WDTPW+WDTHOLD,&WDTCTL ;Stop WDT
SetupFLL bis.b #XCAP14PF,&FLL_CTL0 ;Configure load caps
SetupRTC mov.b #RTCBCD+RTCHOLD+RTCMODE_3+RTCTEV_0+RTCIE,&RTCCTL
;RTC enable, BCD mode,
;alarm every Minute,
;enable RTC interrupt
;Init time
mov.b #000h,&RTCSEC ;Set Seconds
6.9. BASIC TIMER 1 195
mov.b #000h,&RTCMIN ;Set Minutes
mov.b #008h,&RTCHOUR ;Set Hours
; Init date
mov.b #002h,&RTCDOW ;Set DOW
mov.b #023h,&RTCDAY ;Set Day
mov.b #008h,&RTCMON ;Set Month
mov.w #02005h,&RTCYEAR ;Set Year
bic.b #RTCHOLD,&RTCCTL ;Enable RTC
SetupP5 bis.b #002h,&P5DIR ;P5.1 output
Mainloop bis.w #LPM3+GIE,SR ;Enter LPM3, en interrupts
nop ;Required only for debugger
;------------------------------------------------------------------------
BT_ISR; Toggle P5.1
;------------------------------------------------------------------------
xor.b #002h,&P5OUT ;Toggle P5.1
reti ;
;
;------------------------------------------------------------------------
; Interrupt Vectors
;------------------------------------------------------------------------
.sect ".int16" ;Basic Timer Vector
.short BT_ISR ;
.sect ".reset" ;POR, ext.
Reset, Watchdog
.short RESET
.end
;------------------------------------------------------------------------
In C:
//***********************************************************************
//MSP430xG461x Demo - Real Time Clock, Toggle P5.1 Inside ISR,
//32kHz ACLK
//
//Description: This program toggles P5.1 by xor’ing P5.1 inside of
//a Real Time Clock ISR. The Real Time Clock ISR is called once a
196 6. TIMER SYSTEMS
//minute using the Alarm function provided by the RTC. The ACLK is used
//as the clock source for the basic timer. (ACLK = LFXT1 = 32768Hz,
//MCLK = SMCLK = default DCO = 32 x ACLK = 1048576Hz)
//
//An external watch crystal between XIN & XOUT is required for the
//ACLK.
//
//S.Schauer/A. Dannenberg
//Texas Instruments Inc.
//June 2007
//Built with CCE Version: 3.2.0 and IAR Embedded Workbench
//Version: 3.42A
//***********************************************************************
#include <msp430xG46x.h>
//***********************************************************************
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; //Stop Watchdog Timer
FLL_CTL0 = XCAP14PF; //Configure load caps
RTCCTL = RTCBCD+RTCHOLD+RTCMODE_3+RTCTEV_0+RTCIE;
//RTC enable, BCD mode,
//alarm every Minute,
//enable RTC interrupt
// Init time
RTCSEC = 0x00; //Set Seconds
RTCMIN = 0x00; //Set Minutes
RTCHOUR = 0x08; //Set Hours
// Init date
RTCDOW = 0x02; //Set DOW
RTCDAY = 0x23; //Set Day
RTCMON = 0x08; //Set Month
RTCYEAR = 0x2005; //Set Year
RTCCTL &= ˜RTCHOLD; //Enable RTC

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required