Go Down

Topic: Equivalent for PORTB in Arduino Mega 2560 (Read 12696 times) previous topic - next topic

Cisco94

Hello,

like in the subject I would like to know the equivalent for the instruction:


Code: [Select]

PORTB |= 0x01;
PORTB &= ~0x01;


which is working in Arduino Uno to control whether the digital pin 8 to 13 are HIGH or LOW in the Arduino Mega 2560.

Thank you

DrAzzy

#1
Jan 05, 2017, 12:02 am Last Edit: Jan 05, 2017, 12:02 am by DrAzzy
Same, only with a different PORT register and value to and/or it with, depending on which pin.



See a pinout chart (google image search will find nice pinout diagrams - just search the board and the word pinout). They typically show the port pin as P, followed by the letter of the port, then the bit within the port (for example PB0 for the pin you referred to).

So for PB0, it would be
PORTB |= 0x01;
PORTB &= ~0x01;

while for PG5, it would be
PORTG |= 0x10;
PORTG &= ~0x10;

Note that you can also toggle the pin in a single operation by writing a 1 to the PINx register, ex:
PINB=0x01;


ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Cisco94

Thank you very much for your reply.

Sorry, I am sure you are right  but I am not able to understand you :smiley-confuse:

I was thinking that my operation PORTB |= 0x01 just would have set the digital pins from 8 to 13 HIGH at the same time, now you are telling me that it sets only the PB0?

And, what 0x01 means? ( I thought it was 1 in decimal).

Can you kindly be more exhaustive and help a poor student going crazy for his graduating thesis?

Anyway thank you


CrossRoads

#3
Jan 05, 2017, 04:37 pm Last Edit: Jan 05, 2017, 04:38 pm by CrossRoads
PORTB |= 0x01 only changes 1 bit. 0x01 is the same as 0b00000001.

If you want all 6 bits of port B, use 0x3F, or 0b00111111.
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Cisco94

Yes exactly I just need to change the digital pin 8.

I am slowly understanding the logic of port mapping... :)

So from Uno to Mega 2560:  PORTB |= 0x01  would become PORTH |= 0x04 ?

Thank you!

CrossRoads

Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Cisco94

Yes, you are right.

I finally understood  :)

Thank you very much..I'll try it later but I am almost sure it'll work.

CrossRoads

I do that all the time to set & clear bits:
PORTB = PORTB | 0x01; // set LSB, leave rest alone
PORTB = PORTB & 0b11111110; // or 0xFE. clear LSB, leave rest alone
Designing & building electrical circuits for over 25 years.  Screw Shield for Mega/Due/Uno,  Bobuino with ATMega1284P, & other '328P & '1284P creations & offerings at  my website.

Go Up