Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

5 microcontrollers communications using UART.(ATmega32)

Status
Not open for further replies.

manii120

Junior Member level 1
Joined
Sep 14, 2017
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Location
India
Activity points
121
5 microcontrollers communcation using UART.(ATmega32)

Hello friends,
I am trying to communicate 5 micros using UART with 1MHz freq and 2400 baud.
all 5 micros are ATmega32, the problem is there is an never end loop on going in main and i don't want to interrupt it while receiving and transmiting data. and it is working properly when i m doing it with 2 micros.
But now i need to go beyond this and i m using 5 micros. assume 5th micro is master (not defined in codes) sending and receiving data accurately through rest 4 controllers but when those 4 controllers try to transmit data it fails...nothing happens i don't understand why ??

4 micros receiving data and doing action according to received data but unable to transmit data to that 5th micro.
please help me.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

Give information about:
* bus topology
* hardware interface
* protocol including timing
* is bus arbitration done, if yes, how?
* is bus collision is recognized, if yes, how is it treated?

Klaus
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

1. Bus topology- 5th micro rx and tx connected to 4 micros rx,tx.
2. Using proteus free version with AVR studio.
3. 1MHz internal oscillator , 2400 BAUD,
4. Bus arbitration is the thing i want to know (How to do that).
5. I don't understand by what do you mean Bus collision.
Please consider me as Beginner because i am not good in technical terms and communication skills not that good.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

do you want to keep on some standard interfaces/protocols?
Or dou you want to design your own?

Klaus
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

I assume he is trying to work with uart with pooling method, not interrupt.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Well it's good to know about standard interface's and work on them before making my own
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

What does that pooling means.....I am trying to use UART to transmit and receive data without delay.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

You are using the Arduino approach, that is: Allways check on main() loop for incoming bytes. What Easyrider83 referred is that you could enable UART interrupt to get received byte, running the "Serial read" routine just once. Each new byte at UART receiver register triggers interrupt event.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Actually I am doing something different like whenever button pressed ( assume it interrupt) timer's check if there is any data received or not, if yes then see what received and take action accordingly. If not received then recheck it again and again.

- - - Updated - - -

This is my approach for UART.

unsigned char UBRR_value=25;
UBRRH=(unsigned)UBRR_VALUE>>8;
UBRRL=UBRR_value;
UCSRB=1<<RXEN|1<<TXEN;
UCSRC=1<<USBS|3<<UCSZ0;
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

whenever button pressed ( assume it interrupt) timer's check if there is any data received or not

Instead of guessing, how about posting here a snippet of the code ?

- - - Updated - - -

At the above code there is nothing related to how are you getting data from UART.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

For receiving I'm using this code.

If(!(UCSRA&(1<<RXC)))
{
received_data=UDR;
Func(received_data);
}

Where as for transmitting I'm using this->

While(!(UCSRA&(1<<UDRE)));
UDR=DATA_TO_SEND;
 
Last edited by a moderator:

Re: 5 microcontrollers communcation using UART.(ATmega32)

You are not using interruption either for receiving or for serial transmission. In this way, it is predictable that data will be lost. Your routine will be stuck in the while instruction while nothing is received by the UART
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

I see only a couple of code lines.
I can't see if all this in main loop, if it is in an ISR, how often these lines are called...

We need to get useful informations.

Btw: "pooling" is a typo, it should be "polling".
For software this means a cyclic "asking" if data is available .... in opposite to a interrupt method, where an event is triggered by data reception.

Klaus
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Pardon for wrong code for receiving.
HTML:
        if((UCSRA&(1<<RXC)))
	  {
	   received_data=UDR;
	   decide_func(received_data);
	  }

And its working properly.

- - - Updated - - -

Well only the receiving part is in ISR.

The codes i'm using for receiving and ISR is ->

Code:
ISR (TIMER1_COMPA_vect)    // Timer1 ISR
    {
   if(PINA!=0x1f)                  //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
      {
	 val=PINA;                  
	 button(val);              //passing PINA value  
      }	
	  if((UCSRA&(1<<RXC)))                 //if recevied anytime
	  {
	   received_data=UDR;                  //store the value received
	   decide_func(received_data);      //Passing received value.
	  }
    }

And this is how i set-up my timer1 for CTC mode
Code:
   TCCR1B |= (1 << WGM12)|(1 << CS11)|(1 << CS10);
   TCNT1 = 0;
   OCR1A = 2499;                                                           //I just set this value because it works fine not calculated(please help me here how to 
                                                                                    //determine the value to be set in here.
   TIMSK |= (1 << OCIE1A);
