Electronics > Beginners

Problem with RS-232

(1/2) > >>

Chryseus:
I've been trying for a few days now to get my AVR to communicate with my computer via RS-232.
For the most part this works fine, the computer is getting a constant stream of serial data as I intended however this data is often received as garbage, by reconnecting the cable or reopening the serial device I can eventually get it to receive the data correctly.
I have tried multiple baud rates, voltage levels, parity, data and stop bits to little effect, I should also point out my AVR is being clocked by a 20MHz crystal so there is approximately a 0.2% baud rate error.

This is the code I'm currently using:


--- Code: ---#define F_CPU 20000000UL
#define BAUD 600
#include <avr/io.h>
#include <util/setbaud.h>
#include <stdint.h>

int main()
{
// Set baud rate
UBRR0L = UBRRL_VALUE;
UBRR0H = UBRRH_VALUE;

// 8 Data bits, no parity, 1 stop bit
UCSR0C = 0x00;
UCSR0C |= (1 << UCSZ01);
UCSR0C |= (1 << UCSZ00);

// Enable TX and RX
UCSR0B |= (1 << TXEN0);
UCSR0B |= (1 << RXEN0);
uint8_t msg[] = { 0x41, 0x56, 0x52, 0x0D, 0x0A };
uint8_t i = 0;
while (1)
{
if (i == 5) { i = 0; }
if (UCSR0A & (1 << UDRE0))
{
UDR0 = msg[i];
i++;
}
}
}
--- End code ---

And the circuit:



Any suggestions on how to fix this?

baljemmett:
What do your voltage levels look like on the RS232 line?  Some devices are more tolerant than others, but I had a lot of headscratching and frustation over mangled data that turned out to be the receiver not liking 1 bits that weren't sufficiently negative (0s were alright, at around +9v, but 1s were only around -4v...)

Here's where my ignorance shows, since I'm not certain about what that capacitor will be doing in your circuit (it seems to just be between the power rails), but I think you're getting something like 12v and 0v out for a 0 and a 1 respectively?  I've always just gone the easy route and stuck a MAX232 or equivalent in to do the voltage doubling and inversion for me, so I could be totally wrong about that!

Chryseus:
The voltage output should be all positive between 0V to 12V with the signal phase inverted (I have tried various maximum levels) although I don't have a scope to verify this.
The capacitor shown is the output capacitor on the power supply regulator.
Do you think the lack of negative signal is really the problem ?
If I fail to fix this I will buy a MAX232.

slburris:
Technically to be valid, RS232 signalling levels have to be > +3volts or < -3volts.
This circuit pulls down to 0 volts which is invalid.

That being said, depending on what's at the other end, you can often get away
with this, but it's not good practice.

See: http://en.wikipedia.org/wiki/RS-232

Scott

baljemmett:

--- Quote from: Chryseus on June 23, 2011, 04:33:20 pm ---Do you think the lack of negative signal is really the problem ?

--- End quote ---

Almost certainly; as slburris points out, the range between +/- 3v is an undefined state so although some kit will accept it, you can't really reason about any other potential problems until you're within spec.  It does depend upon the receiving device, even at the lower voltages that the standard accomodates -- like the -4v that  my logic analyser didn't like -- so you might as well work with voltages that are definitely correct...

Navigation

[0] Message Index

[#] Next page

There was an error while thanking
Thanking...
Go to full version
Powered by SMFPacks Advanced Attachments Uploader Mod