MSP432的uart口是直接与调试器连接的:
其实我们可以直接用调试器的usb口当做串口用,使用的就是P1.2和P1.3,并不需要额外接线,非常方便。
直接借用TI官方提供的列子,主程序如下:
int main(void)
{
WDT_A->CTL = WDT_A_CTL_PW | // Stop watchdog timer
WDT_A_CTL_HOLD;
clk_init();//clk init
uart_init();//uart init,9600
while(1)
{
send_str((uint8_t *)"uart test...\r\n");
for(int i=0;i<10000;i++)
{;}
}
}
串口初始化:
void uart_init(void)
{
// Configure UART pins
P1->SEL0 |= BIT2 | BIT3; // set 2-UART pin as secondary function
// Configure UART
EUSCI_A0->CTLW0 |= EUSCI_A_CTLW0_SWRST; // Put eUSCI in reset
EUSCI_A0->CTLW0 = EUSCI_A_CTLW0_SWRST | // Remain eUSCI in reset
EUSCI_B_CTLW0_SSEL__SMCLK; // Configure eUSCI clock source for SMCLK
// Baud Rate calculation
// 12000000/(16*9600) = 78.125
// Fractional portion = 0.125
// User's Guide Table 21-4: UCBRSx = 0x10
// UCBRFx = int ( (78.125-78)*16) = 2
EUSCI_A0->BRW = 78; // 12000000/16/9600
EUSCI_A0->MCTLW = (2 << EUSCI_A_MCTLW_BRF_OFS) |
EUSCI_A_MCTLW_OS16;
EUSCI_A0->CTLW0 &= ~EUSCI_A_CTLW0_SWRST; // Initialize eUSCI
EUSCI_A0->IFG &= ~EUSCI_A_IFG_RXIFG; // Clear eUSCI RX interrupt flag
EUSCI_A0->IE |= EUSCI_A_IE_RXIE; // Enable USCI_A0 RX interrupt
// Enable global interrupt
__enable_irq();
// Enable eUSCIA0 interrupt in NVIC module
NVIC->ISER[0] = 1 << ((EUSCIA0_IRQn) & 31);
}
字符串发送函数:
void send_str(uint8_t *str)
{
while(*str!='\0')
{
// Check if the TX buffer is empty first
while(!(EUSCI_A0->IFG & EUSCI_A_IFG_TXIFG));
// Echo the received character back
EUSCI_A0->TXBUF = *str;
str++;
}
}
测试结果:
串口测试ok
|