ATmega 2560 - basic question

Thread Starter

Morten Andersen

Joined Jun 7, 2019
25
So i´ve recently started trying to learn coding a microcontroller. I got myself an Arduino ATmega2560.
Im writing in Atmelstudio.
To start with im just trying to turn 2 LED's on and off. I¨ve been able to turn on and of an LED connected to input 4 with the following code:
DDRG |= (0xFF); //setting all G´s as outputs.
PORTG |=(1<<PB5); //setting 4 on board as high, making LED light.
PORTG ^=(1<<PB5); //turning off LED again.
The last line is just put in to turn of the LED again.

If i put in the code:
DDRG |= (0xFF);
PORTG |=(1<<PB5);
PORTG |=(1<<PB2); //should set 3 as high, as far as i can se in the datasheet?
This however dosent seem to work,

I know this is a very basic understanding problem. but it would be much appreciated if anyone could explain this to me.
 

SProg

Joined Nov 10, 2018
9
Code:
DDRG = 0xFF;  // Set Port G as output
PORTG |=    (1 << PG0); // Pin PG0 goes high
.
.
.
.
.
PORTG &= ~(1 << PG0); // Pin PG0 goes low
I cannot understand the question.
 

danadak

Joined Mar 10, 2018
3,620
Why are you using the inclusive OR operator |= in the assigns ?

Code:
DDRG |= (0xFF);
PORTG |=(1<<PB5);
PORTG |=(1<<PB2); //should set 3 as high, as far as i can se in the datasheet?
First line of code does the inclusive OR with 0b11111111
Second line of code inclusive OR with 0b00100000
Third line of code inclusive OR with 0b00000100

Is that what you want ?

Regards, Dana.
 
Last edited:

SProg

Joined Nov 10, 2018
9
Used to change the status only of the desired bit, letting the other 7 bits without being changed/affected.
 

MrChips

Joined Oct 2, 2009
19,416
Before getting into C constructs, it would be a good idea to learn basic boolean operations.

If your compiler accepts binary representation:

DDRG = 0b00000100;
PORTG = 0b00000100;
delay( );
PORTG = 0b00000000;
delay( );

where delay( ) is some function to provide a delay of about 500ms,

If your compiler does not support binary representation:

DDRG = 4;
PORTG = 4;
delay( );
PORTG = 0;
delay( );
 

mckenney

Joined Nov 10, 2018
60
Code:
DDRG |= (0xFF);
PORTG |=(1<<PB5);
PORTG |=(1<<PB2); //should set 3 as high, as far as i can se in the datasheet?
The pin mapping I found at
https://www.arduino.cc/en/Hacking/PinMapping2560
says that digital pin 3 is connected to PE5, not PG2. So you need
Code:
DDRE |= (1 << PE5);
PORTE |= (1 << PE5);
(It's theoretically possible that PE5 is not the same as PB5 or PG5, but no that hasn't happened in 20 years.)
 
Top