This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

求助,如何在MSP430F6736中编写通过UART发送命令的程序?

Other Parts Discussed in Thread: MSP430F6736

Ti的工程师,您好

我想求助,如何在MSP430F6736中编写通过UART发送字符的程序?

我在resource explorer中找到了一个参考程序,MSP430F673X_USCI_UART_STANDARD_TRANSCEIVER.c ,但是并不能使用。

程序描述为:

设备将在LPM0 / LPM3中等待(基于时钟源),直到收到UART字符。然后设备将回显收到的字符。里面没有涉及发送,无法运行成功。

我想知道我该学习些什么,才能实现基于UART的数据传输。或者您有没有其他的例程方便提供一下么?感激不尽!

  • 您可以参考一下下面的代码

    /* --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--*/
    //******************************************************************************
    //   MSP430F673x Demo - USCI_A0, Ultra-Low Power UART 9600 Echo ISR, 32kHz ACLK
    //
    //   Description: Echo a received character, RX ISR used. Normal mode is LPM3,
    //   USCI_A0 RX interrupt triggers TX Echo.
    //   ACLK = 32768Hz crystal, MCLK = SMCLK = DCO ~1.045MHz
    //   Baud rate divider with 32768Hz XTAL @9600 = 32768Hz/9600 = 3.41
    //   See User Guide for baud rate divider table
    //
    //
    //                MSP430F673x
    //             -----------------
    //        /|\ |              XIN|-
    //         |  |                 | 32kHz
    //         ---|RST          XOUT|-
    //            |                 |
    //            |     P1.3/UCA0TXD|------------>
    //            |                 | 9600 - 8N1
    //            |     P1.2/UCA0RXD|<------------
    //
    //  M. Swanson
    //  Texas Instruments Inc.
    //  December 2011
    //  Built with CCS Version: 5.1.0 and IAR Embedded Workbench Version: 5.40.1
    //******************************************************************************
    #include <msp430.h>
    
    int main(void)
    {
        WDTCTL = WDTPW | WDTHOLD;               // Stop WDT
    
        // Setup P1.2 UCA0RXD, P1.3 UCA0TXD
        P1SEL |= BIT2 | BIT3;                   // Set P1.2, P1.3 to non-IO
        P1DIR |= BIT2 | BIT3;                   // Enable UCA0RXD, UCA0TXD
    
        // Setup LFXT1
        UCSCTL6 &= ~(XT1OFF);                   // XT1 On
        UCSCTL6 |= XCAP_3;                      // Internal load cap
        // Loop until XT1 fault flag is cleared
        do
        {
            UCSCTL7 &= ~(XT2OFFG | XT1LFOFFG | DCOFFG);
            // Clear XT2,XT1,DCO fault flags
            SFRIFG1 &= ~OFIFG;                  // Clear fault flags
        } while (SFRIFG1 & OFIFG);              // Test oscillator fault flag
    
        // Setup eUSCI_A0
        UCA0CTLW0 |= UCSWRST;                   // **Put state machine in reset**
        UCA0CTLW0 |= UCSSEL_1;                  // CLK = ACLK
        UCA0BRW_L = 0x03;                       // 32kHz/9600=3.41 (see User's Guide)
        UCA0BRW_H = 0x00;                       //
        UCA0MCTLW = 0x5300;                     // Modulation UCBRSx=0x53, UCBRFx=0
        UCA0CTLW0 &= ~UCSWRST;                  // **Initialize USCI state machine**
        UCA0IE |= UCRXIE;                       // Enable USCI_A0 RX interrupt
    
        __bis_SR_register(LPM3_bits | GIE);     // Enter LPM3, interrupts enabled
        __no_operation();                       // For debugger
    }
    
    // USCI_A0 interrupt service routine
    #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
    #pragma vector=USCI_A0_VECTOR
    __interrupt void USCI_A0_ISR(void)
    #elif defined(__GNUC__)
    void __attribute__ ((interrupt(USCI_A0_VECTOR))) USCI_A0_ISR (void)
    #else
    #error Compiler not supported!
    #endif
    {
        switch (__even_in_range(UCA0IV, 4))
        {
            case USCI_NONE: break;              // No interrupt
            case USCI_UART_UCRXIFG:             // RXIFG
                while (!(UCA0IFG & UCTXIFG)) ;  // USCI_A0 TX buffer ready?
                UCA0TXBUF = UCA0RXBUF;          // TX -> RXed character
                break;
            case USCI_UART_UCTXIFG: break;      // TXIFG
            case USCI_UART_UCSTTIFG: break;     // TTIFG
            case USCI_UART_UCTXCPTIFG: break;   // TXCPTIFG
            default: break;
        }
    }
    
    

  • 请问这个程序的执行实现了什么功能?因为我看主要分两个部分,上面的main函数以及下面的USCI_A0_ISR中断服务程序,都没有涉及到数据传输的代码,不太理解,您能说的详细一点吗?十分感谢!
  • 是不是我需要添加某些代码在示例特定位置,才能实现功能?如果是,具体怎么办能麻烦大致描述一下么?O(∩_∩)O谢谢
  • 这个程序就实现了一个功能,使用中断方式接收字符并发送出去。main函数里主要是初始化和进入低功耗模式。

  • 您好,这个“接收字符并发送出去”的具体位置在哪里,我没有找到?它的数据是从哪里接收的?我需要的是一个能把MSP430F6736中的数据通过uart方式发送出去的示例代码。
  • LaunchPad的USB口带有虚拟串口功能,可以通过USB接口虚拟串口,与电脑连接。要在电脑上读取串口数据,还需要一个串口调试软件,推荐使用PuTTY

    下图是我用G2553 launchpad来测试的,当在PuTTY键入数字时,运行程序会停在断点(while (!(IFG2&UCA0TXIFG));                // USCI_A0 TX buffer ready?)