code_msp430g2553

Embed Size (px)

Citation preview

//****************************************************************************** // MSP430G2x13/G2x53 Demo - Comp_A, Output Reference Voltages on P1.1 // // Description: Output Comparator_A reference levels on P1.1. Program will // cycle through the on-chip comparator_A reference voltages with output on // P1.1. Normal mode is LPM0, TA0_ISR will interrupt LPM0. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2x13/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1/CA1|--> Vref // | | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void delay(void); void main (void) { WDTCTL = WDTPW + WDTHOLD; CACTL2 = P2CA4; CCTL0 = CCIE; TACTL = TASSEL_2 + ID_3 + MC_2; _EINT(); while (1) { CACTL1 = 0x00; _BIS_SR(LPM0_bits); CACTL1 = CAREF0 + CAON; _BIS_SR(LPM0_bits); CACTL1 = CAREF1 + CAON; _BIS_SR(LPM0_bits); CACTL1 = CAREF1 + CAREF0 + CAON; _BIS_SR(LPM0_bits); } } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) { _BIC_SR_IRQ(LPM0_bits); } // Software delay

// // // // //

Stop WDT CA1/P1.1 = +comp CCR0 interrupt enabled SMCLK/8, cont-mode enable interrupts

// Loop // // // // // // // // No reference voltage Enter LPM0 0.25*Vcc, Comp. on Enter LPM0 0.5*Vcc, Comp. on Enter LPM0 0.55V, Comp. on Enter LPM0

// Clear LPM0 bits from 0(SR)

//****************************************************************************** // MSP430G2x13/G2x53 Demo - Comp_A, Detect Threshold, Set P1.0 if P1.1 > 0.25* Vcc // // Description: Use Comparator_A to detect a voltage threshold.

// Using an external potentiometer, an unknown voltage is applied to P1.1. // Comparator_A compares the unknown voltage to an internal reference // voltage, in this example 0.25*VCC. If the unknown voltage is higher // than 0.25*VCC, P1.0 is set, if not, P1.0 is reset. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2x13/G2x53 // ----------------// /|\ | XIN|// | | | // ---|RST XOUT|// | | | // RLED // | | | // ---|VSS // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main (void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CACTL1 = CARSEL + CAREF0 + CAON; CACTL2 = P2CA4; while (1) { if ((CAOUT & CACTL2)) P1OUT |= 0x01; else P1OUT &= ~0x01; } } //****************************************************************************** // MSP430G2x13/G2x53 Demo - Comp_A, Simple 2.2V Low Battery Detect // // Description: Two comparator_A reference generators 0.25*Vcc and ~0.55V // are compared for a simple battery check of 2.2V. In the subroutine // Batt_Check, a small capacitor between P2.3 and Vss is first charged // to 0.25*Vcc and then compared to ~0.55V. If 0.25*Vcc, // is above ~0.55V, P1.0 is toggled, else set. Batt_Check is called // constantly in an endless loop - in an actual application, Batt_Check // should be called infrequently to save power. // ACLK = n/a, MCLK = SMCLK = default DCO // // There is a tolerance of the Comp_A reference generator and in the // device specific datasheet. In most applications, the tolerence of the // reference generator is more than adequate to detect Low Battery. // // MSP430G2x13/G2x53 // ----------------// /|\ | XIN|// | | | // ---|RST XOUT|-

// // // //

Stop WDT P1.0 output 0.25 Vcc = -comp, on P1.1/CA1 = +comp

// Test comparator_A output // if CAOUT set, set P1.0 // else reset

// | | // +-----|P1.1/CA1 P1.0|-->LED // | | | // ===.1uf| | // | | | // +-----|Vss // void Batt_Check(void); unsigned int i; // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main (void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CACTL2 = P2CA4; while (1) { Batt_Check(); } }

// // // //

Stop WDT P1.0 output P1.1 = CA1 Mainloop

void Batt_Check(void) { CACTL1 = CAREF_1 + CAON; // 0.25*Vcc on P1.1, Comp. on i = 16384; // delay while(i>0) { i--; } CACTL1 = CARSEL + CAREF_2 + CAREF_1 + CAON; // 0.55V on -, Comp. on if (CACTL2 & CAOUT) P1OUT ^= 0x01; // P1.0 toggle else P1OUT |= 0x01; // P1.0 set CACTL1 = 0x00; // Disable Comp_A, save power } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A1, AVcc Ref, Set P1.0 if > 0.5*AVcc // // Description: A single sample is made on A1 with reference to AVcc. // Software sets ADC10SC to start sample and conversion - ADC10SC // automatically cleared at EOC. ADC10 internal oscillator times sample (16x) // and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC10 // conversion complete, ADC10_ISR will force exit from LPM0 in Mainloop on // reti. If A1 > 0.5*AVcc, P1.0 set, else reset. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.1/A1 P1.0|-->LED // // D. Dang

// Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10CTL1 = INCH_1; // input A1 ADC10AE0 |= 0x02; // PA.1 ADC option select P1DIR |= 0x01; // Set P1.0 to output direction for (;;) { ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); if (ADC10MEM < 0x1FF) P1OUT &= ~0x01; else P1OUT |= 0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A1, 1.5V Ref, Set P1.0 if > 0.2V // // Description: A single sample is made on A1 with reference to internal // 1.5V Vref. Software sets ADC10SC to start sample and conversion - ADC10SC // automatically cleared at EOC. ADC10 internal oscillator times sample (16x) // and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC10 // conversion complete, ADC10_ISR will force exit from any LPMx in Mainloop on // reti. If A1 > 0.2V, P1.0 set, else reset. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.1/A1 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE;

// Sampling and conversion start // LPM0, ADC10_ISR will force exit // Clear P1.0 LED off // Set P1.0 LED on

__enable_interrupt(); TACCR0 = 30; TACCTL0 |= CCIE; TACTL = TASSEL_2 | MC_1; LPM0; TACCTL0 &= ~CCIE; __disable_interrupt(); ADC10CTL1 = INCH_1; ADC10AE0 |= 0x02; P1DIR |= 0x01; for (;;) { ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); if (ADC10MEM < 0x88) P1OUT &= ~0x01; else P1OUT |= 0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { __bic_SR_register_on_exit(CPUOFF); }

// // // // // //

Enable interrupts. Delay to allow Ref to settle Compare-mode interrupt. TACLK = SMCLK, Up mode. Wait for delay. Disable timer Interrupt

// input A1 // PA.1 ADC option select // Set P1.0 to output direction

// // // //

Sampling and conversion start LPM0, ADC10_ISR will force exi ADC10MEM = A1 > 0.2V? Clear P1.0 LED off

// Set P1.0 LED on

// Clear CPUOFF bit from 0(SR)

#pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A10 Temp, Set P1.0 if Temp ++ ~2C // // Description: se ADC10 and the integrated temperature sensor to detect // temperature gradients. The temperature sensor output voltage is sampled // ~ every 120ms and compared with the defined delta values using an ISR. // (ADC10OSC/4)/64 determines sample time which needs to be greater than // 30us for temperature sensor. // ADC10 is operated in repeat-single channel mode with the sample and // convert trigger sourced from Timer_A CCR1. The ADC10IFG at the end // of each converstion will trigger an ISR. // ACLK = n/a, MCLK = SMCLK = default DCO ~ 1.2MHz, ADC10CLK = ADC10OSC // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // |A10 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10

//****************************************************************************** #include "msp430g2553.h" static unsigned int FirstADCVal; #define ADCDeltaOn 3 // holds 1st ADC result // ~ 2 Deg C delta for LED on

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog ADC10CTL1 = ADC10DIV_3 + INCH_10 + SHS_1 + CONSEQ_2; // TA trig., rpt, A10 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REF2_5V + ADC10IE + REFON + ADC10ON; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); ADC10CTL0 |= ENC; TACCTL1 = OUTMOD_4; // Toggle on EQU1 (TAR = 0) TACTL = TASSEL_2 + MC_2; // SMCLK, cont-mode while (!(ADC10IFG & ADC10CTL0)); // First conversion? FirstADCVal = ADC10MEM; // Read out 1st ADC value P1OUT = 0x00; // Clear P1 P1DIR = 0x01; // P1.0 as output __bis_SR_register(LPM0_bits + GIE); // Enter LPM0 w/ interrupt } #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { if (ADC10MEM >= FirstADCVal + ADCDeltaOn) P1OUT |= 0x01; // LED on else P1OUT &= ~0x01; // LED off } #pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A1, Signed, Set P1.0 if > 0.5*AVcc // // Description: A single sample is made on A1 with SIGNED reference to AVcc. // Software sets ADC10SC to start sample and conversion - ADC10SC // automatically cleared at EOC. ADC10 internal oscillator times sample (16x) // and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC10 // conversion complete, ADC10_ISR will force exit from any LPMx in Mainloop on // reti. If A1 > 0.5*AVcc, P1.0 set, else reset. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | |

// >---|A1/P.1 | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = ADC10DF + INCH_1; // Conversion code singed format, in put A1 ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // ADC10ON, interrupt enabled ADC10AE0 |= 0x02; // P1.0 ADC option select P1DIR |= 0x01; // Set P1.0 to output direction for (;;) { ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); if ((int)ADC10MEM < 0) P1OUT &= ~0x01; else P1OUT |= 0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A11, Lo_Batt, Set P1.0 if < 2.3V // // Description: A single sample is made on A11 (AVcc/2) with reference to // internal 1.5V Vref. Software sets ADC10SC to start sample and conversion // - ADC10SC automatically cleared at EOC. ADC10 internal oscillator times // sample (16x) and conversion. ADC10BUSY flag is polled for EOC. If A11 // (AVcc/2) < 0311h (0.65V) indicating AVcc is less 2.3V, P1.0 set indicating // a lo_Batt condition, else reset. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // |A11 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h"

// Sampling and conversion start // LPM0, ADC10_ISR will force exit // Clear P1.0 LED off // Set P1.0 LED on

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_11; // AVcc/2 ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON; P1DIR |= 0x01; // Set P1.0 to output direction for (;;) { ADC10CTL0 |= ENC + ADC10SC; while (ADC10CTL1 & ADC10BUSY); if (ADC10MEM < 0x311) P1OUT |= 0x01; else P1OUT &= ~0x01; }

// // // //

Sampling and conversion start ADC10BUSY? ADC10MEM = A11 > 0.65? Set P1.0 LED on

// Clear P1.0 LED off

} //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A11, Lo_Batt, Set P1.0 if < 2.3V // // Description: A single sample is made on A11 (AVcc/2) with reference to // internal 1.5V Vref. Software sets ADC10SC to start sample and conversion // - ADC10SC automatically cleared at EOC. ADC10 internal oscillator times // sample (16x) and conversion. ADC10BUSY flag is polled for EOC. If A11 // (AVcc/2) < 0311h (0.65V) indicating AVcc is less 2.3V, P1.0 set indicating // a lo_Batt condition, else reset. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // |A11 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_11; // AVcc/2 ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON; P1DIR |= 0x01; // Set P1.0 to output direction for (;;) { ADC10CTL0 |= ENC + ADC10SC; while (ADC10CTL1 & ADC10BUSY); if (ADC10MEM < 0x311) P1OUT |= 0x01; else P1OUT &= ~0x01; }

