MSP430FR5739串口程序

时间:2023-03-09 05:49:23
MSP430FR5739串口程序

今天急着用这个片子的串口,匆忙中调试串口也话费了一段时间,在网上下了一个程序,忽然就把所有问题搞清楚了,只是中断就看着头文件中寄存器写的,虽然通讯正常,不过不确定有没有写错。代码如下:

#include <msp430fr5739.h>

void Uart_Init(void)
{
P2SEL1 |= BIT0 + BIT1;
P2SEL0 &= ~(BIT0 + BIT1); // P2.0,1 = USART0 TXD/RXD 选择端口用作接收和发送端口
UCA0CTL1 |= UCSWRST;
UCA0CTL1 = UCSSEL_2; // Set SMCLK as UCLk
UCA0BR0 = 52 ; // 9600 baud
// 8000000/(9600*16) - INT(8000000/(9600*16))=0.083
UCA0BR1 = 0;
// UCBRFx = 1, UCBRSx = 0x49, UCOS16 = 1 (Refer User Guide)
UCA0MCTLW = 0x4911 ;
UCA0CTL1 &= ~UCSWRST; // release from reset
UCA0IE |= UCRXIE; // 使能 USART0 RX 中断
}
/**********************************************************************
* 函数名称: Uart_Put_Char()
* 功能描述: 通过UART向PC机发送一个字符
* 输入参数: c
* 返回参数: 无
**********************************************************************/
void Uart_Put_Char(unsigned char c)
{
while (!(UCA0IFG&UCTXIFG)); //等待发送寄存器为空
UCA0TXBUF = c;
}
/**********************************************************************
* 函数名称: Uart_Put_string
* 功能描述: 通过UART发送字符串
* 输入参数: ptr--指向发送字符串的指针
* 返回参数: 无
**********************************************************************/
void Uart_Put_string(unsigned char *ptr)
{
while(*ptr != '\0')
{
Uart_Put_Char(*ptr++); // 发送数据
}

}
/**********************************************************************
* 函数名称: Uart_Char_Num
* 功能描述: 通过UART发送字符串(特定个数的字符串)
* 输入参数: ulCount 字符串的个数
* 返回参数: 无
**********************************************************************/
void Uart_Char_Num( unsigned char *ptr, unsigned long ulCount )
{
while(ulCount--)
{

Uart_Put_Char(*ptr ++);
}
while (!(UCA0IFG&UCTXIFG));
UCA0TXBUF = '\n'; //发送换行指令

}
void delay()
{
unsigned int i=50000;
while(i--);
}
void main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
CSCTL0_H = 0xA5; // Unlock register
CSCTL1 |= DCOFSEL0 + DCOFSEL1; // Set max. DCO setting
CSCTL2 = SELA_1 + SELS_3 + SELM_3; // set ACLK = vlo; MCLK = DCO
CSCTL3 = DIVA_0 + DIVS_0 + DIVM_0; // set all dividers
CSCTL0_H = 0x01; // Lock Register
Uart_Init();
_EINT();
while(1)
{
delay();
delay();
delay();
delay();
Uart_Put_string("123456789\r\n");
}

}

#pragma vector = USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
UCA0IFG &=~ UCRXIFG; // Clear interrupt
while (!(UCA0IFG&UCTXIFG));
UCA0TXBUF = UCA0RXBUF;
}