EmbeddedRelated.com
Forums
Memfault Beyond the Launch

S/W UART Implementation Question

Started by "Nirosh P. Wijayaratne" March 25, 2007
Hello,

I am trying to implement a s/w UART @ 9600bps. I am sending a keypress from hyperterminal and am expecting to see it echoed back. However, I am receiving random characters. Once in a while, I will receive the expectected character. Some characters seem to be better than others. For Example 'Q, O, W' is echoed 60% of the time whereas 'I' will rarely appear. Whether any correct characters appear at all seem to be PC dependant.

To me, the problem seems to be with what is going out of the MSP430 not being read properly by the PC. A breakpoint in the code indicats that the keypress is being received ok.

A few facts:

1. I am not using a crystal. Depending soleley on the DCO

2. My RS232 level converter is tested by looping back the send and receive and known to be ok

Any help would be appreciated. Also. any idea if I can run multiple s/w UARTS on one MSP430F2121 or on the MSP430F2121. Code attached below.

Thanks

Nirosh

--------
//#include
#include "timerdriver.h"
#include

#define RXD 0x04 // RXD on P2.2
#define TXD 0x02 // TXD on P1.1

//Conditions for 9600 Baud SW UART, DCO = 1MHz
#define Bitime_5 52 // ~ 0.5 bit length
#define Bitime 104 // ~ 9615 baud

unsigned int RXTXData;
unsigned char BitCnt;

void main()
{
BCSCTL1 = CALBC1_1MHZ; // Set DCO
DCOCTL = CALDCO_1MHZ;

TACTL = TASSEL_2 + MC_2 + TACLR; // SMCLK, cont-mode, clear

CCTL0 = OUT; // TXD Idle as Mark
P1SEL = TXD; // P1.1/TA0 for TXD function
P1DIR = TXD; // TXD output on P1
P2SEL = RXD; // P2.2/TA0 as RXD input

//P1DIR |= 0x08; // Power MAX3221
//P1OUT |= 0x08; //
// Enter LPM0 w/ interrupt
for (;;)
{
RX_Ready(); // UART ready to RX one Byte
_BIS_SR(LPM4_bits + GIE); // Enter LPM4 w/ interr until char RXed
TX_Byte(); // TX Back RXed Byte Received
}

}
// Function Transmits Character from RXTXData Buffer
void TX_Byte (void)
{
BitCnt = 0xA; // Load Bit counter, 8data + ST/SP
CCR0 = TAR; // Current state of TA counter
CCR0 += Bitime; // Some time till first bit
RXTXData |= 0x100; // Add mark stop bit to RXTXData
RXTXData = RXTXData << 1; // Add space start bit
CCTL0 = OUTMOD0 + CCIE; // TXD = mark = idle
while ( CCTL0 & CCIE ); // Wait for TX completion
}

// Function Readies UART to Receive Character into RXTXData Buffer
// Sync capture not possible as DCO=TACLK=SMCLK can be off !!
void RX_Ready (void)
{
BitCnt = 0x8; // Load Bit counter
CCTL0 = CM1 + CCIS0 + OUTMOD0 + CAP + CCIE; // Neg Edge, Cap
}