// // // //

Sampling and conversion start ADC10BUSY? ADC10MEM = A11 > 0.65? Set P1.0 LED on

// Clear P1.0 LED off

} //******************************************************************************

// MSP430G2x33/G2x53 Demo - ADC10, Output Internal Vref on P1.4 & ADCCLK on P1. 3 // // Description: Output ADC10 internal Vref on P1.4, toggling between two // avaialble options, 2.5v and 1.5v. ADC10OSC also output on P1.3. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // Vref ADC10OSC ~ 3.5MHz - 6.5MHz // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void delay(void); // Software delay

void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = CONSEQ_2; // Repeat single channel ADC10CTL0 = REFOUT + REFON + MSC + ADC10ON; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); ADC10AE0 |= 0x10; // P1.4 ADC option select P1DIR |= 0x08; // Set P1.3 output direction P1SEL |= 0x08; // Set P1.3 option select for (;;) { ADC10CTL0 &= ~ENC; ADC10CTL0 ^= REF2_5V; ADC10CTL0 |= ENC + ADC10SC; delay(); } } void delay(void) { volatile unsigned long i; for (i = 0x7FFFF; i > 0; i--); } #pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; }

// ADC10 disable // Toggle Vref 1.5/2.5V // Sampling and conversion start

// Exit LPM0 on return

//****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A1 32x, AVcc, Repeat Single, DCO // // Description: Use DTC to sample A1 32 times with reference to AVcc. Software // writes to ADC10SC to trigger sample burst. In Mainloop MSP430 waits in LPM0 // to save power until ADC10 conversion burst complete, ADC10_ISR(DTC) will // force exit from any LPMx in Mainloop on reti. ADC10 internal oscillator // times sample period (16x) and conversion (13x). DTC transfers conversion // code to RAM 200h - 240h. P1.0 set at start of conversion burst, reset on // completion. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.1/A1 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = CONSEQ_2 + INCH_1; // Repeat single channel, A1 ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; // ADC10ON, interrupt enabl ADC10DTC1 = 0x20; // 32 conversions ADC10AE0 |= 0x02; // P1.1 ADC option select P1DIR |= 0x01; // Set P1.0 to output direction for (;;) { ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; P1OUT |= 0x01; ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); P1OUT &= ~0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A1 32x, 1.5V, Repeat Single, DCO // // Description: Use DTC to sample A1 32 times with reference to internal 1.5v. // Vref Software writes to ADC10SC to trigger sample burst. In Mainloop MSP430 // waits in LPM0 to save power until ADC10 conversion complete, ADC10_ISR(DTC) // will force exit from any LPMx in Mainloop on reti. ADC10 internal

// // // // // //

Wait if ADC10 core is active Data buffer start Set P1.0 LED on Sampling and conversion start LPM0, ADC10_ISR will force exit Clear P1.0 LED off

// oscillator times sample period (16x) and conversion (13x). DTC transfers // conversion code to RAM 200h - 240h. P1.0 set at start of conversion burst, // reset on completion. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.1/A1 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = CONSEQ_2+INCH_1; // Repeat single channel ADC10CTL0 = SREF_1 + ADC10SHT_2 + MSC + REFON + ADC10ON + ADC10IE; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); ADC10DTC1 = 0x20; // 32 conversions ADC10AE0 |= 0x02; // P1.1 ADC option select P1DIR |= 0x01; // Set P1.0 output for (;;) { ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; P1OUT |= 0x01; ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); P1OUT &= ~0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); } #pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; }

// // // // // //

Wait if ADC10 core is active Data buffer start Set P1.0 LED on Sampling and conversion start LPM0, ADC10_ISR will force exit Clear P1.0 LED off

// Clear CPUOFF bit from 0(SR)

// Exit LPM0 on return

//****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A10 32x, 1.5V, Repeat Single, DCO // // Description: Use DTC to sample A10 32 times with reference to internal 1.5v. // Vref Software writes to ADC10SC to trigger sample burst. In Mainloop MSP430 // waits in LPM0 to save power until ADC10 conversion complete, ADC10_ISR(DTC) // will force exit from any LPMx in Mainloop on reti. (ADC10OSC/4)/64 // determines sample time which needs to be greater than 30us for temperature // sensor. DTC transfers conversion code to RAM 200h - 240h. P1.0 set at start // of conversion burst, reset oncompletion. Temperature sensor offset and slope // will vary from device to device per datasheet tolerance. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // |A10 P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_10 + ADC10DIV_3 + CONSEQ_2; ADC10CTL0 = SREF_1 + ADC10SHT_3 + MSC + REFON + ADC10ON + ADC10IE; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); ADC10DTC1 = 0x20; // 32 conversions P1DIR |= 0x01; // Set P1.0 output for (;;) { ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; P1OUT |= 0x01; ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); P1OUT &= ~0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(CPUOFF); }

// // // // // // //

Wait if ADC10 core is active Data buffer start P1.0 = 1 Sampling and conversion start LPM0, ADC10_ISR will force exit P1.0 = 0

// Clear CPUOFF bit from 0(SR)

#pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A2-0, AVcc, Single Sequence, DCO // // Description: Sample A3/A2/A1 as single sequence with reference to AVcc. // Software sets ADC10SC to trigger sample sequence. In Mainloop MSP430 waits // in LPM0 to save power until ADC10 conversion complete, ADC10_ISR(DTC) will // force exit from any LPMx in Mainloop on reti. ADC10_ISR will force any LPMx // exit. ADC10 internal oscillator times sample period (16x) and conversion // (13x). DTC transfers conversion code to RAM 200h - 206h. P1.0 set at start // of conversion burst, reset on completion. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.3/A3 P1.0|-->LED // >---|P1.2/A2 | // >---|P1.1/A1 | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_3 + CONSEQ_1; // A3/A2/A1, single sequence ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; ADC10DTC1 = 0x03; // 3 conversions ADC10AE0 |= 0x0E; // P1.3,2,1 ADC10 option select P1DIR |= 0x01; // Set P1.0 output for (;;) { ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; P1OUT |= 0x01; ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); P1OUT &= ~0x01; } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) {

// // // // // //

