Author Topic: Problem with RS-232  (Read 4107 times)

0 Members and 1 Guest are viewing this topic.

Offline ChryseusTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: gb
    • My Website
Problem with RS-232
« on: June 23, 2011, 04:00:22 pm »
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: [Select]
#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++;
}
}
}

And the circuit:



Any suggestions on how to fix this?
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: Problem with RS-232
« Reply #1 on: June 23, 2011, 04:24:24 pm »
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!
« Last Edit: June 23, 2011, 04:26:10 pm by baljemmett »
 

Offline ChryseusTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: gb
    • My Website
Re: Problem with RS-232
« Reply #2 on: June 23, 2011, 04:33:20 pm »
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.
 

Offline slburris

  • Frequent Contributor
  • **
  • Posts: 542
  • Country: us
Re: Problem with RS-232
« Reply #3 on: June 23, 2011, 04:34:41 pm »
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
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: Problem with RS-232
« Reply #4 on: June 23, 2011, 04:57:10 pm »
Do you think the lack of negative signal is really the problem ?

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...
 

Offline ChryseusTopic starter

  • Regular Contributor
  • *
  • Posts: 87
  • Country: gb
    • My Website
Re: Problem with RS-232
« Reply #5 on: June 23, 2011, 05:31:17 pm »
Ok thanks for helping guys I'll grab a MAX232 soon or build my own driver to test it out.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4195
  • Country: us
Re: Problem with RS-232
« Reply #6 on: June 24, 2011, 05:33:43 am »
If your AVR is transmitting continuously, there is a communications failure mode where an rs232 receiver cannot reliably detect when the characters start, because there is no "mark" time on the line that is so long that it MUST be an inter-character gap.  This is annoying.

If this is your problem, it should be the case that you'll reliably receive data on the PC side if the AVR is reset/powered-on/etc while connected, instead of connecting after the data stream has started (perhaps assuming that your level converter works OK when the AVR is initializing.)  And you could prevent it from happening by having the AVR periodically stop transmitting for several character times...

I don't think I've seen an rs232 receiver that wouldn't accept 0/12V levels for a very long time.  I remember trying either that or 0/5V when trying to connect an Intel 8086 SDK to a CPM machine, and being pleased when it worked...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf