EmbeddedRelated.com
Forums
Memfault Beyond the Launch

UART Baud rate help

Started by Andy March 5, 2007
I am using the MSP430F2274 with IAR kickstart and use FET-Pro430 to
download. I am trying to use the UART to send a message accross to my
computer. My following program works and sends the message to the
computer, but it glitches a lot on the recieving side. It overwrites
and forgets characters a lot. I have taken most of the code from the
sample programs from TI.com. My main question is why won't the baud
rate calculations I found work on my program. I use the current ones
and it sends at 9600 baud rate to the computer, but I do the
calculations and I get vastly different numbers i.e.:
UCA0BR0 = 6;
UCA0BR1 = 0;
UCA0MCTL = UCBRF_8;
but it doesn't transmit correctly. I found the ones in my code mostly
by trial and error. Any help with this issue would be greatly
appreciated.
code:
#include "msp430x22x4.h"

void TX_Msg(void);

int i = 0;
unsigned char msg[] = "\n\rbaud rate?!";

int main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
TBCCTL0 = CCIE; // TBCCR0 interrupt on
TBCCR2 = 0xFF;

TBCTL = TBSSEL_2 + MC_2; // SMCLK, contmode
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;

P3SEL = 0x30; // P3.4,5 = USCI_A0
P1DIR |= 0x01;

UCA0CTL1 |= UCSWRST;
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 0x08; // ??????????
UCA0BR1 = 0x08; // ??????????
UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5??
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state
IE2 |= UCA0RXIE + UCA0TXIE; // Enable USCI_A0

__bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts
enabled

}

#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void)
{
i = 0;
TX_Msg();
}

void TX_Msg(void)
{
while(i < sizeof msg){
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready
UCA0TXBUF = msg[i++];
}
IFG2 = 0;
}

// Timer B0 interrupt service routine
#pragma vector=TIMERB0_VECTOR
__interrupt void Timer_B (void)
{
P1OUT ^= 0x01; // Toggle P1.0
i = 0;
TX_Msg();
TBCCR0 = 0xFF; // Add Offset to TBCCR0
}
peace,
Andy

p.s. Very new at programming the MSP430, be nice :-/

Beginning Microcontrollers with the MSP430

Just glancing at your code, it looks like you're trying to run the DCO
at 1 Mhz. If so, shouldn't your baud control reg be 1 Mhz/9600 0x0068h, or 68 and 00 for 0 and 1 respectively. And that would be
just a starting point, then you should scope it from there to
verify/adjust. Also, it doesn't look like you're storing the received
characters anywhere. So I would expect each new character the
overwrite the last. Anyway, this is without me looking at your
datasheet/user guide, so my apologies if I sound ignorant.

-Peter

--- In m..., "Andy" wrote:
>
> I am using the MSP430F2274 with IAR kickstart and use FET-Pro430 to
> download. I am trying to use the UART to send a message accross to my
> computer. My following program works and sends the message to the
> computer, but it glitches a lot on the recieving side. It overwrites
> and forgets characters a lot. I have taken most of the code from the
> sample programs from TI.com. My main question is why won't the baud
> rate calculations I found work on my program. I use the current ones
> and it sends at 9600 baud rate to the computer, but I do the
> calculations and I get vastly different numbers i.e.:
> UCA0BR0 = 6;
> UCA0BR1 = 0;
> UCA0MCTL = UCBRF_8;
> but it doesn't transmit correctly. I found the ones in my code mostly
> by trial and error. Any help with this issue would be greatly
> appreciated.
> code:
> #include "msp430x22x4.h"
>
> void TX_Msg(void);
>
> int i = 0;
> unsigned char msg[] = "\n\rbaud rate?!";
>
> int main(void)
> {
> WDTCTL = WDTPW + WDTHOLD; // Stop WDT
> TBCCTL0 = CCIE; // TBCCR0 interrupt on
> TBCCR2 = 0xFF;
>
> TBCTL = TBSSEL_2 + MC_2; // SMCLK, contmode
> BCSCTL1 = CALBC1_1MHZ; // Set DCO
> DCOCTL = CALDCO_1MHZ;
>
> P3SEL = 0x30; // P3.4,5 = USCI_A0
> P1DIR |= 0x01;
>
> UCA0CTL1 |= UCSWRST;
> UCA0CTL1 |= UCSSEL_2; // SMCLK
> UCA0BR0 = 0x08; // ??????????
> UCA0BR1 = 0x08; // ??????????
> UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5??
> UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state
> IE2 |= UCA0RXIE + UCA0TXIE; // Enable USCI_A0
>
> __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts
> enabled
>
> }
>
> #pragma vector=USCIAB0RX_VECTOR
> __interrupt void USCI0RX_ISR(void)
> {
> i = 0;
> TX_Msg();
> }
>
> void TX_Msg(void)
> {
> while(i < sizeof msg){
> while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready
> UCA0TXBUF = msg[i++];
> }
> IFG2 = 0;
> }
>
> // Timer B0 interrupt service routine
> #pragma vector=TIMERB0_VECTOR
> __interrupt void Timer_B (void)
> {
> P1OUT ^= 0x01; // Toggle P1.0
> i = 0;
> TX_Msg();
> TBCCR0 = 0xFF; // Add Offset to TBCCR0
> }
> peace,
> Andy
>
> p.s. Very new at programming the MSP430, be nice :-/
>
I have tried the calculated baud rates with my code and it doesn't
work correctly. I use the 0x08 and 0x08 for 0 and 1, and they work
for some random reason. When I try 68 I get nothing coming across. I
would like to know if I set my timers up correctly, or if there is
something I am missing out on that is ruining my baud rate. Thanks!

-Andy

--- In m..., "thapeez" wrote:
>
> Just glancing at your code, it looks like you're trying to run the DCO
> at 1 Mhz. If so, shouldn't your baud control reg be 1 Mhz/9600 > 0x0068h, or 68 and 00 for 0 and 1 respectively. And that would be
> just a starting point, then you should scope it from there to
> verify/adjust. Also, it doesn't look like you're storing the received
> characters anywhere. So I would expect each new character the
> overwrite the last. Anyway, this is without me looking at your
> datasheet/user guide, so my apologies if I sound ignorant.
>
> -Peter
>
> --- In m..., "Andy" wrote:
> >
> > I am using the MSP430F2274 with IAR kickstart and use FET-Pro430 to
> > download. I am trying to use the UART to send a message accross to my
> > computer. My following program works and sends the message to the
> > computer, but it glitches a lot on the recieving side. It overwrites
> > and forgets characters a lot. I have taken most of the code from the
> > sample programs from TI.com. My main question is why won't the baud
> > rate calculations I found work on my program. I use the current ones
> > and it sends at 9600 baud rate to the computer, but I do the
> > calculations and I get vastly different numbers i.e.:
> > UCA0BR0 = 6;
> > UCA0BR1 = 0;
> > UCA0MCTL = UCBRF_8;
> > but it doesn't transmit correctly. I found the ones in my code mostly
> > by trial and error. Any help with this issue would be greatly
> > appreciated.
> >
> >
> > code:
> >
> >
> > #include "msp430x22x4.h"
> >
> > void TX_Msg(void);
> >
> > int i = 0;
> > unsigned char msg[] = "\n\rbaud rate?!";
> >
> > int main(void)
> > {
> > WDTCTL = WDTPW + WDTHOLD; // Stop WDT
> > TBCCTL0 = CCIE; // TBCCR0 interrupt on
> > TBCCR2 = 0xFF;
> >
> > TBCTL = TBSSEL_2 + MC_2; // SMCLK, contmode
> > BCSCTL1 = CALBC1_1MHZ; // Set DCO
> > DCOCTL = CALDCO_1MHZ;
> >
> > P3SEL = 0x30; // P3.4,5 = USCI_A0
> > P1DIR |= 0x01;
> >
> > UCA0CTL1 |= UCSWRST;
> > UCA0CTL1 |= UCSSEL_2; // SMCLK
> > UCA0BR0 = 0x08; // ??????????
> > UCA0BR1 = 0x08; // ??????????
> > UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5??
> > UCA0CTL1 &= ~UCSWRST; // **Initialize USCI
state
> > IE2 |= UCA0RXIE + UCA0TXIE; // Enable USCI_A0
> >
> > __bis_SR_register(LPM0_bits + GIE); // Enter LPM0, interrupts
> > enabled
> >
> > }
> >
> > #pragma vector=USCIAB0RX_VECTOR
> > __interrupt void USCI0RX_ISR(void)
> > {
> > i = 0;
> > TX_Msg();
> > }
> >
> > void TX_Msg(void)
> > {
> > while(i < sizeof msg){
> > while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready
> > UCA0TXBUF = msg[i++];
> > }
> > IFG2 = 0;
> > }
> >
> > // Timer B0 interrupt service routine
> > #pragma vector=TIMERB0_VECTOR
> > __interrupt void Timer_B (void)
> > {
> > P1OUT ^= 0x01; // Toggle P1.0
> > i = 0;
> > TX_Msg();
> > TBCCR0 = 0xFF; // Add Offset to TBCCR0
> > }
> >
> >
> > peace,
> > Andy
> >
> > p.s. Very new at programming the MSP430, be nice :-/
>

Memfault Beyond the Launch