Wait if ADC10 core is active Data buffer start P1.0 = 1 Sampling and conversion start LPM0, ADC10_ISR will force exit P1.0 = 0

__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A1, 1.5V, TA1 Trig, Set P1.0 if > 0.5 V // // Description: A1 is sampled 16/second (ACLK/2048) with reference to 1.5V. // Timer_A is run in upmode and TA1 is used to automatically trigger // ADC10 conversion, TA0 defines the period. Internal oscillator times sample // (16x) and conversion (13x). Inside ADC10_ISR if A1 > 0.5Vcc, P1.0 is set, // else reset. Normal mode is LPM3. // //* An external watch crystal on XIN XOUT is required for ACLK *// // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // >---|P1.1/A1 P1.0 |--> LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = SHS_1 + CONSEQ_2 + INCH_1; // TA1 trigger sample start ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 + MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); ADC10CTL0 |= ENC; // ADC10 Enable ADC10AE0 |= 0x02; // P1.1 ADC10 option select P1DIR |= 0x01; // Set P1.0 output TACCR0 = 2048-1; // PWM Period TACCTL1 = OUTMOD_3; // TACCR1 set/reset TACCR1 = 2047; // TACCR1 PWM Duty Cycle TACTL = TASSEL_1 + MC_1; // ACLK, up mode __bis_SR_register(LPM3_bits + GIE); } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { if (ADC10MEM < 0x155) P1OUT &= ~0x01; else P1OUT |= 0x01; } // Enter LPM3 w/ interrupts

// ADC10MEM = A1 > 0.5V? // Clear P1.0 LED off // Set P1.0 LED on

#pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A7, 1.5V, TA1 Trig, Ultra-Low Pwr // // Description: A7 is sampled 1024/second (32xACLK)with reference to 1.5V. All // activity is interrupt driven with proper usage of MSP430 low-power modes, // ADC10 and Vref demonstrated. Timer_A with both TA1/TA0 used in upmode to // drive ADC10 conversion (continuous mode can also be used). Inside // of TA0_ISR software will enable ADC10 and internal reference and // allow > 30us delay for Vref to stabilize prior to sample start. Sample // start is automatically triggered by TA1 every 32 ACLK cycles. ADC10_ISR // will disable ADC10 and Vref and compare ADC10 conversion code. Internal // oscillator times sample (16x) and conversion (13x). If A7 > 0.2Vcc, // P1.0 is set, else reset. Normal Mode is LPM3. // //* An external watch crystal on XIN XOUT is required for ACLK *// // // +-----(0.9766us)---------\\------------------>+ // TA0_ISR TA1 ADC10_ISR TA0_ISR TA1 // -----+------------+------------+-----------\\------+------------+-----> // Enable ADC Trigger ADC Disable ADC // and Vref Compare // +-( >30us--->+ // // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // >---|P1.7/A7 P1.0 |--> LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; ADC10CTL1 = INCH_7 + SHS_1; ADC10AE0 = 0x80; P1DIR |= 0x01; TACCTL0 = CCIE; TACCR0 = 32-1; TACCTL1 = OUTMOD_3; TACCR1 = 2; TACTL = TASSEL_1 + MC_1; __bis_SR_register(LPM3_bits + GIE); }

// Stop WDT // // // // // // // // // P1.7, TA1 trigger sample start P1.7 ADC10 option select Set P1.0 to output direction Enable interrupt PWM Period TACCR1 set/reset TACCR1 PWM Duty Cycle ACLK, up mode Enter LPM3, enable interrupts

// ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { ADC10CTL0 &= ~ENC; ADC10CTL0 = 0; if (ADC10MEM < 0x88) P1OUT &= ~0x01; else P1OUT |= 0x01; }

// // // //

ADC10 disabled ADC10, Vref disabled completely ADC10MEM = A7 > 0.2V? Clear P1.0 LED off

// Set P1.0 LED on

// Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A(void) { ADC10CTL0 = SREF_1 + ADC10SHT_2 + REFON + ADC10ON + ADC10IE; ADC10CTL0 |= ENC; // ADC10 enable set } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A1 32x, AVcc, TA0 Trig, DCO // // Description; A1 is sampled in 32x burst using DTC 16 times per second // (ACLK/2048) with reference to AVcc. Activity is interrupt driven. // Timer_A in upmode uses TA0 toggle to drive ADC10 conversion. Sample burst // is automatically triggered by TA0 rising edge every 2048 ACLK cycles. // ADC10_ISR will exit from LPM3 mode and return CPU active. Internal ADC10OSC // times sample (16x) and conversion (13x). DTC transfers conversion code to // RAM 200h - 240h. In the Mainloop P1.0 is toggled. Normal Mode is LPM3. // //* An external watch crystal on XIN XOUT is required for ACLK *// // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // >---|P1.1/A1 P1.0 |--> LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_1 + SHS_2 + CONSEQ_2; // TA0 trigger ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; ADC10DTC1 = 0x20; // 32 conversions P1DIR |= 0x01; // Set P1.0 output ADC10AE0 |= 0x02; // P1.1 ADC10 option select TACCR0 = 1024-1; // PWM Period TACCTL0 = OUTMOD_4; // TACCR0 toggle TACTL = TASSEL_1 + MC_1; // ACLK, up mode for (;;) {

ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; ADC10CTL0 |= ENC; __bis_SR_register(LPM3_bits + GIE); P1OUT ^= 0x01; } }

// // // // //

Wait if ADC10 core is active Data buffer start Sampling and conversion ready Enter LPM3, enable interrupts Toggle P1.0 using exclusive-OR

// ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR(void) { __bic_SR_register_on_exit(LPM3_bits); // Clear LPM3 bits from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A1-0 16x, AVcc, Repeat Seq, DCO // // Description: Use DTC to sample A1/A0 repeat sequence 16x(32 total samples) // with reference to AVcc. Software sets ADC10SC to trigger sample burst. // In Mainloop MSP430 waits in LPM0 to save power until ADC10 conversion // complete, ADC10_ISR will force exit from any LPMx in Mainloop on reti. // ADC10 internal oscillator times sample period (16x) and conversion (13x). // DTC transfers conversion code to RAM 200h - 240h. ADC10(DTC) interrupt // will return system active. P1.0 set at start of conversion burst, reset // on completion. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.1/A1 | // >---|P1.0/A0 | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_1 + CONSEQ_3; // A1/A0, repeat multi channel ADC10CTL0 = ADC10SHT_2 + MSC + ADC10ON + ADC10IE; ADC10AE0 = 0x03; // P1.0,1 ADC option select ADC10DTC1 = 0x20; // 16 conversions for (;;) { ADC10CTL0 &= ~ENC; while (ADC10CTL1 & BUSY); ADC10SA = 0x200; ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE); _NOP(); _NOP();

// // // // // //

Wait if ADC10 core is active Data buffer start Sampling and conversion ready LPM0, ADC10_ISR will force exit space for debugger Set Breakpoint here to read ADC

} } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { __bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR) } //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, DTC Sample A0 -> TA1, AVcc, DCO // // Description: Use DTC to sample A0 with reference to AVcc and directly // transfer code to TACCR1. Timer_A has been configured for 10-bit PWM mode. // TACCR1 duty cycle is automatically proportional to ADC10 A0. WDT_ISR used // as a period wakeup timer approximately 45ms based on default ~1.2MHz // DCO/SMCLK clock source used in this example for the WDT clock source. // Timer_A also uses default DCO. // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // >---|P1.0/A0 P1.2|--> TACCR1 - 0-1024 PWM // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" void main(void) { WDTCTL = WDT_MDLY_32; IE1 |= WDTIE; ADC10CTL0 = ADC10SHT_2 + ADC10ON; ADC10AE0 |= 0x01; ADC10DTC1 = 0x001; P1DIR |= 0x04; P1SEL |= 0x04; TACCR0 = 1024 - 1; TACCTL1 = OUTMOD_7; TACCR1 = 512; TACTL = TASSEL_2 + MC_1; while(1) { __bis_SR_register(LPM0_bits + GIE); ADC10SA = (unsigned int)&TACCR1; ADC10CTL0 |= ENC + ADC10SC; } } #pragma vector = WDT_VECTOR __interrupt void WDT_ISR(void) { __bic_SR_register_on_exit(LPM0_bits);

// WDT ~45ms interval timer // Enable WDT interrupt // // // // // // // // P1.0 ADC option select 1 conversion P1.2 = output P1.2 = TA1 output PWM Period TACCR1 reset/set TACCR1 PWM Duty Cycle SMCLK, upmode

// LPM0, WDT_ISR will force exit // Data transfer location // Start sampling

