ATmega162 + ST485CN (RS485) problem with sending data.

Go To Last Post
21 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone.

 

I send one byte (continuously) from atmega162 through ST485CN to RS485 line. I receive what hapen on USB-RS485 converter (ATC-820) connected to laptop. I visualizes receiving data on simple TeraTerm terminal.

I allway get the same anser when i send the same byte for example, when I send 0x00 I'm receiving 0xff, when I send 0x66 -> 0xa6 and so on.

Any additional resistors are helpless. If I do this by RS232 and MAX232 it works fine.

I realy need to use interrupts to send one byte?

Please help me with that.

 

Grzegorz

p.s. I'm working on AtmelStudio7.0 and I know the schema is weak but readable :)

 

/*
 * Testy_RS485.cpp
 *
 * Created: 15.11.2017 18:59:01
 *
 */
#define F_CPU	11059200
#include <avr/io.h>
#include <util/delay.h>
// port 0
#define BAUD485	9600
#define BRC485		((F_CPU/16/BAUD485) -1)
// yellow led - status
#define LED1 (1 << PA0);
#define LED1_ON     PORTA &= ~LED1; // LED to VCC
#define LED1_OFF    PORTA |=  LED1; // LED to VCC
#define LED1_TOGGLE PORTA ^=  LED1;
//--------------------------------------------------------------
void RS485_init()
{
	/* Set baud rate */
	UBRR0H = (unsigned char)(BRC485 >> 8);
	UBRR0L = (unsigned char) BRC485;
	/* Just in case ;) */
	UCSR0A = 0;
	/* receiver and transmitter */
	UCSR0B = (1 << TXEN0);//|(1<<RXEN0);
	/* Set frame format: 8data, 1stop bit */
	UCSR0C = (1 << URSEL0)|(3 << UCSZ00);//(1<<USBS0)|
    // direction
	DDRD |= (1 << PD2);
	_delay_ms(500);
}
//--------------------------------------------------------------
// RS485 nadajnik
void RS485_sendingMode()
{
	PORTD |=  0x04; //OFF
	LED1_ON;
}
// RS485 odbiornik
void RS485_receivingMode()
{
	PORTD &= ~0x04; //ON
	LED1_OFF;
}
void RS485_sendByte(unsigned char temp)
{
	///* Wait for empty transmit buffer */
	while ( !(UCSR0A & (1 << UDRE0)) ) ;
	/* Put data into buffer, sends the data */
	UDR0 = temp;
}
unsigned char RS485_receiveByte( void )
{
	/* Wait for data to be received */
	while ( !(UCSR0A & (1 << RXC0)) ) ;
	/* Get and return received data from buffer */
	return UDR0;
}
//--------------------------------------------------------------
//--------------------------------------------------------------
int main(void)
{
	DDRA |= (1 << PA0); // direction yellow LED
	//
	RS485_init();
	RS485_sendingMode();
	_delay_ms(500);
	while (1)
	{
		LED1_TOGGLE;
		RS485_sendByte(0x0f);
	}
}

 

Attachment(s): 

This topic has a solution.
Last Edited: Tue. Nov 21, 2017 - 05:49 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Is that your entire code? It does not make sense. How are you controlling the tx enable?

Jim

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
How are you controlling the tx enable?

I was puzzled as well, but note:

 

-- According to the schematic (OP will need to verify actual wiring and levels at the TE pin) TE and /RE are connected to PD2.

-- PD2 is made an output

-- PD2 is set high (and stays high) in

gotik wrote:
RS485_sendingMode()

-- The comment is ... interesting.  "OFF"? 

gotik wrote:
PORTD |= 0x04; //OFF

-- In real RS485 work, we would of course "manage" TE.  And pay attention to TXC instead of UDRE.  But for continuous sending on one-byte packets, I cannot see where the posted code would be "wrong" for a test program.

 

