ICCAVR, ATMega32, RS-485 & MPCM help

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

Questions regarding Atmega32 use of Multiprocessor (MPCM) and RS-485
Using ICCAVR C compiler with Olimex ATMega32 board.

I understand from the manual the following setup conditions for multiprocessor comm using the UART
Serial port in an RS-485 environment. It is convenient to use the acronym MPCM hereafter.

To set up the Master:
The 9 bit char frame format is set by setting the UCSZ bits to 111 (7). This is done via two separate registers, UCSRB bit 2 and UCSRC bits 1 & 2.
When transmitting the slave address, TXB8 must be set and set prior to writing to UDR.
I understand this to be the only requirements to set the MPCM mode?

To set up the Slave:
The MPCM bit set in UCSRA (bit 0). I think this allows the slave to only look for the 9 bit data?
Also, I think it is better to use the UART RX interrupt to examine the UDR data. Looking at a sample code on the AVR Freaks, it looks like the RX interrupt will occur regardless of the setting of MPCM?
In that case, the slave firmware would then examine the UDR contents and determine if the address is for that
Slave. Question: Does the slave need to also verify that RXB8 bit is set as well?
If it not the addressed slave, then nothing else happens at the slave. If the address is a match, then the slave
will disable the MPCM bit and this is what allows the slave to receive data , 8 bit data as is usually normal.
At this point, the Master must also change the UCSZ bit(s) to reflect the normal 8 bit data format before any data
can be sent to the slave?

In using the ICCAVR C compiler, it looks like I have to select the 9 bit set when using the application builder that I have come to appreciate.

The business of using the UCRSC URSEL bit, per the manual, seems a little bit confusing and I am not sure
If the ICCAVR compiler knows which register you want to use.

I am testing the MPCM mode(s) out just using a simple RS-232 between two ATMega32 devices. The firmware and UART should not care how the data arrives.

I did a search on the Freaks forum and did find an example code, but it leaves some of the above mentioned details
Out of the listing. It also seems targeted more to the receive/slave side, but did offer a great deal more understanding on my part.

As you may see, I am still fumbling with getting this going and would greatly appreciate any help, suggestions and example codes. Thanks in advance.

Also, thanks to theusch for his example code I mentioned above.

I'll believe corporations
are people when Texas executes one.

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

I'm almost with you. I have the latest iccavr, and I've done rs485, but not 9 bit mode. Lee Theusch uses 9 bit mode, but he uses CV compiler. Maybe get AVR to AVR 485 working using 8 bit... main trick is you cant turn the 485 chip tx back to rx UNTIL THE TXE BIT IS SET... (Transmit Empty). The TDRE bit (Transmit Data Register Empty) sets one char time before that. Also, you need to write a 1 to the TXE bit to clear it. One trick might be to make opcodes start with 0x80 (gives you 128 of them) and restrict data to 0-0x7f. You have to do tricks like send words broken up into hunks and reassembled. This works ok for 10 bit a/d values... send 3 bits in one byte, 7 bits in the next for example... Depending on the type of data, this might be more work than getting 9 bit mode to work! I use the BB electronics 232 to 485 dongle on the pc... it sets the pc to tx on every 1 bit... works great, you can get the AVR talking 485 to the pc easy like this

Imagecraft compiler user

Last Edited: Sun. Apr 29, 2007 - 08:57 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bob, I want to use the 9 bit mode so I can have a multidrop setup. Clearing the TXE bit, doesn't the putchar() or printf take care of that? I did not thnk I had to do that when using the C compiler.
When you have done RS-485, were you using more than one slave?
I thought I would get the MPCM multiprocessor business working by just using serial (8 bit) to serial which is not a problem. But, I think I can still use regular RS-232 between two boards to test the 9 bit comm's.
Thanks. I sent a PM to theusch regarding his posted code but have not received anything back yet.

I'll believe corporations
are people when Texas executes one.

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

