调了一整天,串口有数据但是波特率不对,求大神帮帮忙。程序如下:
#define uint unsigned int
#define uchar unsigned char
void SysCtlClockInit()
{
DCOCTL=0;
BCSCTL1=CALBC1_16MHZ;
DCOCTL =CALDCO_16MHZ;
BCSCTL1 |=DIVA_1; //ACLK =MCLK/2=8M
BCSCTL2 |=DIVS_1; //SMCLK=MCLK/2=8M
}
void UartInit()
{
P3SEL |=BIT4+BIT5; //发送和接收引脚为第2功能
UCA0CTL1 |=UCSWRST; //reset UART module,as well as enable UART module
UCA0CTL1 |=UCSSEL_2; //UART clock is SMCLK
UCA0BR0 |=0x41; //Baud N=BCLK/rate,rate=9600,BCLK=SMCLK=8M
UCA0BR1 |=0x03;
UCA0MCTL = UCBRS_1; //UCBRSx=2
UCA0CTL1 &= ~UCSWRST;
//IE2 |= UCA0RXIE; //接收中断使能
}
/************************************************************************
* Function Name : UARTPutChar
* Create Date : 2012/07/27
* Author :
*
* Description :send a character
*
* Param : cTX is willing to send character
************************************************************************/
void UARTPutChar(unsigned char cTX)
{
UCA0TXBUF=cTX;
while (!(IFG2 & UCA0TXIFG)); //waiting UCA0TXBUF is empty
IFG2 &= ~UCA0TXIFG; //clear TX interrupt flag
}
/************************************************************************
* Function Name : UARTGetChar
* Create Date : 2012/07/27
* Author :
*
* Description :get a character
*
* Param : cRX is willing to get character
************************************************************************/
uchar UARTGetChar(void)
{
uchar GetChar;
while (!(IFG2 & UCA0RXIFG)); //UCA1RXBUF has received a complete character
IFG2&=~UCA0RXIFG; //clear RX interrupt flag
GetChar =UCA0RXBUF;
return GetChar;
}
/************************************************************************
* Function Name : UARTPutstring
* Create Date : 2012/07/27
* Author :
*
* Description :output string
*
* Param : char *str point send string
* return: the length of string
************************************************************************/
int UARTPutstring( char *str)
{
unsigned int uCount=0;
do
{
uCount++;
UARTPutChar(*str);
}
while(*++str!='\0');
UARTPutChar('\n');
return uCount;
} |