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);
}
}