// Exit LPM0

} //****************************************************************************** // MSP430G2x33/G2x53 Demo - ADC10, Sample A10 Temp and Convert to oC and oF // // Description: A single sample is made on A10 with reference to internal // 1.5V Vref. Software sets ADC10SC to start sample and conversion - ADC10SC // automatically cleared at EOC. ADC10 internal oscillator/4 times sample // (64x) and conversion. In Mainloop MSP430 waits in LPM0 to save power until // ADC10 conversion complete, ADC10_ISR will force exit from any LPMx in // Mainloop on reti. Temperaure in oC stored in IntDegC, oF in IntDegF. // Uncalibrated temperature measured from device to device will vary with // slope and offset - please see datasheet. // ACLK = n/a, MCLK = SMCLK = default DCO ~1.2MHz, ADC10CLK = ADC10OSC/4 // // MSP430G2x33/G2x53 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // |A10 | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include "msp430g2553.h" long temp; long IntDegF; long IntDegC; void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop WDT ADC10CTL1 = INCH_10 + ADC10DIV_3; // Temp Sensor ADC10CLK/4 ADC10CTL0 = SREF_1 + ADC10SHT_3 + REFON + ADC10ON + ADC10IE; __enable_interrupt(); // Enable interrupts. TACCR0 = 30; // Delay to allow Ref to settle TACCTL0 |= CCIE; // Compare-mode interrupt. TACTL = TASSEL_2 | MC_1; // TACLK = SMCLK, Up mode. LPM0; // Wait for delay. TACCTL0 &= ~CCIE; // Disable timer Interrupt __disable_interrupt(); while(1) { ADC10CTL0 |= ENC + ADC10SC; __bis_SR_register(CPUOFF + GIE);

// Sampling and conversion start // LPM0 with interrupts enabled

// oF = ((A10/1024)*1500mV)-923mV)*1/1.97mV = A10*761/1024 - 468 temp = ADC10MEM; IntDegF = ((temp - 630) * 761) / 1024; // oC = ((A10/1024)*1500mV)-986mV)*1/3.55mV = A10*423/1024 - 278 temp = ADC10MEM; IntDegC = ((temp - 673) * 423) / 1024;

__no_operation(); } } // ADC10 interrupt service routine #pragma vector=ADC10_VECTOR __interrupt void ADC10_ISR (void) { __bic_SR_register_on_exit(CPUOFF); }

// SET BREAKPOINT HERE

// Clear CPUOFF bit from 0(SR)

#pragma vector=TIMER0_A0_VECTOR __interrupt void ta0_isr(void) { TACTL = 0; LPM0_EXIT; // Exit LPM0 on return } //****************************************************************************** // MSP430G2xx3 Demo - Software Toggle P1.0 // // Description; Toggle P1.0 by xor'ing P1.0 inside of a software loop. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; for (;;) { volatile unsigned int i; P1OUT ^= 0x01; i = 50000; do (i--); while (i != 0); // Toggle P1.0 using exclusive-OR // Delay

// Stop watchdog timer // Set P1.0 to output direction

} } //****************************************************************************** // MSP430G2xx3 Demo - Software Toggle P1.0, MCLK = VLO/8 // // Description; Pulse P1.0 with a 1/100 active duty cycle using software. // Ultra-low frequency ~ 1.5kHz, ultra-low power active mode demonstrated.

// ACLK = VL0, MCLK = VLO/8 ~1.5kHz, SMCLK = n/a // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version: 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { volatile unsigned int i; WDTCTL = WDTPW + WDTHOLD; BCSCTL3 |= LFXT1S_2; IFG1 &= ~OFIFG; __bis_SR_register(SCG1 + SCG0); BCSCTL2 |= SELM_3 + DIVM_3; P1DIR = 0xFF; P1OUT = 0; P2DIR = 0xFF; P2OUT = 0; for (;;) { P1OUT |= for (i = P1OUT &= for (i = }

// // // // // // // // // //

Volatile to prevent removal Stop watchdog timer LFXT1 = VLO Clear OSCFault flag Stop DCO MCLK = LFXT1/8 All P1.x outputs All P1.x reset All P2.x outputs All P2.x reset

0x01; 10; i > 0; i--); ~0x01; 1000; i > 0; i--);

// // // //

P1.0 set Delay 1x P1.0 reset Delay 100x

} //****************************************************************************** // MSP430G2xx3 Demo - Basic Clock, Output Buffered SMCLK, ACLK and MCLK/10 // // Description: Buffer ACLK on P2.0, default SMCLK(DCO) on P1.4 and MCLK/10 on // P1.1. // ACLK = LFXT1 = 32768, MCLK = SMCLK = default DCO // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.4/SMCLK|-->SMCLK = Default DCO // | P1.1|-->MCLK/10 = DCO/10 // | P1.0/ACLK|-->ACLK = 32kHz // // D. Dang // Texas Instruments Inc. // December 2010

// Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW +WDTHOLD; P1DIR |= 0x13; P1SEL |= 0x11; while(1) { P1OUT |= 0x02; P1OUT &= ~0x02; } } //****************************************************************************** // MSP430G2xx3 Demo - Basic Clock, Output Buffered clocks with preloaded DCO // calibration constants for BCSCTL1 and DCOCTL. // // Description: Buffer ACLK on P2.0, default SMCLK(DCO) on P1.4 and MCLK/10 on // P1.1. DCO is software selectable to 1, 8, 12, or 16Mhz using calibration // contstants in INFOA. // // ACLK = LFXT1 = 32768, MCLK = SMCLK = Selectable at 1, 8, 12 or 16Mhz // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.4/SMCLK|-->SMCLK = Default DCO // | P1.1|-->MCLK/10 = DCO/10 // | P1.0/ACLK|-->ACLK = 32kHz // // D. Dang // Texas Instruments Inc. // December 2010 // Built with IAR Embedded Workbench Version: 3.42A //****************************************************************************** #include void main(void) { WDTCTL = WDTPW +WDTHOLD; // Stop Watchdog Timer if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) { while(1); } //1Mhz BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; // If calibration constants erased // do not load, trap CPU!! // Set range // Set DCO step + modulation */

// Stop Watchdog Timer // P1.0,1 and P1.4 outputs // P1.0,4 ACLK, SMCLK output

// P1.1 = 1 // P1.1 = 0

/* //8Mhz BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; /* //12Mhz BCSCTL1 = CALBC1_12MHZ; DCOCTL = CALDCO_12MHZ; /* //16Mhz BCSCTL1 = CALBC1_16MHZ; DCOCTL = CALDCO_16MHZ; P1DIR |= 0x13; P1SEL |= 0x11; while(1) { P1OUT |= 0x02; P1OUT &= ~0x02; } }

// Set range // Set DCO step + modulation */ // Set range // Set DCO step + modulation*/ // Set range // Set DCO step + modulation*/ // P1.0,1 and P1.4 outputs // P1.0,4 ACLK, SMCLK output

// P1.1 = 1 // P1.1 = 0

//****************************************************************************** // MSP430G2xx3 Demo - DCO Calibration Constants Programmer // // NOTE: THIS CODE REPLACES THE TI FACTORY-PROGRAMMED DCO CALIBRATION // CONSTANTS LOCATED IN INFOA WITH NEW VALUES. USE ONLY IF THE ORIGINAL // CONSTANTS ACCIDENTALLY GOT CORRUPTED OR ERASED. // // Description: This code re-programs the G2xx2 DCO calibration constants. // A software FLL mechanism is used to set the DCO based on an external // 32kHz reference clock. After each calibration, the values from the // clock system are read out and stored in a temporary variable. The final // frequency the DCO is set to is 1MHz, and this frequency is also used // during Flash programming of the constants. The program end is indicated // by the blinking LED. // ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = target DCO // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // --------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.0|--> LED // | P1.4|--> SMLCK = target DCO // // D. Dang // Texas Instruments Inc. // May 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 3.42A //****************************************************************************** #include "msp430g2553.h" #define #define #define #define DELTA_1MHZ DELTA_8MHZ DELTA_12MHZ DELTA_16MHZ 244 1953 2930 3906 // // // // 244 x 4096Hz = 999.4Hz 1953 x 4096Hz = 7.99MHz 2930 x 4096Hz = 12.00MHz 3906 x 4096Hz = 15.99MHz

