复制了论坛上另一个朋友的代码:
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!=' ');
UARTPutChar('
');
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
}
void main()
{
UartInit();
UARTPutstring("Hello");
}
=================UART发送不过来数据。。。
#include <msp430g2553.h>
#define uint8 unsigned char
#define RXBUF_SIZE 16
#define TXBUF_SIZE 16
unsigned char RX_BUFF[RXBUF_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned int RX_IndexR = 0;
unsigned int RX_IndexW = 0;
unsigned char TX_BUFF[TXBUF_SIZE] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned int UART_OutLen = 16;
unsigned int TX_IndexR = 0;
unsigned int TX_IndexW = 0;
uint8 ch = 0;
void UART_init(void)
{
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_2;
UCA0BR0 = 0x68;
UCA0BR1 = 0x00;
UCA0MCTL = UCBRS_2;
UCA0CTL0 &=~ UCPEN;
UCA0CTL0 &=~ UCSPB;
UCA0CTL0 &=~ UC7BIT;
UCA0CTL1 &=~ UCSWRST;
P1SEL |= BIT1 + BIT2;
P1SEL2 |= BIT1 + BIT2;
IE2 |= UCA0RXIE + UCA0TXIE;
unsigned int j;
for(j=0;j<2000;j++);
}
void UART_send(uint8 Chr)
{
IFG2 &=~ UCA0TXIFG; //清除发送中断标识
UCA0TXBUF = Chr; //向发送缓存寄存器写入数据
while((IFG2 & UCA0TXIFG) == 0); //等待发送完成,发送完成IFG2会被置为UCA0TXIFG;
}
void UART0_PutFrame(unsigned char *Ptr, unsigned int Lenth)
{
int i; //定义计数变量
if(IE2 & UCA0TXIE) //如果没有打开发送中断的话,就退出发送程序
{
return;
}
if(Lenth > TXBUF_SIZE) //如果要发送的字符串长度大于发送缓存数组长度的话,就退出发送程序
{
return;
}
for(i=0;i<Lenth;i++) //根据传入的字符串长度,将该字符串写入TX_BUFF发送缓存数组中
{
// delay
TX_BUFF[i] = Ptr[i];
}
TX_IndexR = 0; //设置发送索引
UART_OutLen = Lenth; //把字符串长度赋值给UART_OutLen变量,用于下面的中断中发送。
IFG2 |= UCA0TXIFG; //设置发送中断标识
IE2 |= UCA0TXIE; //使能发送中断
}
#pragma vector = USCIAB0TX_VECTOR //串口中断向量
__interrupt void USCI0TX_ISR(void) //中断函数
{
if(UART_OutLen > 0) //如果发送的字符串
{
UART_OutLen--;
UCA0TXBUF = TX_BUFF[TX_IndexR];
while(!(IFG2 & UCA0TXIFG));
if( ++TX_IndexR >= TXBUF_SIZE)
{
TX_IndexR = 0;
}
}
else IE2 &=~ UCA0TXIE;
}
void main()
{
UART_init();
UART0_PutFrame("Hello", 5);
}
================uart也发不过来数据。
也试过下载的时候先SW UART模式,然后下载完换HW UART模式,都没有用,串口助手都接不到任何数据
大神们,怎么办,是什么问题啊?坐等,急
此帖出自
小平头技术问答
1.你先把你那个HW 和 SW 交换下,看看有没有改善。
2.你有别的USB转串口的工具吗?有的话直接用外部的usb转串口的试下。
3.波特率的停止位,你自己调下看看有没有改善。
一周热门 更多>