msp432p401r串口通信程序
时间: 2023-10-11 16:11:59 浏览: 34
下面是一个基于 MSP432P401R 的串口通信程序示例:
```c
#include "msp.h"
void UART_init(void);
void UART_sendChar(char c);
void UART_sendString(char *str);
char UART_getChar(void);
int main(void)
{
char c;
WDT_A->CTL = WDT_A_CTL_PW | WDT_A_CTL_HOLD; // stop watchdog timer
UART_init();
while(1)
{
c = UART_getChar();
UART_sendChar(c);
}
}
void UART_init(void)
{
P1->SEL0 |= BIT2 | BIT3; // set P1.2 and P1.3 as UART pins
P1->SEL1 &= ~(BIT2 | BIT3);
EUSCI_A0->CTLW0 |= EUSCI_A_CTLW0_SWRST; // Put eUSCI in reset
EUSCI_A0->CTLW0 = EUSCI_A_CTLW0_SWRST | EUSCI_A_CTLW0_SSEL__SMCLK; // Configure eUSCI clock source for SMCLK
EUSCI_A0->BRW = 26; // 3MHz / 115200 = 26
EUSCI_A0->MCTLW |= EUSCI_A_MCTLW_OS16 | (0x00A0 << 8); // Modulation UCBRSx=0xA, UCBRFx=0, UCOS16=1
EUSCI_A0->CTLW0 &= ~EUSCI_A_CTLW0_SWRST; // Initialize eUSCI
}
void UART_sendChar(char c)
{
while(!(EUSCI_A0->IFG & EUSCI_A_IFG_TXIFG));
EUSCI_A0->TXBUF = c;
}
void UART_sendString(char *str)
{
while(*str)
{
UART_sendChar(*str++);
}
}
char UART_getChar(void)
{
while(!(EUSCI_A0->IFG & EUSCI_A_IFG_RXIFG));
return EUSCI_A0->RXBUF;
}
```
该程序初始化了 MSP432P401R 的串口,然后通过 `UART_sendChar()` 和 `UART_getChar()` 函数实现了串口的发送和接收功能。在主函数中,程序通过不断从串口接收字符并回传相同的字符来模拟一个回显程序。你可以根据需要修改主函数中的代码来实现你自己的串口通信功能。