Kindly help me in improving what to say that in technical terms if i were wrong somewhere(So next time i'll be more accurate).
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

Code:
   if([U]PINA[/U]!=0x1f)                  //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
      {
	 val=[U]PINA[/U];
This can be problematic in some cases, because you read the PINA value two times..
So there is a chance that the port value changes between those two points of time.
--> I recommend to read the port value a single time per run:
Code:
  [U] val=PINA  // single read of port value, [/U]
   if(val!=0x1f)                  //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
      {
	 other code;

Please give additional information about timing: How often per second is your code be executed.
Include this information as comment in your code.

I can't imagine why reading the UART is controlled by a key press. Does it make sense? In most cases the UART can receive data all the time. In your case - when no key is pressed - there will be data loss.

Usually the UART receive is jnterrupt driven. But not by a timer_interrupt, but by the UART_interrupt. This interrupt is only raised when the UART receives data. Then within the ISR the received data is read and placed in a software_receive_buffer, a received_flag may be set, and then the ISR is finished.
ISRs in general should be very short in time. Do only the very imortant steps in an ISR. Process the received data (from buffer) in the main loop.

Klaus
 
Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

Code:
   if([U]PINA[/U]!=0x1f)                  //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
      {
	 val=[U]PINA[/U];
This can be problematic in some cases, because you read the PINA value two times..
So there is a chance that the port value changes between those two points of time.
--> I recommend to read the port value a single time per run:
Code:
  [U] val=PINA  // single read of port value, [/U]
   if(val!=0x1f)                  //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
      {
	 other code;

Klaus

i'm lil bit confused here now. you are saying im reading PINA value two times ?? how??
there is a condition
if(any button pressed) // without any button press it won't let enter any one so how can it read value two times.
{
//store that pin number in this variable
//send this pin number to some function to work upon
}

but in your corrected code i.e.
val=PINA // single read of port value, [/U]
if(val!=0x1f) //this is a condition where if any button pressed(out of 5 buttons i am using) will be detected
{
other code;
val receive PINA value in every 2499 ticks as because i m using CTC so whenever OCR1A value reaches 2499 it will re check ISR and give val its value.
which means this will become more complicated then we think....if i am wrong please correct me.
Thanks.

- - - Updated - - -

Hi,

I can't imagine why reading the UART is controlled by a key press. Does it make sense? In most cases the UART can receive data all the time. In your case - when no key is pressed - there will be data loss.

Usually the UART receive is jnterrupt driven. But not by a timer_interrupt, but by the UART_interrupt. This interrupt is only raised when the UART receives data. Then within the ISR the received data is read and placed in a software_receive_buffer, a received_flag may be set, and then the ISR is finished.
ISRs in general should be very short in time. Do only the very imortant steps in an ISR. Process the received data (from buffer) in the main loop.

Klaus

And here yes you are rihgt i think i miss understood something and using this timer interrupt with UART.
But 1 question is coming in my mind , That is if my main while loop is busy with some stuff and i ain't wanna interrupt at any cost.
so when i use that UART interrupt for receiving and transmiting in main while loop does my running cycle interrupted or it goes on without any disturbance.

If it can happen then please tell what to do and what to change in my UART setup.
Code:
 unsigned int UBRR_value=25;
 UBRRH=(unsigned)(UBRR_value>>8);
 UBRRL=(unsigned)(UBRR_value);

 UCSRB=1<<RXEN|1<<TXEN;
 UCSRC=1<<USBS|3<<UCSZ0;

- - - Updated - - -

USART Control and Status
Register B – UCSRB
• Bit 7 – RXCIE: RX Complete Interrupt Enable
Writing this bit to one enables interrupt on the RXC Flag. A USART Receive Complete
Interrupt will be generated only if the RXCIE bit is written to one, the Global Interrupt
Flag in SREG is written to one and the RXC bit in UCSRA is set.

• Bit 6 – TXCIE: TX Complete Interrupt Enable
Writing this bit to one enables interrupt on the TXC Flag. A USART Transmit Complete
Interrupt will be generated only if the TXCIE bit is written to one, the Global Interrupt
Flag in SREG is written to one and the TXC bit in UCSRA is set.
• Bit 5 – UDRIE: USART Data Register Empty Interrupt Enable
Writing this bit to one enables interrupt on the UDRE Flag. A Data Register Empty Interrupt
will be generated only if the UDRIE bit is written to one, the Global Interrupt Flag in
SREG is written to one and the UDRE bit in UCSRA is set.
• Bit 4 – RXEN: Receiver Enable
Writing this bit to one enables the USART Receiver. The Receiver will override normal
port operation for the RxD pin when enabled. Disabling the Receiver will flush the
receive buffer invalidating the FE, DOR, and PE Flags.
• Bit 3 – TXEN: Transmitter Enable
Writing this bit to one enables the USART Transmitter. The Transmitter will override normal
port operation for the TxD pin when enabled. The disabling of the Transmitter
(writing TXEN to zero) will not become effective until ongoing and pending transmissions
are completed, i.e., when the transmit Shift Register and transmit Buffer Register

This is what my data sheet says. But i dont understand how to use UART interrupt i mean how to enable interrupt for UART and second thing is there is RXCIE which for complete interrupt enable .

above in bold and underlined statment is a riddle for me. What does that mean....am i suppose it as after completing receiving part it will enable interrupt of yes received done now you can done another thing. is this what that says ?
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

PINA:
* the "IF" command needs to read the PINA
* and the "val = PINA" reads the PINA
...at different times. In this case it may cause no problems... but with another program it may cause problems.
Therefore it is good habit to go the safe way.

***
val receive PINA value in every 2499 ticks as because...
As far as I can see "val" isn´t used elsewhere, therefore the "safe way" should have no influence. --> not more complicated.

***
Interrupts....and busy main loop:
They surely interrupt your main loop. That´s why they are called "interrupt".
But as alredy said: The ISR should be as short as possible in time. You won´t recoginze the interrupt if the ISR takes just 10us.
And with the UART_interrupt .. then main loop is interrupted just once per UART byte recieve...but a timer_interrupt will interrupt the main loop in any case, even when no data is received.
Calculation: with UART full load at 2400 Baud and 10us ISR .. the remaining processor power for the main loop is still 99.76%.

Interrupts may be disabled. But don´t do this out of a mood. Do this only when really really urgent, like with atomic multi byte read/write. Re-enable the interrupts as soon as possible.

***
UART interrupt enable:
How to enable interrupt is clearly written. The bold text.
I assume you misinterpre the "complete" word. It is not "interrupt_complete" but "RX_complete" = receive_complete.
Transmitting one byte with 2400 baude takes 10 bittimes x 1/2400s = wich is about 4.2ms. "recive_complete" just means the interrupt is not raised at the beginning of data receive, but at the end, when all 10 bits are recieved. Then you immediately can read and process the received byte.

If you "ENABLE" an interrupt you just allow that an ISR is called. This doesn´t mean the interrupt is raised. If there is no data traffic on the UART..the interrupt never is raised an the ISR will never be called.

****
I have to say this are very basic programming and microcontroller understanding problems. A forum can´t replace a school. You need to learn such basic things on your own. There are countless documents, tutorials and even training videos in the internet. Use them.

Klaus
 
Re: 5 microcontrollers communcation using UART.(ATmega32)

I appreciate your help and work on what you said.
Actually I tried searching for stuff like UART interrupt but all is theory nothing related to codes and process
so that's y I need to ask you about the same. But my apologize I'll try not to ask these basic questions again.

One thing I need to know, how to set 10us for ISR. I already tried but confused in that ...hope you don't mind asking you this...please share where to read about this.
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Hi,

Actually I tried searching for stuff like UART interrupt but all is theory nothing related to codes and process
.. don´t tell stories!

The first place where you should look for that stuff is the manufacturer´s homepage.
--> AVR306 + free code exapmples for download.
http://www.atmel.com/search.aspx?fi...=*&exclude_apps=1&q=avr306&site=en_collection

Additinally there may be hundreds or thousands of examples in the internet.

*********
One thing I need to know, how to set 10us for ISR
You really need to read basics!
You don´t need to set an ISR for 10us or any other time.

The ISR = InterruptServiceRoutine is code.
The more code you write in this routine (or better say: the more code has to be processed) the longer it takes.
The ISR is called when a byte at the UART is recieved....and only then.
As already written:
The code of the ISR just reads the received byte from the UART and stores it in a software buffer, then it leaves the ISR.. (and goes on with the main loop). this code may take 10us, maybe a bit shorter, maybe a bit more .. it depends on the code and the processing speed.

Klaus
 

Re: 5 microcontrollers communcation using UART.(ATmega32)

Ok thank you....
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top