// Timer A0 interrupt service routine
#pragma vector=TIMERA0_VECTOR
__interrupt void Timer_A (void)
{
CCR0 += Bitime; // Add Offset to CCR0

// RX
if (CCTL0 & CCIS0) // RX on CCI0B?
{
if( CCTL0 & CAP ) // Capture mode = start bit edge
{
CCTL0 &= ~ CAP; // Switch from capture to compare mode
CCR0 += Bitime_5;
_BIC_SR_IRQ(SCG1 + SCG0); // DCO reamins on after reti
}
else
{
RXTXData = RXTXData >> 1;
if (CCTL0 & SCCI) // Get bit waiting in receive latch
RXTXData |= 0x80;
BitCnt --; // All bits RXed?
if ( BitCnt == 0)
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
{
CCTL0 &= ~ CCIE; // All bits RXed, disable interrupt
_BIC_SR_IRQ(LPM4_bits); // Clear LPM4 bits from 0(SR)
}
//>>>>>>>>>> Decode of Received Byte Here <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
// TX
else
{
if ( BitCnt == 0)
CCTL0 &= ~ CCIE; // All bits TXed, disable interrupt
else
{
CCTL0 |= OUTMOD2; // TX Space
if (RXTXData & 0x01)
CCTL0 &= ~ OUTMOD2; // TX Mark
RXTXData = RXTXData >> 1;
BitCnt --;
}
}
}

Beginning Microcontrollers with the MSP430

----- Original Message -----
From: "Nirosh P. Wijayaratne"
To:
Sent: Sunday, March 25, 2007 9:17 AM
Subject: [msp430] S/W UART Implementation Question
> Hello,
>
> I am trying to implement a s/w UART @ 9600bps. I am sending a keypress
> from hyperterminal and am expecting to see it echoed back. However, I am
> receiving random characters. Once in a while, I will receive the
> expectected character. Some characters seem to be better than others. For
> Example 'Q, O, W' is echoed 60% of the time whereas 'I' will rarely
> appear. Whether any correct characters appear at all seem to be PC
> dependant.
>
> To me, the problem seems to be with what is going out of the MSP430 not
> being read properly by the PC. A breakpoint in the code indicats that the
> keypress is being received ok.
>
> A few facts:
>
> 1. I am not using a crystal. Depending soleley on the DCO
>
> 2. My RS232 level converter is tested by looping back the send and
> receive and known to be ok
>
> Any help would be appreciated. Also. any idea if I can run multiple s/w
> UARTS on one MSP430F2121 or on the MSP430F2121. Code attached below.

The baud rate is probably wrong. I've had a software UART working reliably
with the DCO and a 32.768 kHz crystal, using TI code.

Leon
Leon,

Thanks for the reply. Is the 32.768KHz crystal a must to make the UART work reliably?

Maybe that's my problem, I do not have a crystal on my board.

Thanks

Nirosh

----- Original Message -----
From: Leon
To: m...
Sent: Sunday, March 25, 2007 2:03 AM
Subject: Re: [msp430] S/W UART Implementation Question
----- Original Message -----
From: "Nirosh P. Wijayaratne"
To:
Sent: Sunday, March 25, 2007 9:17 AM
Subject: [msp430] S/W UART Implementation Question

> Hello,
>
> I am trying to implement a s/w UART @ 9600bps. I am sending a keypress
> from hyperterminal and am expecting to see it echoed back. However, I am
> receiving random characters. Once in a while, I will receive the
> expectected character. Some characters seem to be better than others. For
> Example 'Q, O, W' is echoed 60% of the time whereas 'I' will rarely
> appear. Whether any correct characters appear at all seem to be PC
> dependant.
>
> To me, the problem seems to be with what is going out of the MSP430 not
> being read properly by the PC. A breakpoint in the code indicats that the
> keypress is being received ok.
>
> A few facts:
>
> 1. I am not using a crystal. Depending soleley on the DCO
>
> 2. My RS232 level converter is tested by looping back the send and
> receive and known to be ok
>
> Any help would be appreciated. Also. any idea if I can run multiple s/w
> UARTS on one MSP430F2121 or on the MSP430F2121. Code attached below.

The baud rate is probably wrong. I've had a software UART working reliably
with the DCO and a 32.768 kHz crystal, using TI code.

Leon
----- Original Message -----
From: "Nirosh P. Wijayaratne"
To:
Sent: Sunday, March 25, 2007 4:08 PM
Subject: Re: [msp430] S/W UART Implementation Question
> Leon,
>
> Thanks for the reply. Is the 32.768KHz crystal a must to make the UART
> work reliably?
>
> Maybe that's my problem, I do not have a crystal on my board.

I think it would be a good idea, although I have had a hardware UART running
reliably using the on-chip oscillator with a PIC.

You could try tweaking your delays, to get it to work. It can't be far out,
if you can receive data OK.

Leon
Thanks Leon,

I did try playing with the delays but it did not see to make it any better...worse...yes. I even tried going down to 2400bps to see if it worked any better but it was the same.

Also on the same topic, any reason why I would not be able to run two UARTS on one MSP430 (assuming I get the one to work first)? :)

Thanks

Nirosh

----- Original Message -----
From: Leon
To: m...
Sent: Sunday, March 25, 2007 8:35 AM
Subject: Re: [msp430] S/W UART Implementation Question
----- Original Message -----
From: "Nirosh P. Wijayaratne"
To:
Sent: Sunday, March 25, 2007 4:08 PM
Subject: Re: [msp430] S/W UART Implementation Question

> Leon,
>
> Thanks for the reply. Is the 32.768KHz crystal a must to make the UART
> work reliably?
>
> Maybe that's my problem, I do not have a crystal on my board.

I think it would be a good idea, although I have had a hardware UART running
reliably using the on-chip oscillator with a PIC.

You could try tweaking your delays, to get it to work. It can't be far out,
if you can receive data OK.

Leon
----- Original Message -----
From: "Nirosh P. Wijayaratne"
To:
Sent: Sunday, March 25, 2007 7:31 PM
Subject: Re: [msp430] S/W UART Implementation Question
> Thanks Leon,
>
> I did try playing with the delays but it did not see to make it any
> better...worse...yes. I even tried going down to 2400bps to see if it
> worked any better but it was the same.
>
> Also on the same topic, any reason why I would not be able to run two
> UARTS on one MSP430 (assuming I get the one to work first)? :)

