Skip to content

Instantly share code, notes, and snippets.

@RickKimball
Created May 4, 2011 04:59
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 RickKimball/954778 to your computer and use it in GitHub Desktop.
Save RickKimball/954778 to your computer and use it in GitHub Desktop.
msp430g2231 - use external square wave from ATTiny @ 12 MHZ as LFXT1 clock
//***********************************************************
// runfast.c - Using 12MHZ external square wave clock as LFXTCLK
// source MCLK/SMCLK from 12MHZ wave and blink led
// at 60Hz
//
// Use an external clock ( I'm using an attiny running at 12.00 MHZ
// with the CLKOUT fuse enabled ) as the MCLK/SMCLK for the MSP430.
// I've been told you could also use an external oscillator like an
// ECS-100A-160. I ordered some of these to verify.
//
// LFXT1 oscillator logic level square wave input frequency, LF mode
//
// XIN - external clock in from attiny CLKOUT pin ( set avr fuse to get clock )
// XOUT - NC
// P1.0 - led on until OSCFault cleared
// P1.4 - SMCLK frequency, measuer with multimeter/oscope
// P1.6 - 60Hz led flash, measure with multimeter/oscope
//
//***********************************************************
#include <msp430g2231.h>
void main(void) {
WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
P1DIR = BIT0 | BIT6 | BIT4; // P1.0 and P1.6 output, P1.4 SMCLK frequency
P1SEL = BIT4; // P1.4 output SMCLK on P1.4, measure with multi meter
P1OUT = BIT0; // red LED on until OSCFault is cleared
DCOCTL = 0x00;
BCSCTL2 = SELM_3 + DIVM_0 + SELS + DIVS_0; // MCLK Source Select 3: LFXTCLK
// MCLK/1 SMCLK/1 SMCLK LFXTCLK
BCSCTL1 |= XTS; // XTS
BCSCTL3 = LFXT1S_3 | XCAP_0; // LFXT1 = 12MHZ crystal, No Capacitor
// not sure if this is needed
while(IFG1 & OFIFG) {
IFG1 &= ~OFIFG; // Clear OSCFault flag
_delay_cycles(10000); // delay for flag and visibility
}
P1OUT = 0; // red LED off
__bis_SR_register(SCG0); // Stop DCO, we just use the LFXT1 which is
// actually running at 12MHZ
while(1) {
P1OUT ^= BIT6; // green LED on, measure this, it should be 60Hz
_delay_cycles((12000000/60/2)-35); // 12MHZ, 60/2, 35 overhead clocks
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment