Skip to content

Instantly share code, notes, and snippets.

@dwaq
Last active December 19, 2015 19:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dwaq/6008563 to your computer and use it in GitHub Desktop.
Save dwaq/6008563 to your computer and use it in GitHub Desktop.
MSP430 flip clock project located at http://tinkeringetc.blogspot.com/
//*************************************************************************************************
// MSP430 Flip Clock
//
// By: Dillon Nichols
// http://tinkeringetc.blogspot.com/
//
// Created in Code Composer Studio 5.4.0
//
// Description:
// A frequency is created by the external 32768Hz crystal which causes an interrupt every minute.
// During this interrupt, the servo arm moves to increase the minute count, then quickly moves
// back to the neutral position.
//
// Based on code from http://www.msp430launchpad.com/2012/03/using-aclk-and-32khz-crystal.html
// and http://gushh.net/blog/msp430-servo/
//
// This work is licensed under a Creative Commons Attribution 3.0 Unported License.
//*************************************************************************************************
#include <msp430g2553.h>
#define MCU_CLOCK 1000000 // 1MHz
#define PWM_FREQUENCY 46 // 46Hz
void main (void){
// Disable watchdog timer (password and hold)
WDTCTL = WDTPW + WDTHOLD;
// Set up Timer1_A for tracking seconds
BCSCTL1 |= DIVA_3; // ACLK/8 = 4096Hz, ACLK is sourced from 32768Hz crystal
BCSCTL3 |= XCAP_3; // ~12.5pF cap, setting for 32768Hz crystal
TA1CCTL0 = CCIE; // Capture/compare (TA1CCR0) interrupt enable
// Counts to TA1CCR0 and triggers an interrupt and restarts at 0
TA1CCR0 = 30720; // 512 -> 1 sec, x60 = 30720 -> 1 min
// TASSEL_1 - Timer1_A clock source select, 32768Hz /8 = 4096Hz
// ID_3 - The divider for the input clock -> /8, 4096Hz /8 = 512Hz
// MC_1 - The timer counts up to the value of TA1CCR0
TA1CTL = TASSEL_1 + ID_3 + MC_1;
// Set up Timer0_A for PWM
// OUTMOD_7 - Reset/Set
//The output is reset when the timer counts to the TA0CCR1 value.
//It is set when the timer counts to the TA0CCR0 value.
TA0CCTL1 = OUTMOD_7;
// TASSEL_2 - Timer0_A clock source select, SMCLK = 1MHz
// MC_1 - The timer repeatedly counts from zero to the value of TA0CCR0
TA0CTL = TASSEL_2 + MC_1;
TA0CCR0 = ((MCU_CLOCK/PWM_FREQUENCY)-1); // = 21738 -> PWM Period
TA0CCR1 = 0; // Initial PWM Duty Cycle
// Connect the servo signal wire to P1.2 through a 1K ohm resistor
P1DIR |= BIT2; // Direction of P1.2 set as output
P1SEL |= BIT2; // P1.2 = TA0.1 output (MSP430G2553 datasheet page 43)
// Initialize unused ports 2 and 3 to eliminate waste current consumption
P2SEL = 0xBE;
P3SEL = 0xBE;
// Bit Set Status Register
_BIS_SR(LPM3_bits + GIE); // Enter LPM3 and recognize maskable interrupts
// Maskable interrupts are caused by peripherals with interrupt capability
// Low-power mode 3 (LPM3)
// CPU is disabled
// MCLK and SMCLK are disabled
// DCO's DC generator is disabled
}
// Timer1_A interrupt service routine
#pragma vector=TIMER1_A0_VECTOR // TIMER1_A0_VECTOR is found in msp430g2553.h
__interrupt void Tick (void){ // Tick is an arbitrary name
TA0CCR1 = 1410; // tick
__delay_cycles(150000); // enough time to move
TA0CCR1 = 1260; // neutral
__delay_cycles(150000); // enough time to move back
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment