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.

MSP432P401R: 怎么通过UART从PC端发送命令,让MSP432执行对应命令

Part Number: MSP432P401R

您好!我是初学者,想了解UART怎么传输从PC输入的命令(字符串)。实际上,就是目前我写了一段代码,MOVE_X(float Dx)表示x方向移动Dx mm,放在mian()里面是可以运行的。但是,现在我需要的是mian()里面,不添加这条命令,想通过UART串口来实现命令的传输。软件是Code Composer Stuido, 硬件是MSP432。接下来是两段代码。代码一为实例代码,代码二为本人编写的,用来尝试从PC发送命令。但是自己写的代码不能到达这个目的,希望得到大家的指导,谢谢!

代码一(示例):

*               MSP432P401
 *             -----------------
 *            |                 |
 *            |                 |
 *            |                 |
 *       RST -|     P1.3/UCA0TXD|----> PC (echo)
 *            |                 |
 *            |                 |
 *            |     P1.2/UCA0RXD|<---- PC
 *            |                 |
 *
 *******************************************************************************/
/* DriverLib Includes */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Standard Includes */
#include <stdint.h>
#include <stdbool.h>

//![Simple UART Config]
/* UART Configuration Parameter. These are the configuration parameters to
 * make the eUSCI A UART module to operate with a 9600 baud rate. These
 * values were calculated using the online calculator that TI provides
 * at:
 *software-dl.ti.com/.../index.html
 */
const eUSCI_UART_ConfigV1 uartConfig =
{
        EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
        78,                                     // BRDIV = 78
        2,                                       // UCxBRF = 2
        0,                                       // UCxBRS = 0
        EUSCI_A_UART_NO_PARITY,                  // No Parity
        EUSCI_A_UART_LSB_FIRST,                  // LSB First
        EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
        EUSCI_A_UART_MODE,                       // UART mode
        EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION,  // Oversampling
        EUSCI_A_UART_8_BIT_LEN                  // 8 bit data length
};
//![Simple UART Config]

int main(void)
{
    /* Halting WDT  */
    MAP_WDT_A_holdTimer();

    /* Selecting P1.2 and P1.3 in UART mode */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);

    /* Setting DCO to 12MHz */
    CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);

    //![Simple UART Example]
    /* Configuring UART Module */
    MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);

    /* Enable UART module */
    MAP_UART_enableModule(EUSCI_A0_BASE);

    /* Enabling interrupts */
    MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
    MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster();   
    //![Simple UART Example]

    while(1)
    {
        MAP_PCM_gotoLPM0();
    }
}

/* EUSCI A0 UART ISR - Echoes data back to PC host */
void EUSCIA0_IRQHandler(void)
{
    uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);

    if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
    {
        MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
    }

}

代码二:
 *
 *               MSP432P401
 *             -----------------
 *            |                 |
 *            |                 |
 *            |                 |
 *       RST -|     P1.3/UCA0TXD|----> PC (echo)
 *            |                 |
 *            |                 |
 *            |     P1.2/UCA0RXD|<---- PC
 *            |                 |
 *
 *******************************************************************************/
/* DriverLib Includes */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>

/* Standard Includes */
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#include <stdio.h>

//![Simple UART Config]
/* UART Configuration Parameter. These are the configuration parameters to
 * make the eUSCI A UART module to operate with a 9600 baud rate. These
 * values were calculated using the online calculator that TI provides
 * at:
 *software-dl.ti.com/.../index.html
 */
uint64_t i,j,Nx,Ny,Nz,N;
float   Dx,Dy,Dz,theta,x1,x2,y1,y2,R;
void MOVE_X(float Dx);
const eUSCI_UART_ConfigV1 uartConfig =
{
        EUSCI_A_UART_CLOCKSOURCE_SMCLK,          // SMCLK Clock Source
        78,                                     // BRDIV = 78
        2,                                       // UCxBRF = 2
        0,                                       // UCxBRS = 0
        EUSCI_A_UART_NO_PARITY,                  // No Parity
        EUSCI_A_UART_LSB_FIRST,                  // LSB First
        EUSCI_A_UART_ONE_STOP_BIT,               // One stop bit
        EUSCI_A_UART_MODE,                       // UART mode
        EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION,  // Oversampling
        EUSCI_A_UART_8_BIT_LEN                  // 8 bit data length
};
//![Simple UART Config]

unsigned char* cmd ;

int main(void)
{
    /* Halting WDT  */
    MAP_WDT_A_holdTimer();

    /* Selecting P1.2 and P1.3 in UART mode */
    MAP_GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P1,
            GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION);

    /* Setting DCO to 12MHz */
    CS_setDCOCenteredFrequency(CS_DCO_FREQUENCY_12);

    //![Simple UART Example]
    /* Configuring UART Module */
    MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);

    /* Enable UART module */
    MAP_UART_enableModule(EUSCI_A0_BASE);

    /* Enabling interrupts */
    MAP_UART_enableInterrupt(EUSCI_A0_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT);
    MAP_Interrupt_enableInterrupt(INT_EUSCIA0);
    Interrupt_disableSleepOnIsrExit();
    MAP_Interrupt_enableSleepOnIsrExit();
    MAP_Interrupt_enableMaster();   
    //![Simple UART Example]

    while(1)
    {
        MAP_PCM_gotoLPM0();
        if (cmd == 'MX10')
        {
            MOVE_X(10);
        }
    }
}


/* EUSCI A0 UART ISR - Echoes data back to PC host */
void EUSCIA0_IRQHandler(void)
{
    uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);

    if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)
    {
        cmd = MAP_UART_receiveData(EUSCI_A0_BASE);
        MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));
    }

}


void MOVE_X(float Dx)
{
    if (Dx >= 0)
        P4->OUT |= BIT5;
    else
        {
            P4->OUT &= ~BIT5;
            Dx = -Dx;
        }
    Nx = 800*Dx;       //en mode complet (1/16 step)

    for (i=0; i<=Nx;i++)
    {
        P4->OUT ^= BIT4;
        __delay_cycles(375);
        P4->OUT ^= BIT4;
        __delay_cycles(375);
    }

}

  • 想了解UART怎么传输从PC输入的命令(字符串)

    不知是否理解了您的需求。您可以尝试以下操作:

    1 PC端使用串口调试助手来发送命令

    2 使用MSP432的串口来接收串口字符

    3 MSP432接收到字符后,进入串口中断

    4 MSP432串口中断内写入您需要进行的操作

  • 具体点是这样的。比如说,我想让机器x方向移动10mm,然后我也定义了这个子程序MOVE_X(float Dx)。现在就是,怎么通过UART串口输入命令,比如我在串口中输入MX10,MSP432能识别这个命令,执行MOVE_X(10),让机器x方向移动10mm。

    那么具体的问题就是,如何通过MAP_UART_receiveData(EUSCI_A0_BASE)这个接受的数据,来实现上述功能。

  • 您可以参考一下

    simplelink_msp432p4_sdk\examples\nortos\MSP_EXP432P401R\driverlib\uart_loopback_24mhz_brclk 

    查看代码:

    1  使用

      MAP_UART_enableInterrupt(EUSCI_A2_BASE, EUSCI_A_UART_RECEIVE_INTERRUPT); 

    来使能接收中断

    2 在中断服务程序内

     if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)

    当获取到接收中断(即 EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG)时,会执行

    {
    RXData = MAP_UART_receiveData(EUSCI_A2_BASE); //在此获取接收数据,如MX10

    if(RXData != TXData) // Check value 这部分您可以修改为您自己需要处理的任务,如执行MOVE_X(10),让机器x方向移动10mm
    {
    MAP_GPIO_setOutputHighOnPin(GPIO_PORT_P1, GPIO_PIN0);
    while(1); // Trap CPU
    }
    TXData++;
    MAP_Interrupt_disableSleepOnIsrExit();
    }