TI中文支持网
TI专业的中文技术问题搜集分享网站

关于UART接受缓冲器的疑问

Other Parts Discussed in Thread:MSP430F6779

请问在MSP430F6779的UART通信中,UCAxRXBUF能接收的数据是多大,我在实验使用过程中,发现并不能完整接收返回的字符串,只能接收一半。

Susan Yang:

您可以参考用户指南 www.ti.com/…/slau208q.pdf

36.4.7 UCAxRXBUF Register

,

user6380774:

您好,根据您提供的资料我发现了原因,是因为rxbuf里显示的都是最后一个字符。现在的问题是,我要接收一个完整的字符串信息,但是寄存器只能存下最后一个字符,那请问我该怎么办呢?多谢!

,

Susan Yang:

请问您是和什么设备进行UART通信的?能否给出相关的程序?

另外 请您参考下之前的类似帖子

e2echina.ti.com/…/61954

,

user6380774:

您好,我是和您提供的帖子采取几乎相同的方式,从串口助手向单片机发送字符串命令,我提高了波特率,发现还是不行。代码如下,不正确的地方麻烦您支持,多谢帮助了~

,

Susan Yang:

请您试一下下面的程序,我之前在5529上测试的

示例显示如何配置UCA0 TX和RX中断向量以自动处理字节(字符)接收和发送。收到字节后,它将自动将字节复制到缓冲区。您必须在代码的其他位置设置缓冲区和指针。同样,当您要传输字符串时,请在代码中的某处创建一个缓冲区,并为其分配一个指针。准备好传输字符串时,请启用UCTXIE,ISR将自动处理其余部分。

#include <msp430.h>
#include <stdio.h>
#include <string.h>//******************************************************************************
// UART Initialization *********************************************************
//******************************************************************************#define LED_OUTP1OUT
#define LED_DIRP1DIR
#define LED_PINBIT0#define SMCLK_1152000
#define SMCLK_96001#define UART_MODESMCLK_115200//SMCLK_9600//unsigned char RXDataBuffer[64];
unsigned char TXDataBuffer[64] = "Hello world\r\n\0";
unsigned char* pTXBuffer = &TXDataBuffer[0];
unsigned char* pRXBuffer = &RXDataBuffer[0];
unsigned int bytesToSend;
unsigned int bytesReceived;/** UART timing based on 8MHz SMCLK*/
void initUART()
{// Configure USCI_A0 for UART modeUCA0CTLW0 |= UCSWRST;// Put eUSCI in reset
#if UART_MODE == SMCLK_115200UCA0CTLW0 |= UCSSEL__SMCLK;// CLK = SMCLK// Baud Rate Setting// Use Table 21-5UCA0BRW = 4;UCA0MCTL |= UCOS16 | UCBRF_3 | UCBRS_5;#elif UART_MODE == SMCLK_9600UCA0CTLW |= UCSSEL__SMCLK;// CLK = SMCLK// Baud Rate Setting// Use Table 21-5UCA0BRW = 52;UCA0MCTL |= UCOS16 | UCBRF_1 | UCBRS_0;
#else# error "Please specify baud rate to 115200 or 9600"
#endifUCA0CTLW0 &= ~UCSWRST;// Initialize eUSCIUCA0IE |= UCRXIE;// Enable USCI_A0 RX interrupt
}//******************************************************************************
// Device Initialization *******************************************************
//******************************************************************************void initGPIO()
{// Configure GPIOP3SEL &= ~(BIT3 | BIT4);// USCI_A0 UART operation on FF5529LPP3SEL |= (BIT3 | BIT4);
}void initClockTo16MHz()
{UCSCTL3 = SELREF_2;// Set DCO FLL reference = REFOUCSCTL4 |= SELA_2;// Set ACLK = REFOUCSCTL0 = 0x0000;// Set lowest possible DCOx, MODx// Loop until XT1,XT2 & DCO stabilizes - In this case only DCO has to stabilizedo{UCSCTL7 &= ~(XT2OFFG + XT1LFOFFG + DCOFFG);// Clear XT2,XT1,DCO fault flagsSFRIFG1 &= ~OFIFG;// Clear fault flags}while (SFRIFG1&OFIFG);// Test oscillator fault flag__bis_SR_register(SCG0);// Disable the FLL control loopUCSCTL1 = DCORSEL_5;// Select DCO range 16MHz operationUCSCTL2 |= 249;// Set DCO Multiplier for 8MHz// (N + 1) * FLLRef = Fdco// (249 + 1) * 32768 = 8MHz__bic_SR_register(SCG0);// Enable the FLL control loop// Worst-case settling time for the DCO when the DCO range bits have been// changed is n x 32 x 32 x f_MCLK / f_FLL_reference. See UCS chapter in 5xx// UG for optimization.// 32 x 32 x 8 MHz / 32,768 Hz = 250000 = MCLK cycles for DCO to settle__delay_cycles(250000);
}void TransmitData(unsigned char* string, unsigned int length)
{pTXBuffer = string;bytesToSend = strlen((const char*) string);UCA0IE |= UCTXIE;// Enable USCI_A0 TX interrupt}int main(void)
{WDTCTL = WDTPW | WDTHOLD;// Stop watchdog timerinitGPIO();initClockTo16MHz();initUART();/** Use timer to demonstrate automatic tranmission.* Configure timer to periodically transmit a string.*/TA0CCTL0 |= CCIE;// TACCR0 interrupt enabledTA0CCR0 = 50000;TA0CTL |= TASSEL__SMCLK | MC__CONTINUOUS | ID__8;// SMCLK, continuous mode__bis_SR_register(GIE);while (1){__bis_SR_register(LPM0_bits | GIE);// Enter LPM0__no_operation();// For debugger}
}#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{switch(__even_in_range(UCA0IV,4)){case 0: break;case 2:UCA0IFG &=~ UCRXIFG;// Clear interrupt*pRXBuffer = UCA0RXBUF;// Clear bufferbreak;case 4:UCA0TXBUF = *pTXBuffer++;if(--bytesToSend == 0)UCA0IE &= ~UCTXIE;// Disable USCI_A0 TX interrupt after string has been transmitted.break;default:break;}__bic_SR_register_on_exit(LPM0_bits); // Exit LPM0 on reti
}// Timer A0 interrupt service routine#pragma vector = TIMER0_A0_VECTOR
__interrupt void Timer_A (void)
{static int count = 10;if(--count == 0){count = 10;TransmitData(&TXDataBuffer[0], sizeof(TXDataBuffer));_no_operation();}
}

,

user6380774:

您好!请问您用这个程序试过接收字符串吗?我已经按照老贴子的方法,提高了主频,改变提高了波特率,建立了数组缓存区,经过好久实验发现结果还是一样,用串口助手发送多个字符时还是只能接收单个最后一位,缓存数组保存的也只能是最后一位数据。调试了好久,还是无法发现问题,希望得到您的指导!

,

user6380774:

而且您之前提供的老帖子好像也没有最终的解决方案

,

Susan Yang:

看起来像是之前的传送的字符没有及时取走造成的。

请参考下面帖子内的解决方式

e2echina.ti.com/…/107588

,

user6380774:

非常感谢您对我的帮助!为我提供了一个正确的方向!

,

Susan Yang:

很高兴能帮到您!

赞(0)
未经允许不得转载:TI中文支持网 » 关于UART接受缓冲器的疑问
分享到: 更多 (0)