Baby Question: How do I find out which ports correspond to which on a datasheet?

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

I haven't had the classical way of learning avr and as such I may have missed some beginner course that explained this, and I've honestly beat myself up over whether I should post this or not. However I am stuck. I need help.

 

I have been given an atmega2560 and a breadboard with 3 LEDs plugged in. I need to light them up in sequence as part of a much larger task but I am stuck on this tiny baby step. I believe I have a fundamental misunderstanding of ports but my google-fu and research has confused me further.

 

Here are my assumptions (I call it assumptions because I am not sure if I am correct on any of this):

 

This is the atmega 2560 datasheet: http://www.atmel.com/Images/Atme...

 

I believe that the relevant diagram that lists the ports is on page 2 but I don't know how that abstraction connects to the actual atmega 2560 (i.e. they look different). The most detailed pinout diagram I could find for the atmega2560 is http://www.instructables.com/fil...

 

In order to light up each LED, I need to target each specific port that they are connected to and set a PORTxn |= (1 << PBx) or something to that effect?

 

However in my research I found tutorials like this that explain how to interface with LEDs, however they don't really explain WHY PORTC is where the LEDs are connected. What is the normal way of lighting up an LED?

 

I realise that I simply don't know how to read the data sheet but my efforts in learning how to have come up short. I hope I've described my problem in detail and demonstrated my efforts to solve it.

 

 

 

This topic has a solution.
Last Edited: Wed. Dec 20, 2017 - 05:32 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ThePageMan wrote:
I believe that the relevant diagram that lists the ports is on page 2

Agreed a good place to start.

ThePageMan wrote:
I don't know how that abstraction connects to the actual atmega 2560 (i.e. they look different).

...but that statement confuses me.  Yes, the datasheet has a diagram and not a photograph.  But what in particular "looks different"? 

 

As that model is a hundred-pinner with small pitch, I'd think you are working with the chip mounted to a dev board of some type, such as an Arduino flavour.  Generally that type of board will have many/all of the port pins broungt out to connectors.  Wnad will have chematics available.

 

 

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

Your ATmega2560 basically looks like one of the figures 1-1 to 1-3 in the data sheet. A small flat square black plastic "box" less than or just over 10 millimeters to the side. If that does not sound familiar, then you either haven't got an ATmega2560, or you might have it sitting on a larger PCB.

 

Could it be that you have an Arduino Mega 2560 board

 

If so then the documenation of that board will give the mappings from the ATmega 2560 pinout to the headers on the board. Read through the complete page I just linked to!

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Dec 19, 2017 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ach! Yes I have a board. I got so used to saying ATmega 2560 and leaving out the "board" part that I didn't connect the dots and realise the data sheet is JUST for the ATmega. I took a look at the link and this schematic seems to be the answer but it appears to have more ports than are actually on the board (specifically looking at the right side)? 

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

If you have this:

The numbers identified by the red arrows shown on the board above correspond to

the numbers from the schematic shown below:

 

David (aka frog_jr)

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

ThePageMan wrote:
Ach! Yes I have a board.

 

Good. But can we confirm exactly what board it is?

 

Is it the one I linked to above?

 

Or is it some other board? If so, does it have any markings? Can you take a photo and post here?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Tue. Dec 19, 2017 - 08:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ThePageMan wrote:

Ach! Yes I have a board. I got so used to saying ATmega 2560 and leaving out the "board" part that I didn't connect the dots and realise the data sheet is JUST for the ATmega. I took a look at the link and this schematic seems to be the answer but it appears to have more ports than are actually on the board (specifically looking at the right side)? 

Concentrate on the pinouts of the ATMega2560 on the left side of the schematic and how they map to the Arduino Mega 2560 board pins.  The right side of the schematic has an ATMega32U2 that is used mostly for USB connection to allow programming via a bootloader.  Also, in the future when you refer to this "board" please use the name of the board shown in bolded text as it will make it much easier for us to help answer your questions knowing what "board" you are using.

 

If you need more specific help with this don't hesitate to ask.

"I may make you feel but I can't make you think" - Jethro Tull - Thick As A Brick

"void transmigratus(void) {transmigratus();} // recursio infinitus" - larryvc

"It's much more practical to rely on the processing powers of the real debugger, i.e. the one between the keyboard and chair." - JW wek3

"When you arise in the morning think of what a privilege it is to be alive: to breathe, to think, to enjoy, to love." -  Marcus Aurelius

