Multiple serial communication in atmega

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

Hi to everyone
I want to do multiple serial communication in atmega 328 like i am connecting my atmega chip with Bluetooth module and laptop.
And data i am sending to Bluetooth from phone wants that data to print on serial monitor

So now 1 want 2 (Rx -tx) paires one for Bluetooth and chip and another for laptop and chip .
But i am not bale to figure out how should i configure other pins for serial communication apart from default given pins (0,1) .
I google also but their no such material i get through which i can get any idea ...

So any suggestionss

I go through the software serial library of arduino to see how things are working their but i didn't understand how the things are working their .....

This topic has a solution.

Kunal Gupta

github.com/gkunalupta

Last Edited: Tue. Jan 7, 2020 - 06:00 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Mega328 only has one UART (unless you have a 328PB). So, do one serial port with UART, one with bit-bang. It is close to insanity to try to multiplex two channels into a single UART.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Mon. Jan 6, 2020 - 07:31 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Atmega 328

Kunal Gupta

github.com/gkunalupta

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

Sorry, see rewritten message.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

Sry it is atmega328P

Kunal Gupta

github.com/gkunalupta

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

Kunalgupta wrote:
Sry it is atmega328P

 

Same answer as #2.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

If  "doing multiple serial comms" is a key requirement of your project then, surely, you should be choosing a processor to suit that key requirement - ie, one with sufficient UARTs ?!

 

If you really must do this on a processor without enough UARTs of its own, options include:

 

  1. "Bit-bang" the extra UARTs - as Jim suggested in #1;
  2. Add external hardware UARTs; eg, multiple UART-to-SPI and UART-to-I2C chips are available;
  3. Add an extra microcontroller (or microcontrollers);
  4. Multiplex the UART

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
options include  ... Multiplex the UART

ka7ehk wrote:
It is close to insanity to try to multiplex two channels into a single UART

I do concur!

 

Just because it can be done doesn't mean that it's a sensible thing to do!

 

Again, the sensible thing is just to use a microcontroller with sufficient UARTs in the first place - it's not like they are rare or difficult to find these days ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sry again it is atmega 328PB ...ut has more than one uart pairs  i programmed it with arduino ide using but i want to do now same using embedded c and using registers of UART 

Kunal Gupta

github.com/gkunalupta

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

Kunalgupta wrote:

sry again it is atmega 328PB ...ut has more than one uart pairs  i programmed it with arduino ide using but i want to do now same using embedded c and using registers of UART 

That is all well documented in the datasheet. It even holds a bit of example code that you can use.

 

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

meslomp wrote:

 

That is all well documented in the datasheet. It even holds a bit of example code that you can use.

 

 

well in the datasheet i didnt found no such example for this multiple serial port .....

i found these two lines from datasheet which i thiught will be useful as according to these lines enabling receiver and transmitter simply overwrites the Rxd and Txd on those pins from their normal operation but still i did'nt get that how shouldi declare pins which i am going to use 

 

 

 

 

 

 

 

 

 

When the Receiver is enabled, the normal pin operation of the RxDn pin is overridden by the USART and given the function as the Receiver’s serial input

 

When the Transmitter is enabled, the normal port operation of the TxDn pin is overridden by the USART and given the function as the Transmitter’s serial output ​​​​​​​

Kunal Gupta

github.com/gkunalupta

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

Kunalgupta wrote:

...how shouldi declare pins which i am going to use 

 

You don't. The allocation of pins from the USART to the physical pins is fixed as shown in the datasheet. So you just enable the two USARTs and write, or read, from the respective registers.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

It means that the normal GPIO function is overridden - ie, they stop doing GPIO, and just do their UART functions.

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

Brian Fairchild wrote:
The allocation of pins from the USART to the physical pins is fixed as shown in the datasheet

Here:

 

See also Table 6-1, "PORT Function Multiplexing"  on page 18.

 

http://ww1.microchip.com/downloads/en/DeviceDoc/40001906C.pdf

 

