串口发送时开辟缓冲区存储字符串,实际测试时发现当波特率高于38400时只能发送首字符,找了好久也没发现问题所在,目前程序用不了,很是着急,希望有人能帮我解决,不胜感激!!!
下面时程序源码,编译环境为IAR 6.2。
#include "msp430f5529.h"
#include "UART.c"
int main( void )
{
// Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
P5SEL |= BIT2 +BIT3;
UCSCTL4 |= SELM__XT2CLK + SELS__XT2CLK;
UCSCTL6 &= ~XT2OFF; //Select extern 1048576Hz crystal(XT2) as clock source
UART_Init(115200);
/***************************************
Tramsmit Test
***************************************/
char tx_str[] = "Chinese Academy of Sciences\n";
char *p = tx_str;
while(1)
{
while(*p != '\0')
{
UART_PutChar(*p);
p++;
}
p = tx_str;
__delay_cycles(1000000);
}
UART.c
#include "msp430f5529.h"
/*UART Transmitter buffer*/
#define TX_BUFFER_SIZE 128
volatile char tx_buffer[TX_BUFFER_SIZE] = {0};
#if TX_BUFFER_SIZE <= 256
unsigned char tx_wr_index,tx_rd_index;
#else
unsigned int tx_wr_index,tx_rd_index;
#endif
#if TX_BUFFER_SIZE < 256
unsigned char tx_counter;
#else
unsigned int tx_counter;
#endif
void UART_Init(unsigned long Baud_Rate)
{
P3SEL |= BIT3 + BIT4;
UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL__SMCLK;
switch(Baud_Rate)
{
case 9600 : UCA0BR0 = 109;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_2 + UCBRF_0;
break;
case 19200 : UCA0BR0 = 54;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_5 + UCBRF_0;
break;
case 38400 : UCA0BR0 = 27;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_2 + UCBRF_0;
break;
case 57600 : UCA0BR0 = 18;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_2 + UCBRF_0;
break;
case 115200 : UCA0BR0 = 9;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_1 + UCBRF_0;
break;
default : UCA0BR0 = 109;
UCA0BR1 = 0;
UCA0MCTL |= UCBRS_2 + UCBRF_0;
}
UCA0CTL1 &= ~UCSWRST;
UCA0IFG &= ~UCTXIFG;
UCA0IE |= UCTXIE + UCRXIE;
//UCA0IE |= UCRXIE;
_EINT();
}
void UART_PutChar(char c)
{
while (tx_counter == TX_BUFFER_SIZE);
_DINT();
if (tx_counter || (UCA0STAT & UCBUSY))
{
//UCA0IFG |= UCTXIFG;
tx_counter++;
tx_buffer[tx_wr_index++] = c;
#if TX_BUFFER_SIZE != 256
if (tx_wr_index == TX_BUFFER_SIZE)
{
tx_wr_index=0;
}
#endif
}
else
{
UCA0TXBUF =c;
}
_EINT();
}
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_UART (void)
{
//Transmit Interrupet Service
if(UCA0IFG & UCTXIFG)
{
if(tx_counter)
{
--tx_counter;
UCA0TXBUF = tx_buffer[tx_rd_index++];
#if TX_BUFFER_SIZE != 256
if (tx_rd_index == TX_BUFFER_SIZE)
{
tx_rd_index=0;
}
#endif
}
else
{
UCA0IFG &= ~UCTXIFG;
//UCA0IE &= ~UCTXIE;
}
}
}