Last Edited: Tue. Dec 19, 2017 - 07:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I'm going to assume you have an Arduino Mega. This picture shows the pin correspondences you need.

http://www.pighixxx.com/test/wp-...

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

Frog_jr: Ah thanks! That helps

 

JohanEkdahl wrote:

ThePageMan wrote:
Ach! Yes I have a board.

 

Good. But can we confirm exactly what board it is?

 

Is it the one I linked to above?

 

Or is it some other board? If so, does it have any markings? Can you take a photo and post here?

 

Here is a photo: 

 

larryvc wrote:

Concentrate on the pinouts of the ATMega2560 on the left side of the schematic and how they map to the Arduino Mega 2560 board pins.  The right side of the schematic has an ATMega32U2 that is used mostly for USB connection to allow programming via a bootloader.  Also, in the future when you refer to this "board" please use the name of the board shown in bolded text as it will make it much easier for us to help answer your questions knowing what "board" you are using.

 

If you need more specific help with this don't hesitate to ask.

 

I should have specified, I meant the right side of the left schematic. And thanks for the tip, I'll refer to it correctly in the future.

 

El Tangas wrote:

I'm going to assume you have an Arduino Mega. This picture shows the pin correspondences you need.

http://www.pighixxx.com/test/wp-...

 

Yeah so I refer back to my OP: "In order to light up each LED, I need to target each specific port that they are connected to and set a PORTxn |= (1 << PBx) or something to that effect?". If I connect my LEDs to PWM 2-6, which ports do I use? How does one read a sheet like that?

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

this should answer the question:

https://www.arduino.cc/en/Hackin...

 

Arduino digital pin 2 is AVR mega2560 port PE4. Thus the code would be PORTE |=(1<<4); to set the bit or PORTE &= ~(1<<4); to clear it.

 

According to the table you'll see the allocations aren't in any particular order in regarding to ports and bits. Such is the magic of Arduino that hides the evil from you. Also note you'll need to set the ports as outputs:

eg: DDRE |= (1<<4);

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

