Facebook
From Bulky Prairie Dog, 1 Year ago, written in C.
Embed
Download Paste or View Raw
Hits: 103
  1. /* --COPYRIGHT--,BSD_EX
  2.  * Copyright (c) 2016, 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. //  MSP430FR235x Demo -  eUSCI_A0 UART echo at 4800 baud using BRCLK = 32768Hz.
  47. //
  48. //  Description: This demo echoes back characters received via a PC serial port.
  49. //  ACLK is used as UART clock source and the device is put in LPM3.
  50. //  Note that level shifter hardware is needed to shift between RS232 and MSP
  51. //  voltage levels.
  52. //
  53. //  The example code shows proper initialization of registers
  54. //  and interrupts to receive and transmit data.
  55. //  To test code in LPM3, disconnect the debugger.
  56. //
  57. //  ACLK = REFO = 32768Hz, MCLK = SMCLK = DCODIV ~1MHz.
  58. //
  59. //                MSP430FR2355
  60. //             -----------------
  61. //         /|\|                 |
  62. //          | |                 |
  63. //          --|RST              |
  64. //            |                 |
  65. //            |                 |
  66. //            |     P4.3/UCA1TXD|----> PC (echo)
  67. //            |     P4.2/UCA1RXD|<---- PC
  68. //            |                 |
  69. //
  70. //   Darren Lu
  71. //   Texas Instruments Inc.
  72. //   Oct. 2016
  73. //   Built with IAR Embedded Workbench v6.50 & Code Composer Studio v6.2
  74. //******************************************************************************
  75.  
  76. #include <msp430.h>
  77.  
  78. void Init_GPIO();
  79.  
  80. int main(void)
  81. {
  82.   WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
  83.  
  84.   // Configure GPIO
  85.   Init_GPIO();
  86.  
  87.   PM5CTL0 &= ~LOCKLPM5;                     // Disable the GPIO power-on default high-impedance mode
  88.                                             // to activate 1previously configured port settings
  89.  
  90.   // Configure UART pins
  91.   P4SEL0 |=  BIT2 | BIT3;                    // set 2-UART pin as second function
  92.  
  93.   // Configure UART
  94.   UCA1CTLW0 |= UCSWRST;
  95.   UCA1CTLW0 |= UCSSEL_1;                    // set ACLK as BRCLK
  96.  
  97.   // Baud Rate calculation. Referred to UG 17.3.10
  98.   // (1) N=32768/4800=6.827
  99.   // (2) OS16=0, UCBRx=INT(N)=6
  100.   // (4) Fractional portion = 0.827. Refered to UG Table 17-4, UCBRSx=0xEE.
  101.   UCA1BR0 = 6;                              // INT(32768/4800)
  102.   UCA1BR1 = 0x00;
  103.   UCA1MCTLW = 0xEE00;
  104.  
  105.   UCA1CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
  106.   UCA1IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt
  107.  
  108.   __bis_SR_register(LPM3_bits|GIE);         // Enter LPM3, interrupts enabled
  109.   __no_operation();                         // For debugger
  110. }
  111.  
  112. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  113. #pragma vector=USCI_A1_VECTOR
  114. __interrupt void USCI_A1_ISR(void)
  115. #elif defined(__GNUC__)
  116. void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)
  117. #else
  118. #error Compiler not supported!
  119. #endif
  120. {
  121.   switch(__even_in_range(UCA1IV,USCI_UART_UCTXCPTIFG))
  122.   {
  123.     case USCI_NONE: break;
  124.     case USCI_UART_UCRXIFG:
  125.       while(!(UCA1IFG&UCTXIFG));
  126.       UCA1TXBUF = UCA1RXBUF;
  127.       __no_operation();
  128.       break;
  129.     case USCI_UART_UCTXIFG: break;
  130.     case USCI_UART_UCSTTIFG: break;
  131.     case USCI_UART_UCTXCPTIFG: break;
  132.     default: break;
  133.   }
  134. }
  135.  
  136. void Init_GPIO()
  137. {
  138.     P1DIR = 0xFF; P2DIR = 0xFF;
  139.     P1REN = 0xFF; P2REN = 0xFF;
  140.     P1OUT = 0x00; P2OUT = 0x00;
  141. }*/
  142. /* --COPYRIGHT--,BSD_EX
  143.  * Copyright (c) 2016, Texas Instruments Incorporated
  144.  * All rights reserved.
  145.  *
  146.  * Redistribution and use in source and binary forms, with or without
  147.  * modification, are permitted provided that the following conditions
  148.  * are met:
  149.  *
  150.  * *  Redistributions of source code must retain the above copyright
  151.  *    notice, this list of conditions and the following disclaimer.
  152.  *
  153.  * *  Redistributions in binary form must reproduce the above copyright
  154.  *    notice, this list of conditions and the following disclaimer in the
  155.  *    documentation and/or other materials provided with the distribution.
  156.  *
  157.  * *  Neither the name of Texas Instruments Incorporated nor the names of
  158.  *    its contributors may be used to endorse or promote products derived
  159.  *    from this software without specific prior written permission.
  160.  *
  161.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  162.  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
  163.  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  164.  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  165.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  166.  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  167.  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  168.  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  169.  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  170.  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  171.  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  172.  *
  173.  *******************************************************************************
  174.  *
  175.  *                       MSP430 CODE EXAMPLE DISCLAIMER
  176.  *
  177.  * MSP430 code examples are self-contained low-level programs that typically
  178.  * demonstrate a single peripheral function or device feature in a highly
  179.  * concise manner. For this the code may rely on the device's power-on default
  180.  * register values and settings such as the clock configuration and care must
  181.  * be taken when combining code from several examples to avoid potential side
  182.  * effects. Also see www.ti.com/grace for a GUI- and www.ti.com/msp430ware
  183.  * for an API functional library-approach to peripheral configuration.
  184.  *
  185.  * --/COPYRIGHT--*/
  186. //******************************************************************************
  187. //  MSP430FR235x Demo -  eUSCI_A0 UART echo at 4800 baud using BRCLK = 32768Hz.
  188. //
  189. //  Description: This demo echoes back characters received via a PC serial port.
  190. //  ACLK is used as UART clock source and the device is put in LPM3.
  191. //  Note that level shifter hardware is needed to shift between RS232 and MSP
  192. //  voltage levels.
  193. //
  194. //  The example code shows proper initialization of registers
  195. //  and interrupts to receive and transmit data.
  196. //  To test code in LPM3, disconnect the debugger.
  197. //
  198. //  ACLK = REFO = 32768Hz, MCLK = SMCLK = DCODIV ~1MHz.
  199. //
  200. //                MSP430FR2355
  201. //             -----------------
  202. //         /|\|                 |
  203. //          | |                 |
  204. //          --|RST              |
  205. //            |                 |
  206. //            |                 |
  207. //            |     P4.3/UCA1TXD|----> PC (echo)
  208. //            |     P4.2/UCA1RXD|<---- PC
  209. //            |                 |
  210. // Rata de transfer: 4800 bps
  211.  
  212. //******************************************************************************
  213.  
  214. #include <msp430.h>
  215.  
  216. void Init_GPIO();
  217. unsigned char data=120; //'x' x mic
  218. unsigned char sir[5]={'T','e','m','p',':',10};
  219. unsigned int i=0;
  220. int main(void)
  221. {
  222.   WDTCTL = WDTPW | WDTHOLD;                 // Stop watchdog timer
  223.  
  224.   // Configure GPIO
  225.   Init_GPIO();
  226.  // config P4.1
  227.   P4OUT |= BIT1;                          // Configure P1.3 as pulled-up
  228.   P4REN |= BIT1;                          // P1.3 pull-up register enable
  229.   P4IES |= BIT1;                          // P1.3 Hi/Low edge
  230.   P4IE |= BIT1;                           // P1.3 interrupt enabled
  231.  
  232.   // operation at 24MHz(beyond 8MHz) _before_ configuring the clock system.
  233.       FRCTL0 = FRCTLPW | NWAITS_2;
  234.  
  235.       __bis_SR_register(SCG0);                           // disable FLL
  236.       CSCTL3 |= SELREF__REFOCLK;                         // Set REFO as FLL reference source
  237.       CSCTL0 = 0;                                        // clear DCO and MOD registers
  238.       CSCTL1 |= DCORSEL_7;                               // Set DCO = 24MHz
  239.       CSCTL2 = FLLD_0 + 731;                             // DCOCLKDIV = 24MHz
  240.       __delay_cycles(3);
  241.       __bic_SR_register(SCG0);                           // enable FLL
  242.       while(CSCTL7 & (FLLUNLOCK0 | FLLUNLOCK1));         // FLL locked
  243.  
  244.       CSCTL4 = SELMS__DCOCLKDIV | SELA__REFOCLK;        // set default REFO(~32768Hz) as ACLK source, ACLK = 32768Hz
  245.                                                          // default DCOCLKDIV as MCLK and SMCLK source
  246.  
  247.       P1DIR |= BIT0 | BIT1 | BIT2;                       // set ACLK SMCLK and LED pin as output
  248.       P1SEL1 |= BIT0 | BIT1;                             // set ACLK and  SMCLK pin as second function
  249.  
  250.       PM5CTL0 &= ~LOCKLPM5;                      // Disable the GPIO power-on default high-impedance mode
  251.   // clear P4.1                                          // to activate 1previously configured port settings
  252.   P4IFG &= ~BIT1;
  253.   // Configure UART pins
  254.   P4SEL0 |= BIT2 | BIT3;                    // set 2-UART pin as second function
  255.  
  256.   // Configure UART
  257.   UCA1CTLW0 |= UCSWRST;
  258.   UCA1CTLW0 |= UCSSEL_2;                    // set SMCLK
  259.  
  260.   // Baud Rate calculation. Referred to UG 17.3.10
  261.   // (1) N=24000000/115200=208.33
  262.   // (3) OS16=1, UCBRx = 13;
  263.   //(4)UCBRFx=0
  264.    //UCBRSx=0x49
  265.   //Registri
  266.   UCA1BR0 = 13;                              // INT(32768/4800)
  267.   UCA1BR1 = 0x00;  // UCA1BRW = 13 = 0x000D;
  268.   UCA1MCTLW = 0x4901;
  269.  
  270.  
  271.   UCA1CTLW0 &= ~UCSWRST;                    // Initialize eUSCI
  272.   UCA1IE |= UCRXIE;                         // Enable USCI_A0 RX interrupt
  273.  
  274.   __bis_SR_register(LPM3_bits|GIE);         // Enter LPM3, interrupts enabled
  275.   __no_operation();                         // For debugger
  276. }
  277.  
  278. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  279. #pragma vector=USCI_A1_VECTOR
  280. __interrupt void USCI_A1_ISR(void)
  281. #elif defined(__GNUC__)
  282. void __attribute__ ((interrupt(USCI_A1_VECTOR))) USCI_A1_ISR (void)
  283. #else
  284. #error Compiler not supported!
  285. #endif
  286. {
  287.   switch(__even_in_range(UCA1IV,USCI_UART_UCTXCPTIFG))
  288.   {
  289.     case USCI_NONE: break;
  290.     case USCI_UART_UCRXIFG:
  291.       while(!(UCA1IFG&UCTXIFG));
  292.       data=UCA1RXBUF;
  293.       UCA1TXBUF = UCA1RXBUF;
  294.       __no_operation();
  295.       break;
  296.     case USCI_UART_UCTXIFG: break;
  297.     case USCI_UART_UCSTTIFG: break;
  298.     case USCI_UART_UCTXCPTIFG: break;
  299.     default: break;
  300.   }
  301. }
  302.  
  303. // Port 4 interrupt service routine
  304. #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
  305. #pragma vector=PORT4_VECTOR
  306. __interrupt void Port_4(void)
  307. #elif defined(__GNUC__)
  308. void __attribute__ ((interrupt(PORT4_VECTOR))) Port_4 (void)
  309. #else
  310. #error Compiler not supported!
  311. #endif
  312. {
  313.     P4IFG &= ~BIT1;                         // Clear P1.3 IFG
  314. /*    while(!(UCA1IFG&UCTXIFG));// este o transmisie activa?
  315.     UCA1TXBUF = data;// incarca 'x' in registrul de transmisie
  316.  */
  317.     //for(i=0;i<7;i++)
  318.    // {
  319.         while(!(UCA1IFG&UCTXIFG));// este o transmisie activa?
  320.         UCA1TXBUF = sir[i];// data; incarca 'x' in registrul de transmisie
  321.    // }
  322.    // i=0;
  323.     __bic_SR_register_on_exit(LPM3_bits);   // Exit LPM3
  324. }
  325.  
  326. void Init_GPIO()
  327. {
  328.     P1DIR = 0xFF; P2DIR = 0xFF;
  329.     P1REN = 0xFF; P2REN = 0xFF;
  330.     P1OUT = 0x00; P2OUT = 0x00;
  331. }
  332.