You should also visit the Product Page:

 

https://www.microchip.com/wwwproducts/en/ATmega328PB

 

and take a look at the 'Documents' Tab - there's a whole load of Application Notes to help you with using the chip; eg,

 

AN_1451 - AVR306: Using the AVR UART in C on tinyAVR and megaAVR devices

 

AN_2557 - AVR303: SPI-UART Gateway on tinyAVR and megaAVR devices

 

 

 

EDIT

weird characters in Table reference!

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Mon. Jan 6, 2020 - 03:46 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

okay i get these ..thanks for helping 

 

 

but one thing in the software library of arduino IDE 

SoftwareSerial::SoftwareSerial(uint8_t rxPin, uint8_t txPin, bool inverse_logic /* = false */)
  {
  buffer_overflow = false;
  #if defined(__IMXRT1052__) || defined(__IMXRT1062__)
  if (rxPin == 0 && txPin == 1) {
  port = &Serial1;
  return;
  } else if (rxPin == 6 && txPin == 7) {
  port = &Serial2;
  return;
  } else if (rxPin == 14 && txPin == 15) {
  port = &Serial3;
  return;
  } else if (rxPin == 16 && txPin == 17) {
  port = &Serial4;
  return;
  } else if (rxPin == 21 && txPin == 20) {
  port = &Serial5;
  return;
  } else if (rxPin == 25 && txPin == 24) {
  port = &Serial6;
  return;
  } else if (rxPin == 28 && txPin == 29) {
  port = &Serial7;
  return;
  }
  #else
  if (rxPin == 0 && txPin == 1) {
  port = &Serial1;
  return;
  } else if (rxPin == 9 && txPin == 10) {
  port = &Serial2;
  return;
  } else if (rxPin == 7 && txPin == 8) {
  port = &Serial3;
  return;
  }

 

 

 

here they have used many other pins for serial communication and this is for boards other than atmega 328pb which support many pins for uart ... so here 

why we are defining pins 

Kunal Gupta

github.com/gkunalupta

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

Kunalgupta wrote:

... so here why we are defining pins 

 

Because that is a Software Serial library, ie one that uses pure software to make a USART rather than using the internal hardware. As such...

 

1) They do run run as fast as real hardware

2) They consume more CPU cycles

3) There are usually restriction on your use of interrupts

4) You cannot usually have a blocking function anywhere in your code

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

If using a 328pb, I would look at MCUdude/MiniCore, which has been set up for it (arduino/ArduinoCore-avr is not set up for the 328pb) .

 

SoftwareSerial should allow serial communication on any digital pin of the board (it was updated to use NewSoftSerial.cpp). I have not tried it, and it looks a lot different than what Arduino does, there is a lot of timing that is compiler version-specific.

 

https://github.com/MCUdude/MiniCore/search?q=SoftwareSerial&unscoped_q=SoftwareSerial

 

Hardware Serial is available when register defines are found during compile. This is more like what I would use.

 

https://github.com/MCUdude/MiniCore/blob/ed106a31f6565ab4000b93129eabfea37929d99f/avr/cores/MCUdude_corefiles/HardwareSerial.h#L142

 

I use a modification of a modified version derived from Peter Fleury, but it is not C++, so youngsters (<50) hate it.

 

https://github.com/epccs/Gravimetric/blob/master/Manager/lib/uart.c#L272

 

 

Update: second link was broken

Last Edited: Mon. Jan 6, 2020 - 09:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Brian Fairchild wrote:
a Software Serial library, ie one that uses pure software to make a USART

@ Kunalgupta  : That is what Jim meant by "bit bang" in #2

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Okay... I get it now

Kunal Gupta

github.com/gkunalupta

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

If your question is now resolved, see Tip #5 - in my signature, below (may not be visible on mobile)

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:

If your question is now resolved, see Tip #5 - in my signature, below (may not be visible on mobile)

 

thanks for telling me ...i have done for all my posts now

Kunal Gupta

github.com/gkunalupta