The thing you need at that point is an Arduino data sheet, or one of the many helpful charts people have made. You can just directly write to ports using the arduino library: digitalWrite(2, HIGH); to turn on the LED on port 2. (Don't forget resistors!)

 

As it happens, 2-6 are PORTD, pins 2 through 6. EDIT: No they're not. So also read the diagrams carefully.

 

More edit: Okay I've found two different pinout descriptions for the 2560, one of which says that they're D2-D6, which is what I'd have expected, and one which has a completely different pinout for them. I have no idea.

 

https://www.arduino.cc/en/Hackin...

 

seems to suggest that the completely different pinout is probably right.

Last Edited: Wed. Dec 20, 2017 - 02:08 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From the schematic in JohanEkdahl's post #3 you can see that

the connection to the Arduino Pin2 goes to the ATmega2560 pin 6 which is PE4

Thus:

ArduinoPin#  ATmegaPin  PORTPIN

   2             6      PORTE4

   3             7      PORTE5

   4             1      PORTG5

   5             5      PORTE3

   6            15      PORTH3

 

Just follow the signal names to their source...  

David (aka frog_jr)

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

ThePageMan wrote:
it appears to have more ports than are actually on the board

Yes. The ATmega2560 has 86 I/O pins (distributed over 10 8-bit ports and done 6-bit port).

Not all of these I/O lines are routed out to a header on the Arduino Mega 2560 board. (See the lines going out from the ATmega2560 "ending nowhere" - e.g. PC0..PC7?)

 

ThePageMan wrote:
However in my research I found tutorials like this that explain how to interface with LEDs, however they don't really explain WHY PORTC is where the LEDs are connected.

That is one badly written tutorial: First figure shows (correctly) how to limit current by having a resistor in series with the LED. Second figure is a schematic with eight LEDs connected to an AVR - without current-limiting resistors. LEDs are on port A. Code example shows how to drive LED connected to port C. If video shows an ATmega8515 then the LEDs there are on port C.

 

Please don't "fry" your AVR - do use current limiting resistors!

 

The choice of  what pins to use depends on several things:

- Since special functions (UART, timer/counter output/input, AD converter...) are on specific pins, the need for some of those affects what pins can be used for "plain digital I/O".

- Layout of PCBs might influence what pins are used

- In some cases "power budget" might influence what pins are used

and more...

 

My guess is that they picked port C quite arbitrarily. (They could just as well have used port B or A or...)

 

ThePageMan wrote:
In order to light up each LED, I need to target each specific port that they are connected to and set a PORTxn |= (1 << PBx) or something to that effect?

Close.. It would be

PORTx |= (1 << Pxn);

e.g.

PORTC |= (1 << PC4);

but the symbol PC4 actually equates the simple value 4 so little is gained by using the symbol (and it makes the code less receptive for moving to another port) so many of us choose to actually do

PORTC |= (1 << 4);

Assuming you've wired up the LED from the port pin to ground (again, with a current-limiting resistor) that should light up the LED. To turn it off again you'd

PORTC &= ~(1 << 4);

For a detailed and structured explanation of those two "manouvres" go over to the Tutorials sub-forum and locate a tutorial on "bit manipulation". Mandatory reading for anyone programming microcontrollers in the C programming language.

 

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

ThePageMan wrote:

 

El Tangas wrote:

I'm going to assume you have an Arduino Mega. This picture shows the pin correspondences you need.

http://www.pighixxx.com/test/wp-...

 

Yeah so I refer back to my OP: "In order to light up each LED, I need to target each specific port that they are connected to and set a PORTxn |= (1 << PBx) or something to that effect?". If I connect my LEDs to PWM 2-6, which ports do I use? How does one read a sheet like that?

 

That's in the diagram I linked... Let's zoom in:

 

 

Now read the posts carefully, they explain how you set the pins.

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

On further study: I read "D2" as "PD2" and was wrong.

 

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

I've never seen a data sheet, but from observation, a baby only has three ports: a bidirectional port at the top, and two output ports at the bottom end.

Four legs good, two legs bad, three legs stable.

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

the_real_seebs wrote:
On further study: I read "D2" as "PD2" and was wrong.

 

In Arduino speak, D2 or Digital pin2 is connected to micro pin PE4 or Port E pin 4 (from the diagram in #14)

So to change D2 you write to port E pin 4. 

 

Please let us know if you are using an Arduino sketch language to do your project or using the C/C++ language, as how you access port connector pins will be different.

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...

 

 

 

 

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

Oh, I don't know - there's also 2 video input ports, 2 audio input ports, and an olfactory input port.

 

The olfactory port does sometimes seem to enable a rather unpleasant output mode ...

 

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:
there's also
Don't overlook the audio output port(s)...

David (aka frog_jr)

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

frog_jr wrote:
Don't overlook the audio output port(s)...

John already covered that:

John_A_Brown wrote:
bidirectional port at the top

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

Holy cow! These responses have been incredibly helpful! Everyone who helped describe how to read the sheet and identify the ports, thank you! And thanks for the awesomely simple yet detailed explanation of each part Johan! One thing in particular that I had wondered was the difference between (1 << PC4) and (1 << 4). Also good to know I wasn't crazy in seeing them plug into PORTA but then activate PORTC in their code.

 

It's cool seeing all these names that I saw in loads of avrfreaks threads helping others, and now seeing them help me. I tinkered further, trying out a bunch of things and finally got a working system! But most importantly, I understand what all the lines of C do and what they are needed for! Here is my thanks to everyone here :)

Gif of blinking LEDs

https://gfycat.com/SerpentineFor...

Last Edited: Wed. Dec 20, 2017 - 05:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

awneil wrote:
John already covered that:
John_A_Brown wrote:
John_A_Brown wrote: bidirectional port at the top
I was assuming that was with regard for material movement...

David (aka frog_jr)

Last Edited: Wed. Dec 20, 2017 - 06:03 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

perhaps we should clarify that it's a multi-function, bidirectional port at the top ... 

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:
at the top ...
and the bottom...devil

David (aka frog_jr)

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

Nice :-)

It was a school project, I presume.

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

El Tangas wrote:

Nice :-)

It was a school project, I presume.

 

Ah no, I just made a blinking pattern to make sure I knew what I was doing. I'll be using these lights for debugging purposes for something else.

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

Congrats!

 

From zero to that in one day, and 21 posts in thread to solution - with 4 posts being yours and with steady progress.

 

It's been a pleasure, so a heartly Welcome to AVRfreaks. Don't go away - your kind is needed here. (-:

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Well done!

 

Jim

 

Click Link: Get Free Stock: Retire early! PM for strategy

share.robinhood.com/jamesc3274
get $5 free gold/silver https://www.onegold.com/join/713...