unsigned char CAL_DATA[8]; volatile unsigned int i; int j; char *Flash_ptrA; void Set_DCO(unsigned int Delta); void main(void) { WDTCTL = WDTPW + WDTHOLD; for (i = 0; i < 0xfffe; i++); P1OUT = 0x00; P1SEL = 0x10; P1DIR = 0x11; j = 0; Set_DCO(DELTA_16MHZ); CAL_DATA[j++] = DCOCTL; CAL_DATA[j++] = BCSCTL1; Set_DCO(DELTA_12MHZ); CAL_DATA[j++] = DCOCTL; CAL_DATA[j++] = BCSCTL1; Set_DCO(DELTA_8MHZ); CAL_DATA[j++] = DCOCTL; CAL_DATA[j++] = BCSCTL1; Set_DCO(DELTA_1MHZ); CAL_DATA[j++] = DCOCTL; CAL_DATA[j++] = BCSCTL1; Flash_ptrA = (char *)0x10C0; FCTL2 = FWKEY + FSSEL0 + FN1; FCTL1 = FWKEY + ERASE; FCTL3 = FWKEY + LOCKA; *Flash_ptrA = 0x00; FCTL1 = FWKEY + WRT; Flash_ptrA = (char *)0x10F8; for (j = 0; j < 8; j++) *Flash_ptrA++ = CAL_DATA[j]; FCTL1 = FWKEY; FCTL3 = FWKEY + LOCKA + LOCK; while (1) { P1OUT ^= 0x01; for (i = 0; i < 0x4000; i++); } } void Set_DCO(unsigned int Delta) { unsigned int Compare, Oldcapture = 0; BCSCTL1 |= DIVA_3; TACCTL0 = CM_1 + CCIS_1 + CAP; TACTL = TASSEL_2 + MC_2 + TACLR; while (1)

// Temp. storage for constants // Segment A pointer

// // // // //

Stop WDT Delay for XTAL stabilization Clear P1 output latches P1.4 SMCLK output P1.0,4 output

// Reset pointer // Set DCO and obtain constants

// Set DCO and obtain constants

// Set DCO and obtain constants

// Set DCO and obtain constants

// // // // // // //

Point to beginning of seg A MCLK/3 for Flash Timing Generator Set Erase bit Clear LOCK & LOCKA bits Dummy write to erase Flash seg A Set WRT bit for write operation Point to beginning of cal consts

// re-flash DCO calibration data // Clear WRT bit // Set LOCK & LOCKA bit

// Toggle LED // SW Delay

// Set DCO to selected frequency

// ACLK = LFXT1CLK/8 // CAP, ACLK // SMCLK, cont-mode, clear

{ while (!(CCIFG & TACCTL0)); TACCTL0 &= ~CCIFG; Compare = TACCR0; Compare = Compare - Oldcapture; Oldcapture = TACCR0; if (Delta == Compare) break; else if (Delta < Compare) { DCOCTL--; if (DCOCTL == 0xFF) if (BCSCTL1 & 0x0f) BCSCTL1--; } else { DCOCTL++; if (DCOCTL == 0x00) if ((BCSCTL1 & 0x0f) != 0x0f) BCSCTL1++; } } TACCTL0 = 0; TACTL = 0; BCSCTL1 &= ~DIVA_3; // // // // // Wait until capture occured Capture occured, clear flag Get current captured SMCLK SMCLK difference Save current captured SMCLK

// If equal, leave "while(1)" // DCO is too fast, slow it down // Did DCO roll under? // Select lower RSEL

// DCO is too slow, speed it up // Did DCO roll over? // Sel higher RSEL // Stop TACCR0 // Stop Timer_A // ACLK = LFXT1CLK

} //****************************************************************************** // MSP430G2xx3 Demo - Flash In-System Programming, Copy SegC to SegD // // Description: This program first erases flash seg C, then it increments all // values in seg C, then it erases seg D, then copies seg C to seg D. // Assumed MCLK 771kHz - 1428kHz. // //* Set Breakpoint on NOP in the Mainloop to avoid Stressing Flash *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include char value; // Function prototypes void write_SegC (char value); void copy_C2D (void); void main(void) { WDTCTL = WDTPW + WDTHOLD; // 8-bit value to write to segment A

// Stop watchdog timer

if (CALBC1_1MHZ ==0xFF || CALDCO_1MHZ == 0xFF) { while(1); } BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; FCTL2 = FWKEY + FSSEL0 + FN1; value = 0; while(1) { write_SegC(value++); copy_C2D(); _NOP(); } } void write_SegC (char value) { char *Flash_ptr; unsigned int i; Flash_ptr = (char *) 0x1040; FCTL1 = FWKEY + ERASE; FCTL3 = FWKEY; *Flash_ptr = 0; t FCTL1 = FWKEY + WRT; for (i=0; i 0; i--); // Time for flag to set P1OUT ^= 0x01; // Toggle P1.0 using exclusive-OR } while (IFG1 & OFIFG); // OSCFault flag still set? IE1 |= OFIE; // Enable Osc Fault } //****************************************************************************** // MSP430G2xx3 Demo - Basic Clock, LPM3 Using WDT ISR, 32kHz ACLK // // Description: This program operates MSP430 normally in LPM3, pulsing P1.0 // at 4 second intervals. WDT ISR used to wake-up system. All I/O configured // as low outputs to eliminate floating inputs. Current consumption does // increase when LED is powered on P1.0. Demo for measuring LPM3 current. // ACLK = LFXT1/4 = 32768/4, MCLK = SMCLK = default DCO ~ 800kHz // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // // MSP430G2xx3 // --------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { BCSCTL1 |= DIVA_2; WDTCTL = WDT_ADLY_1000; IE1 |= WDTIE; P1DIR = 0xFF; P1OUT = 0; P2DIR = 0xFF; P2OUT = 0; while(1) { int i; P1OUT |= 0x01; for (i = 5000; i>0; i--); P1OUT &= ~0x01; _BIS_SR(LPM3_bits + GIE); } }

// // // // // // //

ACLK/4 WDT 1s/4 interval timer Enable WDT interrupt All P1.x outputs All P1.x reset All P2.x outputs All P2.x reset

// // // //

Set P1.0 LED on Delay Reset P1.0 LED off Enter LPM3

#pragma vector=WDT_VECTOR __interrupt void watchdog_timer (void) { _BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) } //****************************************************************************** // MSP430G2xx3 Demo - Basic Clock, LPM3 Using WDT ISR, VLO ACLK // // Description: This program operates MSP430 normally in LPM3, pulsing P1.0 // ~ 6 second intervals. WDT ISR used to wake-up system. All I/O configured // as low outputs to eliminate floating inputs. Current consumption does // increase when LED is powered on P1.0. Demo for measuring LPM3 current. // ACLK = VLO/2, MCLK = SMCLK = default DCO // // // MSP430G2xx3 // --------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { BCSCTL1 |= DIVA_1; BCSCTL3 |= LFXT1S_2; WDTCTL = WDT_ADLY_1000; IE1 |= WDTIE; P1DIR = 0xFF; P1OUT = 0; P2DIR = 0xFF; P2OUT = 0; while(1) { int i; P1OUT |= 0x01; for (i = 10000; i>0; i--); P1OUT &= ~0x01; _BIS_SR(LPM3_bits + GIE); } } #pragma vector=WDT_VECTOR __interrupt void watchdog_timer (void) { _BIC_SR_IRQ(LPM3_bits); // Clear LPM3 bits from 0(SR) } //******************************************************************************

// // // // // // // //

ACLK/2 ACLK = VLO Interval timer Enable WDT interrupt All P1.x outputs All P1.x reset All P2.x outputs All P2.x reset

// // // //

Set P1.0 LED on Delay Reset P1.0 LED off Enter LPM3

// MSP430G2xx3 Demo - Basic Clock, Configure RST/NMI as NMI // // Description: Configure RST/NMI as NMI, hi/lo edge. Flash P1.0 inside of // NMI_ISR if NMI occurs. General enable interrupt in status register does // not need to be set for NMI. NMIIE does need to be reset, as NMI_ISR // automatically clears NMI enable to prevent unintentional stack overflow // that could result from, bounce or uncontrolled NMI's. // ACLK = n/a, MCLK = SMCLK = DCO ~ 800k // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD + WDTNMI + WDTNMIES; // WDT off NMI hi/lo P1DIR |= 0x01; // Set P1.0 to output direction P1OUT &= ~0x01; // Clear P1.0 LED off IE1 |= NMIIE; // Enable NMI _BIS_SR(LPM0_bits); } #pragma vector=NMI_VECTOR __interrupt void nmi_ (void) { volatile unsigned int i; P1OUT |= 0x01; // Set P1.0 LED on for (i = 20000; i > 0; i--); // Delay P1OUT &= ~0x01; // Clear P1.0 LED off IFG1 &= ~NMIIFG; // Reclear NMI flag in case bounce IE1 |= NMIIE; // Enable NMI } //****************************************************************************** // MSP430G2xx3 Demo - Software Poll P1.4, Set P1.0 if P1.4 = 1 // // Description: Poll P1.4 in a loop, if hi P1.0 is set, if low, P1.0 reset. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// /|\ | | // --o--|P1.4 P1.0|-->LED // \|/ // // D. Dang // Enter LPM0

// Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; while (1) { if ((0x10 & P1IN)) P1OUT |= 0x01; else P1OUT &= ~0x01; }

// Stop watchdog timer // Set P1.0 to output direction // Test P1.4 // if P1.4 set, set P1.0 // else reset

} //****************************************************************************** // MSP430G2xx3 Demo - Software Port Interrupt Service on P1.4 from LPM4 // // Description: A hi/low transition on P1.4 will trigger P1_ISR which, // toggles P1.0. Normal mode is LPM4 ~ 0.1uA. LPM4 current can be measured // with the LED removed, all unused P1.x/P2.x configured as output or inputs // pulled high or low, and ensure the P1.4 interrupt input does not float. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// /|\ | | // --o--|P1.4 P1.0|-->LED // \|/ // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; P1IE |= 0x10; P1IES |= 0x10; P1IFG &= ~0x10; _BIS_SR(LPM4_bits + GIE); } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { P1OUT ^= 0x01;

