|
大神,我写的串口程序,上位机显示出现乱码,让后按复位键就恢复了,之后改为 UCSWRST软件复位。我是在串口配置前关闭,配置完成后打开,这样对不对?
#include "msp430g2553.h"
#define uchar unsigned char
#define uint unsigned int
# define LED1_1 P1OUT |= BIT0
# define LED1_0 P1OUT &= ~BIT0
# define LED2_1 P1OUT |= BIT6
# define LED2_0 P1OUT &= ~BIT6
/*****系统时钟初始化*****/
void Clock_Init()
{
//BCSCTL2 |= SELS;
//BCSCTL1 = CALBC1_8MHZ;//MCLK为DCO,8MHZ
//DCOCTL = CALDCO_8MHZ;
BCSCTL1 = CALBC1_1MHZ;//MCLK为DCO,8MHZ
DCOCTL = CALDCO_1MHZ;
}
/*****串口设置*****/
void UART_Init()
{
P1SEL = BIT1 + BIT2;//P1.1 P1.2设置为TXD,RXD
P1SEL2 = BIT1 + BIT2;
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_1; //ACLK
UCA0BR0 = 3; // ACLK 32768Hz 9600 32768Hz/9600 = 3.41
UCA0BR1 = 0; // 32768Hz 9600
UCA0MCTL = UCBRS1 + UCBRS0; // Modulation UCBRSx = 3
UCA0CTL1 &= (~UCSWRST);//串口设置完毕,在UCSWRST=1时设置串口
UC0IE |= UCA0TXIE + UCA0RXIE;//使能接收和发送中断
}
/*****串口发送数据函数*****/
void Send_Byte(uchar data)
{
while(IFG2 & UCA0TXIFG == 0);//发送寄存器空的时候发送数据
UCA0TXBUF = data;
}
/*****处理来自串口的接收中断*****/
#pragma vector = USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
uchar data = 0;
data = UCA0RXBUF;//接收到的数据存起来
Send_Byte(data);//将接收到的数据再发送出去
}
/*****处理来自串口的发送中断*****/
#pragma vector = USCIAB0TX_VECTOR
__interrupt void USCI0TX_ISR(void)
{
}
/*****主函数*****/
int main( void )
{
WDTCTL = WDTPW + WDTHOLD;
Clock_Init();
UCA0CTL1 &= ~UCSWRST;
UART_Init();//串口初始化
//_EINT();//开中断
while(1)
{
Send_Byte('n');
Send_Byte('s');
}
}
|
|