I don't really know why the test program isn't working.  Is the AVR really running at 11.xxMHz?  How has this been proven?  Is the receiver expecting 9600?  Are the A/B connections and Gnd proper?  I see bias and load resistors but cannot read the value.  Tine to get out the 'scope, send 'U' characters, and measure the bit width at the receiver's "RXD" pin or equivalent.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

theusch wrote:
Is the AVR really running at 11.xxMHz?

The picture of the circuit does show an 11059 crystal, but yes, if DIV8 is programmed then thats a problem

 

ATmega162 is a VERY old part.  I had to open AS4.19 in order to look up the fuse bits.  It's not listed in AS6_2.

 

 

 

JIm

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

jgmdesign wrote:
ATmega162 is a VERY old part. I had to open AS4.19 in order to look up the fuse bits. It's not listed in AS6_2.

Interesting.  From AS 7.x:

From AS 6.2:

jgmdesign wrote:
but yes, if DIV8 is programmed then thats a problem

OP's "picture" of the fuse settings does not have CKDIV8 programmed.  However, I cannot tell from what is posted whether those fuse values have actually been applied successfully to the AVR under test.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

It's not listed in AS6_2.

But it is on AS7?? wink Maybe you need to select a tool that supports ye' olde chip.

 

 

 

can't go further as I don't have a chip.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

we concur!!

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ahhh!

The machine I was using didnt have a programmer attached to it so under Device Programming>Tool I picked SIMULATOR, and here are my options in AS6_2:

 

I went and picked STK500 on Com1 and yes it does show up there

If you want a career with a known path - become an undertaker. Dead people don't sue! - Kartman

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thx for so many answers. I have read all, but still I get no solution in that :(

 

 

jgmdesign wrote:
Is that your entire code? It does not make sense. How are you controlling the tx enable? Jim

I thought that isnt nessesery when I'm only seending data constantly?

 

theusch wrote:
OP's "picture" of the fuse settings does not have CKDIV8 programmed. However, I cannot tell from what is posted whether those fuse values have actually been applied successfully to the AVR under test.

Yes its set before download the program and verified after. During the test i check it again. I have no idea how can I convince You I did that

 

theusch wrote:
-- According to the schematic (OP will need to verify actual wiring and levels at the TE pin) TE and /RE are connected to PD2. -- PD2 is made an output -- PD2 is set high (and stays high) in

I'm still trying diferent things, but DE (not TE???) and /RE are connected to PD2 and set as output and set as high - if not, I would be in receiving mode and I'm getting some data but not adequate to sending data.

 

theusch wrote:
-- The comment is ... interesting. "OFF"? gotik wrote: PORTD |= 0x04; //OFF

This is my mistake, but does not affect to the wrong work of my case - corrected :)

 

theusch wrote:
I don't really know why the test program isn't working. Is the AVR really running at 11.xxMHz? How has this been proven? Is the receiver expecting 9600? Are the A/B connections and Gnd proper? I see bias and load resistors but cannot read the value. Tine to get out the 'scope, send 'U' characters, and measure the bit width at the receiver's "RXD" pin or equivalent.

The same program works fine when I connect port D to MAX232 and converter RS232->USB. I think it proves all of Yours questions.

 

theusch wrote:
Are the A/B connections and Gnd proper?

I have not any GND on my converter and never use Gnd to program controller such as Carel or JohnsonControls via this converter.

 

theusch wrote:
I see bias and load resistors but cannot read the value.

1. From A to Vcc -> 10kOhms/2 = 5kOhms (I dont have 4,7k now)

2. From B to Gnd -> 10kOhms/2 = 5kOhms (I dont have 4,7k now)
3. Between A and B 120Ohms

4. I have not any pullup resistor between Atmega and ST485. Maybe this is a problem?

 

theusch wrote:
Tine to get out the 'scope, send 'U' characters, and measure the bit width at the receiver's "RXD" pin or equivalent.