// // // // //

Stop watchdog timer Set P1.0 to output direction P1.4 interrupt enabled P1.4 Hi/lo edge P1.4 IFG cleared

// Enter LPM4 w/interrupt

// P1.0 = toggle

P1IFG &= ~0x10; }

// P1.4 IFG cleared

//****************************************************************************** // MSP430G2xx3 Demo - Poll P1 With Software with Internal Pull-up // // Description: Poll P1.4 in a loop, if hi P1.0 is set, if low, P1.0 reset. // Internal pullup enabled on P1.4. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// /|\ | R | // --o--| P1.4-o P1.0|-->LED // \|/ // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR = 0x01; P1OUT = 0x10; P1REN |= 0x10; while (1) { if (0x10 & P1IN) P1OUT |= 0x01; else P1OUT &= ~0x01; } } //****************************************************************************** // MSP430G2xx3 Demo - P1 Interrupt from LPM4 with Internal Pull-up // // Description: A hi/low transition on P1.4 will trigger P1_ISR which, // toggles P1.0. Normal mode is LPM4 ~ 0.1uA. // Internal pullup enabled on P1.4. // ACLK = n/a, MCLK = SMCLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// /|\ | R | // --o--| P1.4-o P1.0|-->LED // \|/ // // D. Dang

// // // //

Stop P1.0 P1.4 P1.4

watchdog timer output, else input set, else reset pullup

// Test P1.4 // if P1.4 set, set P1.0 // else reset

// Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR = 0x01; P1OUT = 0x10; P1REN |= 0x10; P1IE |= 0x10; P1IES |= 0x10; P1IFG &= ~0x10; _BIS_SR(LPM4_bits + GIE); } // Port 1 interrupt service routine #pragma vector=PORT1_VECTOR __interrupt void Port_1(void) { P1OUT ^= 0x01; P1IFG &= ~0x10; }

// // // // // // //

Stop P1.0 P1.4 P1.4 P1.4 P1.4 P1.4

watchdog timer output, else input set, else reset pullup interrupt enabled Hi/lo edge IFG cleared

// Enter LPM4 w/interrupt

// P1.0 = toggle // P1.4 IFG cleared

//****************************************************************************** // MSP430G2xx3 Demo - Capacitive Touch, Pin Oscillator Method, 1 button // // Description: Basic 1-button input using the built-in pin oscillation feature // on GPIO input structure. PinOsc signal feed into TA0CLK. WDT interval is use d // to gate the measurements. Difference in measurements indicate button touch. // LEDs flash if input is touched. // // ACLK = VLO = 12kHz, MCLK = SMCLK = 1MHz DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1| 1; delta_cnt = 0; // } if (delta_cnt > j) // { // j = delta_cnt; key_pressed = 1; // } else key_pressed = 1; /* Delay to next sample, sample if (key_pressed) { BCSCTL1 = (BCSCTL1 & 0x0CF) + cycles = 20; } else { cycles--; if (cycles > 0) BCSCTL1 = (BCSCTL1 & 0x0CF) else { BCSCTL1 = (BCSCTL1 & 0x0CF) cycles = 0; } } WDTCTL = WDT_delay_setting; C decrease*/ If negative: result increased beyond baseline, i.e. cap dec // Re-average quickly Zero out for pos determination Determine if each key is pressed per a preset threshold key pressed

more slowly if no keys are pressed*/ DIVA_0; // ACLK/(0:1,1:2,2:4,3:8)

+ DIVA_0; // ACLK/(0:1,1:2,2:4,3:8) + DIVA_3; // ACLK/(0:1,1:2,2:4,3:8)

// WDT, ACLK, interval timer

/* Handle baseline measurment for a base C increase*/ if (!key_pressed) // Only adjust baseline down { // if no keys are touched base_cnt = base_cnt - 1; // Adjust baseline down, should be } // slow to accomodate for genuine pulse_LED(); // changes in sensor C __bis_SR_register(LPM3_bits); } } // End Main /* Measure count result (capacitance) of each sensor*/ /* Routine setup for four sensors, not dependent on NUM_SEN value!*/ void measure_count(void)

{ TA0CTL = TASSEL_3+MC_2; TA0CCTL1 = CM_3+CCIS_2+CAP; // TACLK, cont mode // Pos&Neg,GND,Cap

/*Configure Ports for relaxation oscillator*/ /*The P2SEL2 register allows Timer_A to receive it's clock from a GPIO*/ /*See the Application Information section of the device datasheet for info*/ P1DIR &= ~ BIT1; // P1.1 is the input used here P1SEL &= ~ BIT1; P1SEL2 |= BIT1; /*Setup Gate Timer*/ WDTCTL = WDT_meas_setting; TA0CTL |= TACLR; __bis_SR_register(LPM0_bits+GIE); TA0CCTL1 ^= CCIS0; meas_cnt = TACCR1; WDTCTL = WDTPW + WDTHOLD; P1SEL2 &= ~BIT1; } void pulse_LED(void) { if(key_pressed) { P1OUT ^= LED_1 + LED_2; }else{ P1OUT = 0; } } /* Watchdog Timer interrupt service routine*/ #pragma vector=WDT_VECTOR __interrupt void watchdog_timer(void) { TA0CCTL1 ^= CCIS0; // Create SW capture of CCR1 __bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 on reti } //****************************************************************************** // MSP430G2xx3 Demo - Capacitive Touch, Pin Oscillator Method, 4-buttons // // Description: Basic 4-button input using the built-in pin oscillation feature // on GPIO input structure. PinOsc signal feed into TA0CLK. WDT interval is use d // to gate the measurements. Difference in measurements indicate button touch. // LEDs flash if input is touched. // // Input 1: LED1 (LED2 off) // Input 2: LED2 (LED1 off) // Input 3: Both LEDs on // Input 4: Both LEDs flash on/off // // ACLK = VLO = 12kHz, MCLK = SMCLK = 1MHz DCO // // MSP430G2xx3 // ----------------// // // // // // WDT, ACLK, interval timer Clear Timer_A TAR Wait for WDT interrupt Create SW capture of CCR1 Save result Stop watchdog timer

// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1| 1; // Stop Timer_A

// TX on CCI0B? // Add Offset to CCR0 // All bits TXed, disable interrupt

// TX Mark // TX Space

BitCnt --; } CCTL0 &= ~ CCIFG; } else TACCTL0 &= ~CCIE; // for LED gradient only // interrupt disbled

__bic_SR_register_on_exit(LPM0_bits); } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, CCR0 Cont. Mode ISR, DCO SMCLK // // Description: Toggle P1.0 using software and TA_0 ISR. Toggles every // 50000 SMCLK cycles. SMCLK provides clock source for TACLK. // During the TA_0 ISR, P1.0 is toggled and 50000 clock cycles are added to // CCR0. TA_0 ISR is triggered every 50000 cycles. CPU is normally off and // used only during TA_ISR. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO // // MSP430G2xx3 // --------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CCTL0 = CCIE; CCR0 = 50000; TACTL = TASSEL_2 + MC_2; _BIS_SR(LPM0_bits + GIE); } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) { P1OUT ^= 0x01; // Toggle P1.0 CCR0 += 50000; // Add Offset to CCR0 } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, CCR0 Up Mode ISR, DCO SMCLK // // Description: Toggle P1.0 using software and TA_0 ISR. Timer_A is // configured for up mode, thus the timer overflows when TAR counts to CCR0. // In this example CCR0 is loaded with 50000. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO

// Stop WDT // P1.0 output // CCR0 interrupt enabled // SMCLK, contmode // Enter LPM0 w/ interrupt

// // // MSP430G2xx3 // --------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CCTL0 = CCIE; CCR0 = 50000; TACTL = TASSEL_2 + MC_1; _BIS_SR(LPM0_bits + GIE); } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) { P1OUT ^= 0x01; }