I think 8 bit and multi nodes will work... here's my scheme... every node has an address... packets are always opcode,address,data.... before a node can transmit, he needs to see an idle line... then he delays hisaddress char times... its simpler to have a master/slave type of setup where master says 'node 3... send your pot values!' and node 3 says 'hey! hes talking to me!' That way the collision avoidance is handled by the protocol.

The 'stock' putchar is unfortunately just wait for the TXDRE bit, stuff char into UDR. Doesnt know about setting 485 tx enable, or wait for TXE bit set, or clearing the bit. Thats what us programmers are for!

Imagecraft compiler user

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

Oh yea, I know I will have to have the means to set the transmit enable.disable for the 485 device to work, especially using the half duplex, two wire setup.
I will give some thought into your scheme, bit I would really like to get a handle on the 9 bit mode. If my memory serves me, I thought the 8051 handled a lot of the 9 bit mode(s) in hardware. Not sure anymore , old age :-)

I'll believe corporations
are people when Texas executes one.

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

Bobgardner: I noticed that if I use printf, then the TXE and the other control bits, is handled by the function, or at least by observing using debug (dragon).

So, I guess, if I use putchar , then I will have to handle those bits. Don't want to use printf, except for testing.

I'll believe corporations
are people when Texas executes one.

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

printf just calls putchar. If you write a putchar that talks 485, Bob's Your Uncle. I have a 'file descriptor' variable. 0 is uart 1 is the other uart 2 is the lcd 3 could be something else

Imagecraft compiler user

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

"Bob's your uncle" , huh?

I'll believe corporations
are people when Texas executes one.

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

My Buddy Timmy is a Blues Man.... when he gets it workin, he says he's Golden....

Imagecraft compiler user

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

My Buddy Timmy is a Blues Man.... when he gets it workin, he says he's Golden....

Did a goggle search, no computee

I'll believe corporations
are people when Texas executes one.

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

I was searching for my goggles the other day... wanted to go swimming underwater... Oh the BYU thing? Its Brit slang for Chancellor of the Exchequer that hired all his relatives for a Govt Job.

Imagecraft compiler user

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

If you want multidrop scheme, why you don't use ModBus? Quite common protocol that many people had used over RS485 (including myself, with ImageCraft and ATmega64/128).

Guillem.

Guillem.
"Common sense is the least common of the senses" Anonymous.

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

Guille: ModBus may be for another project. RIght now I am interested in using multidrop based on 9 bit format.
I think that in the future, I would like to use ModBus for PLC to micro interface. The CRC calculations look intimidating :-)
I was hoping for this post, to get some in depth 9 bit information. I did find post from theusch but it was more about the receive(slave) end of the interface. I am still getting the transmit set up. One of the questions involves the UCSZ settings after the Master has sent a address frame. Do you have to change the UCSZ bits at the Master in order to change to 8 bit mode?
Well, I am looking forward to suggestions/comments on 9 bit mode.

I'll believe corporations
are people when Texas executes one.

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

Quote:

I am still getting the transmit set up.

I do it very simply. When I know I'm going to send a poll string with the address as the first byte, I just tick the TXB8 bit for that character (I know that putchar doesn't fuss with it). Crude but effective, 'cause I did the slave(s) too and know that they never speak unless spoken to.

// Do the address byte first, with the 9th bit set
			scratch = frame_command[0];
			frame_checksum = scratch;
			UCSR1B |= (1<<TXB81);		// mark the first byte as an address -- MPCM
			putchar1 (scratch);			// send off the character, including the 9th bit on
			UCSR1B &= ~(1 << TXB81);	// then clear for subsequent characters

// ... then the rest of the frame
			for (looper = 1; looper < frame_index; looper++)
				{
				scratch = frame_command[looper];
				frame_checksum += scratch;
				putchar1 (scratch);		// send off the character
				}
			// Send off the checksum
			putchar1 (~frame_checksum);
			ticks_commo = TICKS_POLL;	// start the timeout tick countdown
			}

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: If each poll of the slave(s) only requires a response from the slave, is it required to send an additional "command" byte to the slave. At this point, I only need the slave to know it is addressed, then it should send it's data, no other slave functions at this time.
Also, in setting up the transmit, I have set the UCSZ bits to 111 in the appropriate registers.
If I need to send data format, then I have to change the UCSZ bits to select 8 bits?

