项目简介
本项目受流行的 MSP430 Printf 启发而开发,主要为 MSP432 微控制器实现了 UART 的打印和输入功能。借助 UART 实现 MSP432 与其他设备间的数据通信,提供了方便的输入输出接口。
项目的主要特性和功能
- 提供类似 C 标准库
printf
和gets
的MSPrintf
和MSPgets
功能,便于格式化输出和接收用户输入。 - 可对 UART 进行配置,设置波特率、奇偶校验等参数。
- 能初始化 UART 和时钟硬件,确保系统正常运行。
- 支持用户通过串口终端向 MSP432 发送信息,MSP432 可回显接收到的字符。
安装使用步骤
安装前提
确保已安装 MSP432 SimpleLink SDK 库。安装时,访问链接并选择 SIMPLELINK - MSP432 - SDK 选项,安装完成后将本项目导入 Code Composer 的工作区。
使用步骤
- 在代码中包含必要的头文件: ```c
include
include "MSPIO.h"
2. 声明 UART 配置结构变量:
c
eUSCI_UART_Config UART0Config =
{
EUSCI_A_UART_CLOCKSOURCE_SMCLK,
13,
0,
37,
EUSCI_A_UART_NO_PARITY,
EUSCI_A_UART_LSB_FIRST,
EUSCI_A_UART_ONE_STOP_BIT,
EUSCI_A_UART_MODE,
EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION
};
3. 初始化 UART 和时钟硬件:
c
include
include "MSPIO.h"
eUSCI_UART_Config UART0Config = { EUSCI_A_UART_CLOCKSOURCE_SMCLK, 13, 0, 37, EUSCI_A_UART_NO_PARITY, EUSCI_A_UART_LSB_FIRST, EUSCI_A_UART_ONE_STOP_BIT, EUSCI_A_UART_MODE, EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION };
void main() { WDT_A_holdTimer();
/*24 MHz clock, you might want to try different clock rates*/
CS_Init();
/*Initialize the desired UART peripheral, in this example we use UART A0.
Pass the initialized UART configuration structure variable*/
UART_Init(EUSCI_A0_BASE, UART0Config);
}
4. 使用 `MSPrintf` 进行格式化输出:
c
char c = 'c';
char str[] = "Hello World";
int integer = 1234;
MSPrintf(EUSCI_A0_BASE, "This is a formatted string %c, %s, %i", c, str, integer);
5. 使用 `MSPgets` 等待用户输入:
c
char Buffer[128];
/Wait for user input/
MSPgets(EUSCI_A0_BASE, Buffer, sizeof(Buffer));
/if we are here that means the user sent a '\r' or '\n' character, the received string will be stored in the Buffer array/ ```
其他说明
若使用 MSP432 PC UART,需初始化 UART A0,然后打开喜欢的串口终端并输入 COM 端口。若不知道 MSP432 的 COM 端口,可参考 此指南。若要禁用 MSP432 的回显功能,可在 UARTIO.c
源文件的 EUSCIA0_IRQHandler
下注释掉以下代码行:
c
/*Transmit data only if it made it to the buffer*/
MAP_UART_transmitData(EUSCI_A0_BASE, c);
下载地址
点击下载 【提取码: 4003】【解压密码: www.makuang.net】