注意一点: lauchpad的J4 跳线帽地方的RX和TX需要竖着插。横着插不能通信。
贡献自己写的MSP430G2553的串口通信代码:使用内部DCO到16M,选择串口通信时钟来源为SMCLK=8M (系统时钟2分频)。
1.设置波特率为9600。计算方法为:Fclk=SMCLK=8M. N=Fclk/9600=833.333;则UCxBRx=INT(N)=833 (N取整) 。又UCxBRx=UCBR0+(UCBR1*256) ,所以UCxBR1=3;UCxBR0=65 ; UCBRSx=(N-INT(N)) *8=2 (四舍五入)
下面是源代码:
void UartRegCfg()
{
UCA0CTL1 |=UCSWRST; //reset UART module,as well as enable UART module
UCA0CTL1 |=UCSSEL_2; //UART clock is SMCLK
UCA0BR0 |=65; //Baud N=BCLK/rate,rate=9600,BCLK=SMCLK=8M
UCA0BR1 |=3;
UCA0MCTL = UCBRS1; //UCBRSx=2
UCA0CTL1 &=~UCSWRST; //UART reset end
}
void UartGpioCfg()
{
P1DIR |= BIT2; //P1.2 UART_TX
P1DIR &=~BIT1; //P1.2 UART_RX
P1SEL |= BIT1+BIT2; //select P1.1 and P1.2 as UART port
P1SEL2 |= BIT1+BIT2;
}
void UartInit()
{
UartRegCfg();
UartGpioCfg();
}
/************************************************************************
* 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
************************************************************************/
int UARTGetChar(void)
{
int GetChar=0;
while (!(IFG2&UCA0RXIFG)); //UCA1RXBUF has received a complete character
IFG2&=~UCA0RXIFG; //clear RX interrupt flag
UCA0TXBUF=UCA0RXBUF; //back to display
GetChar =UCA0RXBUF;
while (!(IFG2&UCA0TXIFG)); //waiting UCA0TXBUF is empty
IFG2&=~UCA0TXIFG; //clear TX interrupt flag
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;
}
void SysCtlClockInit()
{
DCOCTL=0;
BCSCTL1=CALBC1_16MHZ;
DCOCTL =CALDCO_16MHZ;
BCSCTL1|=DIVA_1; //ACLK =MCLK/2=8M
BCSCTL2|=DIVS_1; //SMCLK=MCLK/2=8M
}
[ 本帖最后由 zw357234798 于 2012-8-2 09:14 编辑 ]
|