Guest User

Untitled

a guest
Feb 18th, 2015
513
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.15 KB | None | 0 0
  1. /* --COPYRIGHT--,BSD_EX
  2.  * Copyright (c) 2012, Texas Instruments Incorporated
  3.  * All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  *
  9.  * *  Redistributions of source code must retain the above copyright
  10.  *    notice, this list of conditions and the following disclaimer.
  11.  *
  12.  * *  Redistributions in binary form must reproduce the above copyright
  13.  *    notice, this list of conditions and the following disclaimer in the
  14.  *    documentation and/or other materials provided with the distribution.
  15.  *
  16.  * *  Neither the name of Texas Instruments Incorporated nor the names of
  17.  *    its contributors may be used to endorse or promote products derived
  18.  *    from this software without specific prior written permission.
  19.  *
  20.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  21.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  22.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  23.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  24.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  25.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  26.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  27.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  28.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31.  *
  32.  *******************************************************************************
  33.  *
  34.  *                       MSP430 CODE EXAMPLE DISCLAIMER
  35.  *
  36.  * MSP430 code examples are self-contained low-level programs that typically
  37.  * demonstrate a single peripheral function or device feature in a highly
  38.  * concise manner. For this the code may rely on the device's power-on default
  39.  * register values and settings such as the clock configuration and care must
  40.  * be taken when combining code from several examples to avoid potential side
  41.  * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
  42.  * for an API functional library-approach to peripheral configuration.
  43.  *
  44.  * --/COPYRIGHT--*/
  45. //******************************************************************************
  46. //   MSP-FET430P140 Demo - USART0, Ultra-Low Pwr UART 9600 Echo ISR, 32kHz ACLK
  47. //
  48. //  Description: Echo a received character, USART0 RX ISR at high-speed used
  49. //  with ultra-low power techniques. Normal operation in LPM3, Set_DCO
  50. //  function needs to be called to configure DCO that is used for UART baud
  51. //  generation. On valid RX character, character echoed back. Use start-bit
  52. //  edge detect - URXSE - to automatically (re)enable DCO and trigger ISR. ISR
  53. //  must make sure DCO clock source remains enabled for the UART to receive
  54. //  full character.
  55. //  Software needs to make sure a character has been completely TX'ed, or RX'ed
  56. //  before entering LPM3, which disables DCO required for the USART baud rate
  57. //  generator. In the example, TX'ing is checked using the TXEPT bit directly.
  58. //  RX'ing is checked using the SSEL0 clock select bit as a flag. This is
  59. //  possible because UCLK0 = SMCLK when either both SSEL1 and SSEL0 or just
  60. //  SSEL1 = 1. In the example, when SSEL1 = SSEL0 = 1 there is no RX'ing, and
  61. //  LPM3 is allowed. When SSEL 1 = 1 and  SSEL0 = 0 SMCLK is selected, but
  62. //  RX'ing is active and the DCO is required, thus LPM3 is not allowed.
  63. //  ACLK = LFXT1/8 = 32768/8,  MCLK = SMCLK = UCLK0 = DCOCLK ~ 2MHz
  64. //  Baud rate divider with 2MHz = 2MHz/9600 ~ 208 (00D0h)
  65. //  //* An external 32kHz watch crystal on XIN XOUT is required for ACLK *//   
  66. //
  67. //
  68. //                MSP430F149
  69. //             -----------------
  70. //         /|\|              XIN|-
  71. //          | |                 | 32768Hz
  72. //          --|RST          XOUT|-
  73. //            |                 |
  74. //            |             P3.4|------------>
  75. //            |                 | 9600 - 8N1
  76. //            |             P3.5|<------------
  77. //
  78. //
  79. //  M. Buccini
  80. //  Texas Instruments Inc.
  81. //  Feb 2005
  82. //  Built with CCE Version: 3.2.0 and IAR Embedded Workbench Version: 3.21A
  83. //******************************************************************************
  84.  
  85. #include <msp430.h>
  86.  
  87. void Set_DCO (void);
  88.  
  89. int main(void)
  90. {
  91.   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  92.   P3SEL |= 0x30;                            // P3.4,5 = USART0 TXD/RXD
  93.   BCSCTL1 |= DIVA_3;                        // ACLK= LFXT1CLK/8
  94.   Set_DCO();                                // Set DCO
  95.  
  96.   ME2 |= UTXE0 + URXE0;                     // Enabled USART0 TXD/RXD
  97.   UCTL0 |= CHAR;                            // 8-bit character, SWRST = 1
  98.   UTCTL0 |= SSEL1 + SSEL0 + URXSE;          // UCLK = SMCLK, start edge detect
  99.   UBR00 = 0xD0;                             // 9600 from 2Mhz
  100.   UBR10 = 0x00;
  101.   UMCTL0 = 0x00;                            // No modulation
  102.   UCTL0 &= ~SWRST;                          // Initialize USART state machine
  103.   IE1 |= URXIE0;                            // Enable USART0 RX interrupt
  104.  
  105.   for (;;)
  106.   {
  107.     while (!(UTCTL0 & TXEPT));              // Confirm no TXing before --> LPM3
  108.     __disable_interrupt();                  // Disable interrupts for flag test
  109.     __no_operation();
  110.     if (!(UTCTL0 & SSEL0))
  111.        __bis_SR_register(LPM0_bits + GIE);  // RX'ing char, enter LPM0, int's active
  112.     else
  113.     __bis_SR_register(LPM3_bits + GIE);     // Enter LPM3, int's active
  114.   }
  115. }
  116.  
  117. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  118. #pragma vector=USART0RX_VECTOR
  119. __interrupt void usart0_rx (void)
  120. #elif defined(__GNUC__)
  121. void __attribute__ ((interrupt(USART0RX_VECTOR))) usart0_rx (void)
  122. #else
  123. #error Compiler not supported!
  124. #endif
  125. {
  126.   if ((IFG1 & URXIFG0))                     // Test URXIFG0
  127.   {
  128.     while (!(IFG1 & UTXIFG0));              // USART0 TX buffer ready?
  129.     TXBUF0 = RXBUF0;                        // RXBUF0 to TXBUF0
  130.     __bic_SR_register_on_exit(LPM3_bits);   // Exit LPM3 after reti
  131.     UTCTL0 |= SSEL0;                        // SSEL0 = 1, no RX activity
  132.   }
  133.   else                                      // Start edge
  134.   {
  135.     UTCTL0 &= ~URXSE;                       // Clear URXS signal
  136.     UTCTL0 |= URXSE;                        // Re-enable edge detect
  137.     __bic_SR_register_on_exit(SCG1 + SCG0); // DCO reamins on after reti
  138.     UTCTL0 &= ~SSEL0;                       // SSEL0= 0, RX activity
  139.   }
  140. }
  141.  
  142. //------------------------------------------------------------------------------
  143. void Set_DCO (void)                         // Set DCO to selected frequency
  144. //------------------------------------------------------------------------------
  145. {
  146. #define DELTA 488                           // Target DCO = DELTA*(4096) = 2MHz
  147.  
  148.   unsigned int Compare, Oldcapture = 0;
  149.  
  150.   CCTL2 = CM_1 + CCIS_1 + CAP;              // CAP, ACLK
  151.   TACTL = TASSEL_2 + MC_2 + TACLR;          // SMCLK, cont-mode, clear
  152.  
  153.   while (1)
  154.   {
  155.     while (!(CCIFG & CCTL2));               // Wait until capture occured
  156.     CCTL2 &= ~CCIFG;                        // Capture occured, clear flag
  157.     Compare = CCR2;                         // Get current captured SMCLK
  158.     Compare = Compare - Oldcapture;         // SMCLK difference
  159.     Oldcapture = CCR2;                      // Save current captured SMCLK
  160.     if (DELTA == Compare) break;            // If equal, leave "while(1)"
  161.  
  162.     else if (DELTA < Compare)
  163.     {
  164.       DCOCTL--;
  165.       if (DCOCTL == 0xFF)                   // DCO is too fast, slow it down
  166.       {
  167.         if (!(BCSCTL1 == (XT2OFF + DIVA_3)))
  168.         BCSCTL1--;                          // Did DCO role under?, Sel lower RSEL
  169.       }
  170.     }
  171.     else
  172.     {
  173.       DCOCTL++;                             // DCO is too slow, speed it down
  174.       if (DCOCTL == 0x00)
  175.       {
  176.         if (!(BCSCTL1 == (XT2OFF + DIVA_3 + 0x07)))
  177.         BCSCTL1++;                          // Did DCO role over? Sel higher RSEL
  178.       }
  179.     }
  180.   }
  181.  
  182.   CCTL2 = 0;                                // Stop CCR2
  183.   TACTL = 0;                                // Stop Timer_A
  184. }
Advertisement
Add Comment
Please, Sign In to add comment