Part Number:MSP432E401Y
#include <stdint.h> #include <stdbool.h> #include "driverlib.h" #include "uartstdio.h" #ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endif int g_ui32SysClock; unsigned char a; void UART6_IRQHandler(void) {uint32_t ui32Status; // // Get the interrrupt status. //ui32Status = MAP_UARTIntStatus(UART6_BASE, true); // // Clear the asserted interrupts. //MAP_UARTIntClear(UART6_BASE, ui32Status); // // Loop while there are characters in the receive FIFO. //while(MAP_UARTCharsAvail(UART6_BASE)) { // // Read the next character from the UART and write it back to the UART. //MAP_UARTCharPutNonBlocking(UART6_BASE,MAP_UARTCharGetNonBlocking(UART6_BASE)); // // Blink the LED to show a character transfer is occuring. //GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, GPIO_PIN_0); // // Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks. //SysCtlDelay(g_ui32SysClock / (1000 * 3)); // // Turn off the LED //GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0); } } void UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count) { // // Loop while there are more characters to send. // while(ui32Count--) { // // Write the next character to the UART. // MAP_UARTCharPutNonBlocking(UART6_BASE, *pui8Buffer++); } } void InitConsole(void) {/* Enable the clock to GPIO port A and UART 0 */MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);/* Configure the GPIO Port A for UART 0 */MAP_GPIOPinConfigure(GPIO_PA0_U0RX);MAP_GPIOPinConfigure(GPIO_PA1_U0TX);MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);/* Configure the UART for 115200 bps 8-N-1 format with internal 16 MHz* oscillator as the UART clock source */MAP_UARTClockSourceSet(UART0_BASE, UART_CLOCK_ALTCLK);UARTStdioConfig(0, 115200, 16000000); } int main(void) { // // Set the clocking to run directly from the crystal at 120MHz. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000); InitConsole(); // // Enable the GPIO port that is used for the on-board LED. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION); // // Enable the GPIO pins for the LED (PN0). // MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0); // // Enable the peripherals used by this example. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART6); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP); // // Enable processor interrupts. // MAP_IntMasterEnable(); // // Set GPIO A0 and A1 as UART pins. // GPIOPinConfigure(GPIO_PP0_U6RX); GPIOPinConfigure(GPIO_PP1_U6TX); MAP_GPIOPinTypeUART(GPIO_PORTP_BASE, GPIO_PIN_0 | GPIO_PIN_1); // // Configure the UART for 115,200, 8-N-1 operation. // MAP_UARTConfigSetExpClk(UART6_BASE, g_ui32SysClock, 115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE)); // // Enable the UART interrupt. // MAP_IntEnable(INT_UART6); MAP_UARTIntEnable(UART6_BASE, UART_INT_RX | UART_INT_RT); // // Prompt for text to be entered. // // // Loop forever echoing data through the UART. // while(1) { // a=UARTFR; UARTprintf("%d \n",1); } }
Susan Yang:
“串口通信怎么得到读取的数据”
您是想获取串口读取的数据?
Sending and receiving data via the UART is handled by the UARTCharGet(), UARTCharGetNonBlocking(), UARTCharPut(), UARTCharPutNonBlocking(), UARTBreakCtl(), UARTCharsAvail(), and UARTSpaceAvail() functions.
在此例程中使用的是
UARTCharGetNonBlocking()
http://software-dl.ti.com/simplelink/esd/simplelink_msp432e4_sdk/4.10.00.13/docs/driverlib/msp432e4/api_guide/html/group__uart__api.html#gaad338221fc820412b005653855cedaf7
,
Zhengyuan Fang Fang:
我可以读到数了,我现在是使用a=UARTCharGetNonBlocking(UART6_BASE);这个函数,但是打印出来的a一直是255,无论我发送的是什么都是这个数,这是怎么回事呢,求教
,
Susan Yang:
请问代码是怎样的?a是如何定义的呢?
,
Zhengyuan Fang Fang:
#include <stdint.h> #include <stdbool.h> #include "driverlib.h" #include "uartstdio.h"#ifdef DEBUG void __error__(char *pcFilename, uint32_t ui32Line) { } #endifint g_ui32SysClock; unsigned char a; void UART6_IRQHandler(void) {uint32_t ui32Status;// // Get the interrrupt status. //ui32Status = MAP_UARTIntStatus(UART6_BASE, true);// // Clear the asserted interrupts. //MAP_UARTIntClear(UART6_BASE, ui32Status);// // Loop while there are characters in the receive FIFO. //while(MAP_UARTCharsAvail(UART6_BASE)){ // // Read the next character from the UART and write it back to the UART. //MAP_UARTCharPutNonBlocking(UART6_BASE,MAP_UARTCharGetNonBlocking(UART6_BASE));// // Blink the LED to show a character transfer is occuring. //GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, GPIO_PIN_0);// // Delay for 1 millisecond. Each SysCtlDelay is about 3 clocks. //SysCtlDelay(g_ui32SysClock / (1000 * 3));// // Turn off the LED //GPIOPinWrite(GPIO_PORTN_BASE, GPIO_PIN_0, 0);} }void UARTSend(const uint8_t *pui8Buffer, uint32_t ui32Count) { // // Loop while there are more characters to send. // while(ui32Count--) { // // Write the next character to the UART. // MAP_UARTCharPutNonBlocking(UART6_BASE, *pui8Buffer++); } }void InitConsole(void) {/* Enable the clock to GPIO port A and UART 0 */MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);/* Configure the GPIO Port A for UART 0 */MAP_GPIOPinConfigure(GPIO_PA0_U0RX);MAP_GPIOPinConfigure(GPIO_PA1_U0TX);MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);/* Configure the UART for 115200 bps 8-N-1 format with internal 16 MHz* oscillator as the UART clock source */MAP_UARTClockSourceSet(UART0_BASE, UART_CLOCK_ALTCLK);UARTStdioConfig(0, 115200, 16000000); }int main(void) { // // Set the clocking to run directly from the crystal at 120MHz. // g_ui32SysClock = MAP_SysCtlClockFreqSet((SYSCTL_XTAL_25MHZ | SYSCTL_OSC_MAIN | SYSCTL_USE_PLL | SYSCTL_CFG_VCO_480), 120000000);InitConsole();// // Enable the GPIO port that is used for the on-board LED. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPION);// // Enable the GPIO pins for the LED (PN0). // MAP_GPIOPinTypeGPIOOutput(GPIO_PORTN_BASE, GPIO_PIN_0);// // Enable the peripherals used by this example. // MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART6); MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOP);// // Enable processor interrupts. // MAP_IntMasterEnable();// // Set GPIO A0 and A1 as UART pins. // GPIOPinConfigure(GPIO_PP0_U6RX); GPIOPinConfigure(GPIO_PP1_U6TX); MAP_GPIOPinTypeUART(GPIO_PORTP_BASE, GPIO_PIN_0 | GPIO_PIN_1);// // Configure the UART for 115,200, 8-N-1 operation. // MAP_UARTConfigSetExpClk(UART6_BASE, g_ui32SysClock, 115200,(UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |UART_CONFIG_PAR_NONE));// // Enable the UART interrupt. // MAP_IntEnable(INT_UART6); MAP_UARTIntEnable(UART6_BASE, UART_INT_RX | UART_INT_RT);// // Prompt for text to be entered. // while(!UARTCharsAvail(UART6_BASE)) { }while(1) { // a=UARTFR;a=UARTCharGetNonBlocking(UART6_BASE);UARTprintf("%d \n",a); } }代码是这样的,a我定义的是unsigned char
,
Susan Yang:
Zhengyuan Fang Fang 说:a我定义的是unsigned char
您需要使用数组的方式,如
char rxMsgData[NUM_RX_CHARS]
,
Zhengyuan Fang Fang:
您好,我试了下还是不太会,能否发一个例程研究一下
,
Susan Yang:
因为没有MSP432E系列的板子,我是用的MSP432P401的开发板测试的
C:\ti\simplelink_msp432p4_sdk_3_40_01_02\examples\nortos\MSP_EXP432P401R\driverlib\uart_pc_echo_12mhz_brclk
可以在var内读到键盘输入的字符
代码如下
/* EUSCI A0 UART ISR - Echoes data back to PC host */ void EUSCIA0_IRQHandler(void) {uint32_t status = MAP_UART_getEnabledInterruptStatus(EUSCI_A0_BASE);uint8_t val=0;if(status & EUSCI_A_UART_RECEIVE_INTERRUPT_FLAG){// MAP_UART_transmitData(EUSCI_A0_BASE, MAP_UART_receiveData(EUSCI_A0_BASE));val = UART_receiveData(EUSCI_A0_BASE);}}
,
Susan Yang:
关于该函数,您可以看一下
25.3.2.12 uint8_t UART_receiveData ( uint32_t moduleInstance )
https://software-dl.ti.com/msp430/msp430_public_sw/mcu/msp430/MSP432_Driver_Library/latest/exports/driverlib/msp432_driverlib_3_21_00_05/doc/MSP432P4xx/MSP432_DriverLib_Users_Guide-MSP432P4xx-3_21_00_05.pdf