I looked at a older post you had made and the receive code did explain a lot about the receive side , so much thanks for your post there.

I will keep plugging away, it has got to work sooner or later :-)

I'll believe corporations
are people when Texas executes one.

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

Quote:

Also, in setting up the transmit, I have set the UCSZ bits to 111 in the appropriate registers.
If I need to send data format, then I have to change the UCSZ bits to select 8 bits?

Are you talking about the master? It is set for 9-bit characters, as are the slaves. I see I had to add some BS to make it work. ;) Once set, the only bit I fuss with is the mentioned TXB8.

// USART1 initialization (I/O module communication)
// Communication Parameters: 9 Data, 1 Stop, No Parity
// USART1 Receiver: On
// USART1 Transmitter: On
// USART1 Mode: Asynchronous
// USART1 Baud rate: tbd
UCSR1A=0x00;
UCSR1B=0xDC;
//UCSR1B = BIT_UCSRB_RXCIE |
//		BIT_UCSRB_TXCIE |
//		BIT_UCSRB_RXEN |
//		BIT_UCSRB_TXEN |
//		BIT_UCSRB_UCSZ2;
UCSR1C=0x0e; // USBS, UCSZ1, UCSZ2 -- two stop bits, 9-bit characters with UCSZ2 of UCSRb.

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

So, you keep the 9 bit frame and don't switch between 9 and 8 bits. Reading the manual, it seems to imply that you would send the address then switch to a data frame format......
I will look closer at your attached code snippit.
Thanks theusch

I'll believe corporations
are people when Texas executes one.

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

It has been several years since I put it together, and puzzled over the seemingly-simple datasheet page myself.

If you think about it, it makes sense for any "general" protocol--the slaves need to expect the same character frame every time. I suppose you >>could<< fuss with the character frame sizes continually, but why bother? [An extra bit time each character doesn't bother me, and the slave has no per-character processing unless actually pertinent.]

So, it boiled down to the master has a fixed setup and only two lines of code for MPCM every pass: Turn on TXB8 for the address, and turn it off after.

Lee

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

OK Lee, I agree, the manual doesn't cover the subject in any great detail. I am using a simple 232-232 between two Atmel AVR's. I should be able to test as if it were 485, without the line turn around ofcourse.

As I understand the use of the MPCM bit, it only applies to the Slave/receive? Then the master sets the TXB8 bit for address frame and then clears that bit for data.

Also, at this point I only need the slave to recognize
it is addressed and then it shuold send it's data. In this case, all I am trying to do is to get the slave, when addressed, to send the value of a port.
That seems to say that once the master transmits the address along with the TXB8 set, then it can just wait for the slave to send it's data back. In both the master and slave, I am planning on using the UART rx interrupt to receive address or data. Address for the slave and data back to the master.

Thanks for the input.

I'll believe corporations
are people when Texas executes one.

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

Well I dug in some more. What I think I have found is:
Both the slave and the master are set for 9 character bits.
The slave must have the MPCM bit to recognize an address frame.

In testing, I added a port pin input to select between TXB8 set or cleared.(On the master)
This worked as expected. If the TXB8 was not set, the slave did not respond.
If the TXB8 was set, then the slave responded as expected.
Also did a similar test on the status of MPCM in the slave.

What seems confusing to me is that I would think that you would want to switch from 9 bit mode when
sending a address frame, to the 8 bit mode when sending/receiving data from the selected slave.

I think the extra bit just looks like additional stop bits?

I can post the code for the receiver/transmitter if someone would like to critique it.
It is a simple setup where the master polls the slave every so many milliseconds and then receives
the value of the slaves PORTA and then outputs that data on the Master Port.
I know that I have to add in control for the RS-485 devices (when I get them).
If there are any additional information or comments I would appreciate it.
Thanks for the assistance.

I'll believe corporations
are people when Texas executes one.

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

Quote:
to the 8 bit mode when sending/receiving data from the selected slave.
NOOOO the whole thing uses 9 bits ALL THE TIME. The first packet byte which is the destination address has bit 8 set so that the receiver(s) will get a RX int when the HARDWARE sees bit 8 set, then the receiver checks if the packet is addressed to it (check for an address match) if it is it will accepts all further bytes that have bit 8 CLEAR (data bytes), if not it can go to sleep or on holidays until another byte comes in with bit 8 set. In other words bytes with bit 8 clear DO NOT trigger a RX int in MPCM mode, you need to turn it off in the receiver if you have a good address match, once the packet has come in you will need to turn it back one again.
I have used MPCM for many years with the HC11, HC05, (gulp) PIC and AVR, all these use the same set up.

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

OK, it is now clear that you have to leave the 9 bit character mode set up on BOTH the master and the slaves.
I hope all of this helps anyone else struggling with the MPCM mode for multiprocessor comm's.
Now, the fun begins as I wait for the 485 driver/receiver chips to get here. I thought of using the B&B electronics or the RE Smith (www.rs485.com)modules but will try and roll my own.
This is pretty much the end of this thread for me, thanks to the responders for your help and advise.

I'll believe corporations
are people when Texas executes one.

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

One last thing to keep in mind is that the TXC flag become very important in your code as you MUST NOT change the 9th bit or change the direction of the RS485 chip untill this flags is set or you will lose part of your data and subsequently get stunning headaches and suicidal tendencies...:)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

