sdk讲的非常清楚了,要注意的是display可以选用不同的类型进行输出,暂时先使用串口吧,还可以用lcd,Host,debug等输出类型,这个以后再慢慢探讨
UART API接口定义表
#include
#include
UARTMSP432_Object uartMSP432Objects[MSP_EXP432P401R_UARTCOUNT]; //这个是外设的数据结构体,不能在应用程序中使用
unsigned char uartMSP432RingBuffer[MSP_EXP432P401R_UARTCOUNT][32];
//波特率发生器
const UARTMSP432_BaudrateConfig uartMSP432Baudrates[] = {
/* {baudrate, input clock, prescalar, UCBRFx, UCBRSx, oversampling} */
{
.outputBaudrate = 115200,
.inputClockFreq = 24000000, //输入时钟
.prescalar = 13, //预分频器
.hwRegUCBRFx = 0,
.hwRegUCBRSx = 37,
.oversampling = 1 //过采样模式
},
{115200, 12000000, 6, 8, 32, 1},
{115200, 10000000, 5, 7, 0, 1},
{115200, 6000000, 3, 4, 2, 1},
{115200, 3000000, 1, 10, 0, 1},
{9600, 24000000, 156, 4, 0, 1},
{9600, 12000000, 78, 2, 0, 1},
{9600, 10000000, 65, 2, 0, 1},
{9600, 6000000, 39, 1, 0, 1},
{9600, 3000000, 19, 8, 85, 1},
{9600, 32768, 3, 0, 146, 0}
};
/*
外设硬件属性组
基地址可选,有四个串口
EUSCI_A0_BASE
EUSCI_A1_BASE
EUSCI_A2_BASE
EUSCI_A3_BASE
对应有四个中断号
INT_EUSCIA0
INT_EUSCIA1
INT_EUSCIA2
INT_EUSCIA3
时钟源有两个可选,暂时不知道这两个的区别,需要看msp432时钟部分说明
EUSCI_A_UART_CLOCKSOURCE_SMCLK
EUSCI_A_UART_CLOCKSOURCE_ACLK
串口的rx tx引脚是很灵活的,可以重定义到其他引脚上,具体见UARTMSP432.h文件
*/
const UARTMSP432_HWAttrsV1 uartMSP432HWAttrs[MSP_EXP432P401R_UARTCOUNT] = {
{
.baseAddr = EUSCI_A0_BASE, //EUSCI_A0 寄存器基地址
.intNum = INT_EUSCIA0, //中断号
.intPriority = (~0), //中断优先级
.clockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK, //时钟源
.bitOrder = EUSCI_A_UART_LSB_FIRST, //顺序是LSB,低位先行
.numBaudrateEntries = sizeof(uartMSP432Baudrates)
sizeof(UARTMSP432_BaudrateConfig),
.baudrateLUT = uartMSP432Baudrates,
.ringBufPtr = uartMSP432RingBuffer[MSP_EXP432P401R_UARTA0], //缓冲区指针
.ringBufSize = sizeof(uartMSP432RingBuffer[MSP_EXP432P401R_UARTA0]),
.rxPin = UARTMSP432_P1_2_UCA0RXD, //rx引脚
.txPin = UARTMSP432_P1_3_UCA0TXD, //tx引脚
.errorFxn = NULL
},
{
.baseAddr = EUSCI_A2_BASE,
.intNum = INT_EUSCIA2,
.intPriority = (~0),
.clockSource = EUSCI_A_UART_CLOCKSOURCE_SMCLK,
.bitOrder = EUSCI_A_UART_LSB_FIRST,
.numBaudrateEntries = sizeof(uartMSP432Baudrates) /
sizeof(UARTMSP432_BaudrateConfig),
.baudrateLUT = uartMSP432Baudrates,
.ringBufPtr = uartMSP432RingBuffer[MSP_EXP432P401R_UARTA2],
.ringBufSize = sizeof(uartMSP432RingBuffer[MSP_EXP432P401R_UARTA2]),
.rxPin = UARTMSP432_P3_2_UCA2RXD,
.txPin = UARTMSP432_P3_3_UCA2TXD,
.errorFxn = NULL
}
};
const UART_Config UART_config[MSP_EXP432P401R_UARTCOUNT] = {
{
.fxnTablePtr = &UARTMSP432_fxnTable,
.object = &uartMSP432Objects[MSP_EXP432P401R_UARTA0],
.hwAttrs = &uartMSP432HWAttrs[MSP_EXP432P401R_UARTA0]
},
{
.fxnTablePtr = &UARTMSP432_fxnTable,
.object = &uartMSP432Objects[MSP_EXP432P401R_UARTA2],
.hwAttrs = &uartMSP432HWAttrs[MSP_EXP432P401R_UARTA2]
}
};
const uint_least8_t UART_count = MSP_EXP432P401R_UARTCOUNT;
//在其相应的.h文件中有串口号定义,应用程序就使用这些串口号即可!!!但需要在.c文件中自己定义好
typedef enum MSP_EXP432P401R_UARTName {
MSP_EXP432P401R_UARTA0 = 0,
MSP_EXP432P401R_UARTA2,
MSP_EXP432P401R_UARTCOUNT
} MSP_EXP432P401R_UARTName;
一种使用读取不定长的方法
使用一个任务进行接收,初始只接受一个字节,然后用信号量阻塞任务,当串口收到数据后,再发布信号量,这样只是知道了有串口消息过来,然后再次读取
UART_read(uartHandle, &uartBuffer[1], 50); //中断读到一个数据后,再次读10ms的数据
Task_sleep(5);
UART_readCancel(uartHandle);
记住,取消完后也会进入回调函数,需要区分开两种进入接收回调函数的情况,一个发布信号量,另外一个不需要,只需要等待几个ms即可,这样效率可能会低一点,但是一般应用足够了