// Stop WDT // P1.0 output // CCR0 interrupt enabled // SMCLK, upmode // Enter LPM0 w/ interrupt

// Toggle P1.0

//****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, Overflow ISR, DCO SMCLK // // Description: Toggle P1.0 using software and Timer_A overflow ISR. // In this example an ISR triggers when TA overflows. Inside the TA // overflow ISR P1.0 is toggled. Toggle rate is approximatlely 12Hz. // Proper use of TA0IV interrupt vector generator is demonstrated. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO // // MSP430G2xx3 // --------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include

void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; TACTL = TASSEL_2 + MC_2 + TAIE; _BIS_SR(LPM0_bits + GIE); }

// Stop WDT // P1.0 output // SMCLK, contmode, interrupt // Enter LPM0 w/ interrupt

// Timer_A3 Interrupt Vector (TA0IV) handler #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer_A(void) { switch( TA0IV ) { case 2: break; // CCR1 not used case 4: break; // CCR2 not used case 10: P1OUT ^= 0x01; // overflow break; } } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, Overflow ISR, 32kHz ACLK // // Description: Toggle P1.0 using software and the Timer_A overflow ISR. // In this example an ISR triggers when TA overflows. Inside the ISR P1.0 // is toggled. Toggle rate is exactly 0.5Hz. Proper use of the TA0IV interrupt // vector generator is demonstrated. // ACLK = TACLK = 32768Hz, MCLK = SMCLK = default DCO // //* An external watch crystal on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // --------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; TACTL = TASSEL_1 + MC_2 + TAIE; _BIS_SR(LPM3_bits + GIE); } // Timer_A3 Interrupt Vector (TA0IV) handler #pragma vector=TIMER0_A1_VECTOR

// Stop WDT // P1.0 output // ACLK, contmode, interrupt // Enter LPM3 w/ interrupt

__interrupt void Timer_A(void) { switch( TA0IV ) { case 2: break; // CCR1 not used case 4: break; // CCR2 not used case 10: P1OUT ^= 0x01; // overflow break; } } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, CCR0 Up Mode ISR, 32kHz ACLK // // Description: Toggle P1.0 using software and the TA_0 ISR. Timer_A is // configured for up mode, thus the the timer overflows when TAR counts // to CCR0. In this example, CCR0 is loaded with 1000-1. // Toggle rate = 32768/(2*1000) = 16.384Hz // ACLK = TACLK = 32768Hz, MCLK = SMCLK = DCO // //* An external watch crystal on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // --------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CCTL0 = CCIE; CCR0 = 1000-1; TACTL = TASSEL_1 + MC_1; _BIS_SR(LPM3_bits + GIE); } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A (void) { P1OUT ^= 0x01; }

// Stop WDT // P1.0 output // CCR0 interrupt enabled // ACLK, upmode // Enter LPM3 w/ interrupt

// Toggle P1.0

//****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0, CCR1 Cont. Mode ISR, DCO SMCLK // // Description: Toggle P1.0 using software and TA_1 ISR. Toggles every // 50000 SMCLK cycles. SMCLK provides clock source for TACLK. // During the TA_1 ISR, P1.0 is toggled and 50000 clock cycles are added to

// CCR0. TA_1 ISR is triggered every 50000 cycles. CPU is normally off and // used only during TA_ISR. Proper use of the TA0IV interrupt vector generator // is demonstrated. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO // // MSP430G2xx3 // --------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.0|-->LED // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x01; CCTL1 = CCIE; CCR1 = 50000; TACTL = TASSEL_2 + MC_2; _BIS_SR(LPM0_bits + GIE); } // Timer_A3 Interrupt Vector (TA0IV) handler #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer_A(void) { switch( TA0IV ) { case 2: // CCR1 { P1OUT ^= 0x01; // Toggle P1.0 CCR1 += 50000; // Add Offset to CCR1 } break; case 4: break; // CCR2 not used case 10: break; // overflow not used } } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0-2, Cont. Mode ISR, DCO SMCLK // // Description: Use Timer_A CCRx units and overflow to generate four // independent timing intervals. For demonstration, CCR0 and CCR1 output // units are optionally selected with port pins P1.1 and P1.2 in toggle // mode. As such, these pins will toggle when respective CCRx registers match // the TAR counter. Interrupts are also enabled with all CCRx units, // software loads offset to next interval only - as long as the interval offset // is aded to CCRx, toggle rate is generated in hardware. Timer_A overflow ISR // is used to toggle P1.0 with software. Proper use of the TA0IV interrupt

// Stop WDT // P1.0 output // CCR1 interrupt enabled // SMCLK, Contmode // Enter LPM0 w/ interrupt

// vector generator is demonstrated. // ACLK = n/a, MCLK = SMCLK = TACLK = default DCO ~1MHz // As coded and assuming ~1MHz DCO, toggle rates are: // P1.1 = CCR0 ~ 1MHz/(2*200) ~2500Hz // P1.2 = CCR1 ~ 1MHz/(2*1000) ~500Hz // P1.0 = overflow ~ 1MHz/(2*65536) ~8Hz // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1/TA0|--> CCR0 // | P1.2/TA1|--> CCR1 // | P1.0|--> Overflow/software // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + P1SEL |= 0x06; P1DIR |= 0x07; CCTL0 = OUTMOD_4 CCTL1 = OUTMOD_4 TACTL = TASSEL_2 } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A0 (void) { CCR0 += 200; }

WDTHOLD; + CCIE; + CCIE; + MC_2 + TAIE;

// // // // // //

Stop WDT P1.1 - P1.2 option select P1.0 - P1.2 outputs CCR0 toggle, interrupt enabled CCR1 toggle, interrupt enabled SMCLK, Contmode, int enabled

_BIS_SR(LPM0_bits + GIE);

// Enter LPM0 w/ interrupt

// Add Offset to CCR0

// Timer_A2 Interrupt Vector (TA0IV) handler #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer_A1(void) { switch( TA0IV ) { case 2: CCR1 += 1000; // Add Offset to CCR1 break; case 10: P1OUT ^= 0x01; // Timer_A3 overflow break; } } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.0-2, Cont. Mode ISR, 32kHz ACLK //

// Description: Use Timer_A CCRx units and overflow to generate four // independent timing intervals. For demonstration, CCR0 and CCR1 output // units are optionally selected with port pins P1.1 and P1.2 in toggle // mode. As such, these pins will toggle when respective CCRx registers match // the TAR counter. Interrupts are also enabled with all CCRx units, // software loads offset to next interval only - as long as the interval offset // is aded to CCRx, toggle rate is generated in hardware. Timer_A overflow ISR // is used to toggle P1.0 with software. Proper use of TA0IV interrupt vector // generator is demonstrated. // ACLK = TACLK = 32kHz, MCLK = SMCLK = Default DCO // As coded and with TACLK = 32768Hz, toggle rates are: // P1.1 = CCR0 = 32768/(2*4) = 4096Hz // P1.2 = CCR1 = 32768/(2*16) = 1024Hz // P1.0 = overflow = 32768/(2*65536) = 0.25Hz // //* External watch crystal on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // --------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.1/TA0|--> CCR0 // | P1.2/TA1|--> CCR1 // | P1.0|--> Overflow/software // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + P1SEL |= 0x06; P1DIR |= 0x07; CCTL0 = OUTMOD_4 CCTL1 = OUTMOD_4 TACTL = TASSEL_1 } // Timer A0 interrupt service routine #pragma vector=TIMER0_A0_VECTOR __interrupt void Timer_A0 (void) { CCR0 += 4; }

WDTHOLD; + CCIE; + CCIE; + MC_2 + TAIE;

// // // // // //

Stop WDT P1.1 - P1.2 option select P1.0 - P1.2 outputs CCR0 toggle, interrupt enabled CCR1 toggle, interrupt enabled ACLK, contmode, interrupt enabled

_BIS_SR(LPM3_bits + GIE);

// Enter LPM3 w/interrupt

// Add Offset to CCR0

// Timer_A2 Interrupt Vector (TA0IV) handler #pragma vector=TIMER0_A1_VECTOR __interrupt void Timer_A1(void) { switch( TA0IV ) { case 2: CCR1 += 16; // Add Offset to CCR1

break; case 10: P1OUT ^= 0x01; break; } }

// Timer_A3 overflow

