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.

MSP432的串口问题



如何用MAP_UART_transmitData()函数发送一个字符?这个函数的第二个参数应该是什么?比如要发送一个字符‘m’,第二个参数应该是什么?


  • /! Transmits a byte from the UART Module.
    //!
    //! \param moduleInstance is the instance of the eUSCI A (UART) module.
    //! Valid parameters vary from part to part, but can include:
    //! - \b EUSCI_A0_MODULE
    //! - \b EUSCI_A1_MODULE
    //! - \b EUSCI_A2_MODULE
    //! - \b EUSCI_A3_MODULE
    //! <br> It is important to note that for eUSCI modules, only "A" modules such
    //! as EUSCI_A0 can be used. "B" modules such as EUSCI_B0 do not support the
    //! UART mode
    //! \param transmitData data to be transmitted from the UART module
    //!
    //! This function will place the supplied data into UART transmit data register
    //! to start transmission
    //!
    //! Modified register is \b UCAxTXBUF
    //! \return None.
    //
    //*****************************************************************************
    extern void UART_transmitData(uint32_t moduleInstance,
    uint_fast8_t transmitData);

    ------------------------------------------------------

    看该头文件解释:第一个是moduleInstance,可以选择的参数是:

    //! - \b EUSCI_A0_MODULE
    //! - \b EUSCI_A1_MODULE
    //! - \b EUSCI_A2_MODULE
    //! - \b EUSCI_A3_MODULE

    第二个参数是发送的数据

  • 所以第二个参数你就可以填写个‘m’,或者你前面定义个变量存放m,或者用指针都是可以的

  •      

    volatile uint8_t TXData = 1;

      MAP_UART_transmitData(EUSCI_A0_MODULE, TXData);

    ---------------------------------------------------------------------------------------

    /*
     * -------------------------------------------
     *    MSP432 DriverLib - v01_04_00_18 
     * -------------------------------------------
     *
     * --COPYRIGHT--,BSD,BSD
     * Copyright (c) 2015, 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.
     * --/COPYRIGHT--*/
    /******************************************************************************
     * MSP432 UART - Loopback with 48MHz DCO BRCLK
     *
     * Description: This demo connects TX to RX of the MSP432 UART
     * The example code shows proper initialization of registers
     * and interrupts to receive and transmit data. If data is incorrect P1.0 LED
     * is turned ON.
     *
     *  MCLK = HSMCLK = SMCLK = DCO of 48MHz
     *
     *               MSP432P401
     *             -----------------
     *            |                 |
     *       RST -|     P1.3/UCA0TXD|----|
     *            |                 |    |
     *           -|                 |    |
     *            |     P1.2/UCA0RXD|----|
     *            |                 |
     *            |             P1.0|---> LED
     *            |                 |
     *
     * Author: Timothy Logan
    *******************************************************************************/
    /* DriverLib Includes */
    #include "driverlib.h"
    
    /* Standard Includes */
    #include <stdint.h>
    
    #include <stdbool.h>
    
    volatile uint8_t TXData = 1;
    volatile uint8_t RXData = 0;
    
    /* UART Configuration Parameter. These are the configuration parameters to
     * make the eUSCI A UART module to operate with a 115200 baud rate. These
     * values were calculated using the online calculator that TI provides
     * at:
     * http://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP430BaudRateConverter/index.html
     */
    const eUSCI_UART_Config uartConfig =
    {
            EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
            26,                                     // BRDIV = 26
            0,                                       // UCxBRF = 0
            111,                                       // UCxBRS = 111
            EUSCI_A_UART_NO_PARITY,                  // No Parity
            EUSCI_A_UART_MSB_FIRST,                  // MSB First
            EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
            EUSCI_A_UART_MODE,                       // UART mode
            EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // Oversampling
    };
    
    int main(void)
    {
        /* Halting WDT  */
        MAP_WDT_A_holdTimer();
    
        /* Selecting P1.2 and P1.3 in UART mode and P1.0 as output (LED) */
        MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
                 GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);
        MAP_GPIO_setAsOutputPin(GPIO_PORT_P1, GPIO_PIN0);
        MAP_GPIO_setOutputLowOnPin(GPIO_PORT_P1, GPIO_PIN0);
    
        /* Setting DCO to 48MHz (upping Vcore) */
        MAP_PCM_setCoreVoltageLevel(PCM_VCORE1);
        CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_48);
    
        /* Configuring UART Module */
        MAP_UART_initModule(EUSCI_A0_MODULE, &uartConfig);
    
        /* Enable UART module */
        MAP_UART_enableModule(EUSCI_A0_MODULE);
    
        /* Enabling interrupts */
        MAP_UART_enableInterrupt(EUSCI_A0_MODULE, EUSCI_A_UART_RECEIVE_INTERRUPT);
        MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
        MAP_Interrupt_enableSleepOnIsrExit();
    
        while(1)
        {
            MAP_UART_transmitData(EUSCI_A0_MODULE, TXData);
    
            MAP_Interrupt_enableSleepOnIsrExit();
            MAP_PCM_gotoLPM0InterruptSafe();
        }
    }
    
    /* EUSCI A0 UART ISR - Echos data back to PC host */
    void euscia0_isr(void)
    {
        uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_MODULE);
    
        MAP_UART_clearInterruptFlag(EUSCI_A0_MODULE, status);
    
        if(status & EUSCI_A_UART_RECEIVE_INTERRUPT)
        {
            RXData = MAP_UART_receiveData(EUSCI_A0_MODULE);
    
            if(RXData != TXData)              // Check value
            {
                MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
                while(1);                       // Trap CPU
            }
            TXData++;
            MAP_Interrupt_disableSleepOnIsrExit();
        }
    
    }
    
  • 我一开始也是这么写的:  MAP_UART_transmitData(EUSCI_A0_MODULE, 'm');,可是在上位机上收到的字符完全不对,不知道是为什么,波特率是9600,用的是ti的12mhz的那个例程,稍微改动了一下,把接受中断里的发送函数改成了MAP_UART_transmitData(EUSCI_A0_MODULE, 'm');。难道是我的硬件有问题吗?是不是只要插上launchpad上那根usb线就好了?不需要其他的操作吧?

  • 如果能接收到数据,却内容不对,你连续发送10次m,试试看,如果能发送,且收到若干相同的字符或乱码,那么就是你时钟配置错误。

  • 我也是碰到这个问题,结果发现是串口字节倒序了。比如你发送的'm'二进制是0x01101101,而电脑收到的却是0x10110110。是不是固件库有BUG啊,不知道楼主解决这个问题了不

  • TI 给的例程中串口配置为MSB,即低位先发送,而正常情况下一般使用LSB,配置的时候改成LSB就好了

  • 哇,太感谢了。才发现的