It's counter-intuitive, but higher baud rates are less critical.

You might be able to run two of them.

Leon
--
Leon Heller
Amateur radio call-sign G1HSM
Yaesu FT-817ND and FT-857D transceivers
Suzuki SV1000S motorcycle
l...@btinternet.com
http://webspace.webring.com/people/jl/leon_heller/
I think you may need a 3.6864MHz (NOT KHz) crystal for 9600. I get
2400 baud with the 32.768KHz, and my 9600 design (which uses a
3.6864MHz crystal) handles one software UART and an interface to a
RS232 Transiever chip for another software UART. This is on a
MSP430F1121a. If you use MSP430 chips with hardware UARTS, you should
be able to go faster.

--- In m..., "Nirosh P. Wijayaratne"
wrote:
>
> Leon,
>
> Thanks for the reply. Is the 32.768KHz crystal a must to make the
UART work reliably?
>
> Maybe that's my problem, I do not have a crystal on my board.
--- In m..., "Leon" wrote:
>
> It's counter-intuitive, but higher baud rates are less critical.
>

Certainly counter-intuitive for me. Could you please explain what you
mean?

Thanks.
Norm.
Leon/Baxter Codeworks,

Thanks for the help last weekend re. the s/w UART. Finally got everything to work. Turns out it was the hardware guy's(me!) fault. My RS232 level converter was not working properly even though the loopback worked ok.

Just as info for anyone else who might have been interested. The F2121 side worked perfectly no crystal at 57600bps. The F4250 side needed a 32kHz crystal and then that too worked perfectly!

Thanks

Nirosh

----- Original Message -----
From: baxtercodeworks
To: m...
Sent: Sunday, March 25, 2007 1:39 PM
Subject: [msp430] Re: S/W UART Implementation Question
I think you may need a 3.6864MHz (NOT KHz) crystal for 9600. I get
2400 baud with the 32.768KHz, and my 9600 design (which uses a
3.6864MHz crystal) handles one software UART and an interface to a
RS232 Transiever chip for another software UART. This is on a
MSP430F1121a. If you use MSP430 chips with hardware UARTS, you should
be able to go faster.

--- In m..., "Nirosh P. Wijayaratne"
wrote:
>
> Leon,
>
> Thanks for the reply. Is the 32.768KHz crystal a must to make the
UART work reliably?
>
> Maybe that's my problem, I do not have a crystal on my board.
>
>

Memfault Beyond the Launch