//****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.1/TA0, Up Mode, DCO SMCLK // // Description: Toggle P1.1 using hardware TA0 output. Timer_A is configured // for up mode with CCR0 defining period, TA0 also output on P1.1. In this // example, CCR0 is loaded with 500-1 and TA0 will toggle P1.1 at TACLK/500. // Thus the output frequency on P1.1 will be the TACLK/1000. No CPU or // software resources required. // ACLK = n/a, SMCLK = MCLK = TACLK = default DCO // As coded with TACLK = SMCLK, P1.1 output frequency is ~1000000/1000 // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1/TA0|--> SMCLK/1000 // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x02; P1SEL |= 0x02; CCTL0 = OUTMOD_4; CCR0 = 500-1; TACTL = TASSEL_2 + MC_1;

// // // //

Stop P1.1 P1.1 CCR0

WDT output option select toggle mode

// SMCLK, upmode

_BIS_SR(CPUOFF); // CPU off } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.1/TA0, Up Mode, 32kHz ACLK // // Description: Toggle P1.1 using hardware TA0 output. Timer_A is configured // for up mode with CCR0 defining period, TA0 also output on P1.1. In this // example, CCR0 is loaded with 10-1 and TA0 will toggle P1.1 at TACLK/10. // Thus the output frequency on P1.1 will be the TACLK/20. No CPU or software // resources required. Normal operating mode is LPM3. // ACLK= TACLK = 32kHz, MCLK = default DCO // As coded with TACLK = ACLK, P1.1 output frequency = 32768/20 = 1.6384kHz // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz

// --|RST XOUT|// | | // | P1.1/TA0|--> ACLK/20 // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x02; P1SEL |= 0x02; CCTL0 = OUTMOD_4; CCR0 = 10-1; TACTL = TASSEL_1 + MC_1;

// // // //

Stop P1.1 P1.1 CCR0

WDT output option select toggle mode

// ACLK, upmode

_BIS_SR(LPM3_bits); // Enter LPM3 w/interrupt } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.1/TA0, Up/Down Mode, DCO SMCLK // // Description: Toggle P1.1 using hardware TA0 output. Timer_A is configured // for up/down mode with CCR0 defining period, TA0 also output on P1.1. In // this example, CCR0 is loaded with 250 and TA0 will toggle P1.1 at // TACLK/2*250. Thus the output frequency on P1.1 will be the TACLK/1000. // No CPU or software resources required. // ACLK = n/a, SMCLK = MCLK = TACLK = default DCO // As coded with TACLK = SMCLK, P1.1 output frequency is ~1000000/1000 // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.1/TA0|--> SMCLK/1000 // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x02; P1SEL |= 0x02; CCTL0 = OUTMOD_4; CCR0 = 250; TACTL = TASSEL_2 + MC_3; _BIS_SR(CPUOFF);

// // // //

Stop P1.1 P1.1 CCR0

WDT output option select toggle mode

// SMCLK, up-downmode // CPU off

} //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Toggle P1.1/TA0, Up/Down Mode, 32kHz ACLK // // Description: Toggle P1.1 using hardware TA0 output. Timer_A is configured // for up/down mode with CCR0 defining period, TA0 also output on P1.1. In // this example, CCR0 is loaded with 5 and TA0 will toggle P1.1 at TACLK/2*5. // Thus the output frequency on P1.1 will be the TACLK/20. No CPU or software // resources required. Normal operating mode is LPM3. // ACLK = TACLK = 32kHz, MCLK = default DCO // As coded with TACLK = ACLK, P1.1 output frequency = 32768/20 = 1.6384kHz // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32 kHz // --|RST XOUT|// | | // | P1.1/TA0|--> ACLK/20 // // D. Dang // Texas Instruments Inc. // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x02; P1SEL |= 0x02; CCTL0 = OUTMOD_4; CCR0 = 5; TACTL = TASSEL_1 + MC_3;

// // // //

Stop P1.1 P1.1 CCR0

WDT output option select toggle mode

// ACLK, up-downmode

_BIS_SR(LPM3_bits); // Enter LPM3 } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, PWM TA1-2, Up Mode, DCO SMCLK // // 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. // A 75% duty cycle on P1.2. // ACLK = na, SMCLK = MCLK = TACLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.2/TA1|--> CCR1 - 75% PWM // // D. Dang // Texas Instruments, Inc

// December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x0C; P1SEL |= 0x0C; CCR0 = 512-1; CCTL1 = OUTMOD_7; CCR1 = 384; TACTL = TASSEL_2 + MC_1; _BIS_SR(CPUOFF); } //****************************************************************************** // MSP430G2xx3 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 *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.2/TA1|--> CCR1 - 75% PWM // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x0C; P1SEL |= 0x0C; CCR0 = 512-1; CCTL1 = OUTMOD_7; CCR1 = 384; TACTL = TASSEL_1 + MC_1; _BIS_SR(LPM3_bits); } //******************************************************************************

// // // // // // //

Stop WDT P1.2 and P1.3 output P1.2 and P1.3 TA1/2 options PWM Period CCR1 reset/set CCR1 PWM duty cycle SMCLK, up mode

// Enter LPM0

// // // // // // //

Stop WDT P1.2 and P1.3 output P1.2 and P1.3 TA1/2 options PWM Period CCR1 reset/set CCR1 PWM duty cycle ACLK, up mode

// Enter LPM3

// MSP430G2xx3 Demo - Timer_A, PWM TA1, Up/Down Mode, DCO SMCLK // // Description: This program generates one PWM output on P1.2 using // Timer_A configured for up/down mode. The value in CCR0, 128, defines the PWM // period/2 and the value in CCR1 the PWM duty cycles. // A 75% duty cycle is on P1.2. // SMCLK = MCLK = TACLK = default DCO // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | // --|RST XOUT|// | | // | P1.2/TA1|--> CCR1 - 75% PWM // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10 //****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x0C; P1SEL |= 0x0C; CCR0 = 128; CCTL1 = OUTMOD_6; CCR1 = 32; TACTL = TASSEL_2 + MC_3; _BIS_SR(LPM0_bits); } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, PWM TA1, Up/Down Mode, 32kHz ACLK // // Description: This program generates one PWM output on P1.2 using // Timer_A configured for up/down mode. The value in CCR0, 128, defines the // PWM period/2 and the value in CCR1 the PWM duty cycle. Using // 32kHz ACLK as TACLK, the timer period is 7.8ms with a 75% duty cycle on // P1.2. Normal operating mode is LPM3. // ACLK = TACLK = LFXT1 = 32768Hz, MCLK = default DCO // //* External watch crystal installed on XIN XOUT is required for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | P1.2/TA1|--> CCR1 - 75% PWM // // D. Dang // Texas Instruments, Inc // December 2010 // Built with CCS Version 4.2.0 and IAR Embedded Workbench Version: 5.10

// // // // // // //

Stop WDT P1.2 and P1.3 output P1.2 and P1.3 TA1/2 options PWM Period/2 CCR1 toggle/set CCR1 PWM duty cycle SMCLK, up-down mode

// Enter LPM0

//****************************************************************************** #include void main(void) { WDTCTL = WDTPW + WDTHOLD; P1DIR |= 0x0C; P1SEL |= 0x0C; CCR0 = 128; CCTL1 = OUTMOD_6; CCR1 = 32; TACTL = TASSEL_1 + MC_3; _BIS_SR(LPM3_bits); } //****************************************************************************** // MSP430G2xx3 Demo - Timer_A, Ultra-Low Pwr UART 2400 Echo, 32kHz ACLK // // Description: Use Timer_A CCR0 hardware output modes and SCCI data latch // to implement UART function @ 2400 baud. Software does not directly read and // write to RX and TX pins, instead proper use of output modes and SCCI data // latch are demonstrated. Use of these hardware features eliminates ISR // latency effects as hardware insures that output and input bit latching and // timing are perfectly synchronised with Timer_A regardless of other // software activity. In the Mainloop the UART function readies the UART to // receive one character and waits in LPM3 with all activity interrupt driven. // After a character has been received, the UART receive function forces exit // from LPM3 in the Mainloop which echo's back the received character. // ACLK = TACLK = LFXT1 = 32768Hz, MCLK = SMCLK = default DCO // //* An external watch crystal is required on XIN XOUT for ACLK *// // // MSP430G2xx3 // ----------------// /|\| XIN|// | | | 32kHz // --|RST XOUT|// | | // | CCI0B/TXD/P1.5|--------> // | | 2400 8N1 // | CCI0A/RXD/P1.1| 1; BitCnt --; } }

// TX Space // TX Mark

// RX else { if( CCTL0 & CAP ) // Capture mode = start bit edge { CCTL0 &= ~ CAP; // Switch from capture to compare mo de CCR0 += Bitime_5; } else { RXTXData = RXTXData >> 1; if (CCTL0 & SCCI) // Get bit waiting in receive latch RXTXData |= 0x80; BitCnt --; // All bits RXed? if ( BitCnt == 0) //>>>>>>>>>> Decode of Received Byte Here