I just use TXC all the time, even though not politically correct.

Lee

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

John: Just got through headaches!
That is an important note to keep in mind. My test setup is just two boards tied rs-232 together to verify how the 9 bit stuff works. So far I am just using the fact that the slave 'see's' its address and then sends the status of PORTA to the master. Using this scheme, with only one slave action, I think I don't have to change anything?
However, when I start to expand, then I will need to switch between data and address. Lee mentioned that he only has to change the TXB8 bit, this seems to be the case. This turned out to be more complicated than I originally thought, but think I am getting the feel for it now.

I'll wait for the suicidal tendencies when I start attempting to get all of this together and working with the two wire rs-485 hardware.

BTW I had a problem accessing your web site listed with your post. I got homepages.chili.net.au which must be your Internet hosting location.
Thanks John

I'll believe corporations
are people when Texas executes one.

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

Quote:
I had a problem accessing your web site listed with your post
Me too :? the idiots at my ISP seem to turn everything off once I go over my 400Mb quota which I did yesterday. However today is the 1st day of the month and a new 500Mb quota and still not working. In the next week or so I hope to have ADSL2 FINALLY!! so I can dump them, I'll get ~6 times the data (3Gb + 6Bg offpeak) and at several times the data rate..or so I hope...I signed up a month ago and still today they are messing around with changes to the phone line..I hope not a sign of their brodaband speed :( :(

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

hi friends,

i am doing one project in that i want to communicate to pc with Rs232 . here from controller(Atmega32) to max485 then rs485/rs232 converter .we can see our tx value in hyper terminal window .

i wrote the code for usart init and transmit but its not working , why ?

void USART_Init()
{
/* Set baud rate */
int UBBR;
//UBRRH = (unsigned char)(baud>>8);
//UBRRL = (unsigned char)baud;

UBRR=(fosc/(16*baud))-1;

/* Enable receiver and transmitter */
UCSRB = (1<<RXEN)|(1<<TXEN);
/* Set frame format: 8data, 1 stop bit */
UCSRC = (1<<URSEL)|(3<<UCSZ0); /*BV(URSEL)|_BV(UCSZ1)|_BV(UCSZ0);*/

}
16mhz crystal,its not talikng the value of UBBR ITS SHOWING ERROR AND CAN U TELL ME THIS INITIALISATION IS CORRECT OR WRONG ?