Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* --COPYRIGHT--,BSD_EX
- * Copyright (c) 2012, Texas Instruments Incorporated
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions
- * are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- *
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- *
- * * Neither the name of Texas Instruments Incorporated nor the names of
- * its contributors may be used to endorse or promote products derived
- * from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
- * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
- * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
- * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
- * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
- * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
- * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
- * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
- * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
- * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
- * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- *******************************************************************************
- *
- * MSP430 CODE EXAMPLE DISCLAIMER
- *
- * MSP430 code examples are self-contained low-level programs that typically
- * demonstrate a single peripheral function or device feature in a highly
- * concise manner. For this the code may rely on the device's power-on default
- * register values and settings such as the clock configuration and care must
- * be taken when combining code from several examples to avoid potential side
- * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
- * for an API functional library-approach to peripheral configuration.
- *
- * --/COPYRIGHT--*/
- //******************************************************************************
- // MSP-FET430P140 Demo - USART0, Ultra-Low Pwr UART 9600 Echo ISR, 32kHz ACLK
- //
- // Description: Echo a received character, USART0 RX ISR at high-speed used
- // with ultra-low power techniques. Normal operation in LPM3, Set_DCO
- // function needs to be called to configure DCO that is used for UART baud
- // generation. On valid RX character, character echoed back. Use start-bit
- // edge detect - URXSE - to automatically (re)enable DCO and trigger ISR. ISR
- // must make sure DCO clock source remains enabled for the UART to receive
- // full character.
- // Software needs to make sure a character has been completely TX'ed, or RX'ed
- // before entering LPM3, which disables DCO required for the USART baud rate
- // generator. In the example, TX'ing is checked using the TXEPT bit directly.
- // RX'ing is checked using the SSEL0 clock select bit as a flag. This is
- // possible because UCLK0 = SMCLK when either both SSEL1 and SSEL0 or just
- // SSEL1 = 1. In the example, when SSEL1 = SSEL0 = 1 there is no RX'ing, and
- // LPM3 is allowed. When SSEL 1 = 1 and SSEL0 = 0 SMCLK is selected, but
- // RX'ing is active and the DCO is required, thus LPM3 is not allowed.
- // ACLK = LFXT1/8 = 32768/8, MCLK = SMCLK = UCLK0 = DCOCLK ~ 2MHz
- // Baud rate divider with 2MHz = 2MHz/9600 ~ 208 (00D0h)
- // //* An external 32kHz watch crystal on XIN XOUT is required for ACLK *//
- //
- //
- // MSP430F149
- // -----------------
- // /|\| XIN|-
- // | | | 32768Hz
- // --|RST XOUT|-
- // | |
- // | P3.4|------------>
- // | | 9600 - 8N1
- // | P3.5|<------------
- //
- //
- // M. Buccini
- // Texas Instruments Inc.
- // Feb 2005
- // Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.21A
- //******************************************************************************
- #include <msp430.h>
- void Set_DCO (void);
- int main(void)
- {
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
- P3SEL |= 0x30; // P3.4,5 = USART0 TXD/RXD
- BCSCTL1 |= DIVA_3; // ACLK= LFXT1CLK/8
- Set_DCO(); // Set DCO
- ME2 |= UTXE0 + URXE0; // Enabled USART0 TXD/RXD
- UCTL0 |= CHAR; // 8-bit character, SWRST = 1
- UTCTL0 |= SSEL1 + SSEL0 + URXSE; // UCLK = SMCLK, start edge detect
- UBR00 = 0xD0; // 9600 from 2Mhz
- UBR10 = 0x00;
- UMCTL0 = 0x00; // No modulation
- UCTL0 &= ~SWRST; // Initialize USART state machine
- IE1 |= URXIE0; // Enable USART0 RX interrupt
- for (;;)
- {
- while (!(UTCTL0 & TXEPT)); // Confirm no TXing before --> LPM3
- __disable_interrupt(); // Disable interrupts for flag test
- __no_operation();
- if (!(UTCTL0 & SSEL0))
- __bis_SR_register(LPM0_bits + GIE); // RX'ing char, enter LPM0, int's active
- else
- __bis_SR_register(LPM3_bits + GIE); // Enter LPM3, int's active
- }
- }
- #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
- #pragma vector=USART0RX_VECTOR
- __interrupt void usart0_rx (void)
- #elif defined(__GNUC__)
- void __attribute__ ((interrupt(USART0RX_VECTOR))) usart0_rx (void)
- #else
- #error Compiler not supported!
- #endif
- {
- if ((IFG1 & URXIFG0)) // Test URXIFG0
- {
- while (!(IFG1 & UTXIFG0)); // USART0 TX buffer ready?
- TXBUF0 = RXBUF0; // RXBUF0 to TXBUF0
- __bic_SR_register_on_exit(LPM3_bits); // Exit LPM3 after reti
- UTCTL0 |= SSEL0; // SSEL0 = 1, no RX activity
- }
- else // Start edge
- {
- UTCTL0 &= ~URXSE; // Clear URXS signal
- UTCTL0 |= URXSE; // Re-enable edge detect
- __bic_SR_register_on_exit(SCG1 + SCG0); // DCO reamins on after reti
- UTCTL0 &= ~SSEL0; // SSEL0= 0, RX activity
- }
- }
- //------------------------------------------------------------------------------
- void Set_DCO (void) // Set DCO to selected frequency
- //------------------------------------------------------------------------------
- {
- #define DELTA 488 // Target DCO = DELTA*(4096) = 2MHz
- unsigned int Compare, Oldcapture = 0;
- CCTL2 = CM_1 + CCIS_1 + CAP; // CAP, ACLK
- TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear
- while (1)
- {
- while (!(CCIFG & CCTL2)); // Wait until capture occured
- CCTL2 &= ~CCIFG; // Capture occured, clear flag
- Compare = CCR2; // Get current captured SMCLK
- Compare = Compare - Oldcapture; // SMCLK difference
- Oldcapture = CCR2; // Save current captured SMCLK
- if (DELTA == Compare) break; // If equal, leave "while(1)"
- else if (DELTA < Compare)
- {
- DCOCTL--;
- if (DCOCTL == 0xFF) // DCO is too fast, slow it down
- {
- if (!(BCSCTL1 == (XT2OFF + DIVA_3)))
- BCSCTL1--; // Did DCO role under?, Sel lower RSEL
- }
- }
- else
- {
- DCOCTL++; // DCO is too slow, speed it down
- if (DCOCTL == 0x00)
- {
- if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))
- BCSCTL1++; // Did DCO role over? Sel higher RSEL
- }
- }
- }
- CCTL2 = 0; // Stop CCR2
- TACTL = 0; // Stop Timer_A
- }
Advertisement
Add Comment
Please, Sign In to add comment