The UART port of MSP432 is directly connected to the debugger:
In fact, we can directly use the USB port of the debugger as a serial port, using P1.2 and P1.3, and no additional wiring is required, which is very convenient.
Directly borrowing the example provided by TI, the main program is as follows:
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++)
{;}
}
}
Copy code
Serial port initialization:
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);
}
Copy code
String sending function:
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++;
}
}
Copy code
Test results:
Serial port test ok
|