I have not an oscilloscope, then I dont do this unfortunately :(

 

If above dont help You to help me, I will buy any atmega CPU (for example ATmega8) and MAX485 and i will try again.

 

Anyway thx for current help :)))))

 

Grzegorz

 

 

 

 

 

 

Last Edited: Tue. Nov 21, 2017 - 07:47 AM
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Have you tried swapping A and B?

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, but I do not know why this would help?

However, looking at the bits, I sometimes have the impression that I get the negative bits in the answer, so I will try this - However, I have no confidence in this.

 

Thx

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gotik wrote:

No, but I do not know why this would help?

 

Read this...

 

http://www.bb-elec.com/Learning-...•-Polarities-for-Differential-Pair-Signals-(RS-422.aspx

 

 

Short summary, people disagree about which is A and which is B.

'This forum helps those who help themselves.'

 

pragmatic  adjective dealing with things sensibly and realistically in a way that is based on practical rather than theoretical consideration.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can use your PC’s soundcard as a simple oscilloscope. Works well for 9600 baud. Google soundcard oscilloscope

How many times have i had rs485 wires swapped? Too many.

Last Edited: Tue. Nov 21, 2017 - 12:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gotik wrote:
I think it proves all of Yours questions.

No, it certainly does not.  For example, how does that "prove" that A and B are not swapped?  How does that "prove" that reasonable resistor values are installed?  And common ground? and similar.

gotik wrote:
I have not any GND on my converter

Hmmm... others will need to comment on that.

 

gotik wrote:
Tine to get out the 'scope, send 'U' characters, and measure the bit width at the receiver's "RXD" pin or equivalent.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
Try this: http://www.bb-elec.com/Learning-...(RS-422.aspx

About this article - unfortunately I was wrong, now I will be smarter.

 

I finally problem solved !!!!

 

I swapped A and B and it works fine - this is strange for me, but Now I understand why.

Above article and You (in particular Brian Fairchild - first thought of swapped) helped me :)))))))

Thank you very much.

 

Kartman wrote:
You can use your PC’s soundcard as a simple oscilloscope. Works well for 9600 baud. Google soundcard oscilloscope

On the occasion: how can I do this for the future: direclty or via Voltage divider (resistor) or other?

 

Thanks for all answers.

 

Grzegorz

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

swapped connections are a common problem - you've got a 50% chance of getting it wrong. With the leds you can also observe this - you'll see the receive leds flicker if the polarity is correct, otherwise with incorrect polarity, they will be full on.

 

There's plenty of examples of using the soundcard as an oscilloscope on the interwebs with resistor dividers. The soundcard input is usually around 1V, so for 5V signals you need a 5:1 divider.

https://makezine.com/projects/so...

Last Edited: Tue. Nov 21, 2017 - 10:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The fuse diagram shows the low fuse to be 0x00.  This sets the mega162 to look for an external square wave for its main system clock.   This may be causing a problem.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sound-card oscilloscopes expect to have an AC input signal with a +0.6v DC offset and a peak-to-peak maximum input voltage of about +1.5 volts.  They are designed to digitize line-input level audio signals.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Kartman wrote:
swapped connections are a common problem - you've got a 50% chance of getting it wrong. With the leds you can also observe this - you'll see the receive leds flicker if the polarity is correct, otherwise with incorrect polarity, they will be full on.

Yes, one LED was full on. After swapped they stay off in this case.

 

Thx for links.

 

Simonetta wrote:
Sound-card oscilloscopes expect to have an AC input signal with a +0.6v DC offset and a peak-to-peak maximum input voltage of about +1.5 volts. They are designed to digitize line-input level audio signals.

Thx too for specifications :)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Simonetta wrote:

The fuse diagram shows the low fuse to be 0x00.  This sets the mega162 to look for an external square wave for its main system clock.   This may be causing a problem.

???  Not the OP's info in #1.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.