Trying to understand register assignments

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

Looking at a particular code for ATmega328P, I see the following assignment
EIMSK |= (1<<INT0);

The Atmega328p specification clearly says - "Bit 7..2 "“ Res: Reserved Bits"

Is the above not a violation of reserved bits which are supposed to be set to 0?
Below is what I have understood from some preliminary investigation...

I had a look at the header file of ATmega328p and some search revealed the following info

#define EIMSK =_SFR_IO8(0x1D)
#define INT0 0
#define _SFR_IO8(io_addr) ((io_addr) + __SFR_OFFSET)

and

# if __AVR_ARCH__ >= 100
# define __SFR_OFFSET 0x00
# else
# define __SFR_OFFSET 0x20
# endif

so let's assume that EIMSK is essentially 0x2D. So in the original assignment i.e. EIMSK |= (1<<INT0), doesn't 0x2D get written to EIMSK(which itself is also 0x2E - location in address space)? And if this is the case, then it's a clear violation of the reserved bits as mentioned in the specification document. I see that such assignments are very common so most likely I am missing something obvious.

Please help me understand this.

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

Quote:
Is the above not a violation of reserved bits which are supposed to be set to 0?
Why would you think that? It sets one bit (INT0) to 1 and leaves all other bits the same. Even if it was "=" instead of "|=", it would set INT0 to 1 and all others to 0.
Quote:
doesn't 0x2D get written to EIMSK
No, 0x2D is the address of EIMSK, not the value being written.
Quote:
#define EIMSK =_SFR_IO8(0x1D)
The "=" does not belong there (and I presume is something that you added).

Regards,
Steve A.

The Board helps those that help themselves.

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

Ok, thanks. I got confused because if EIMSK were a variable instead of a '#define', then it would have had the value '0x2D' after the mentioned assignment.

Thanks again, Steve !

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

EIMSK is at address 0x2D.

First, (1<<INT0) ==> (1<<0) ==> 1
So, EIMSK |= (1<<INT0) means: retrieve the value at address 0x2D and OR it with 1
Ie set the lowest bit to a one (the other bits are left unchanged).

Dr. David Harris OpenLCB Development Team openlcb.org

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

Yeah, ok. Thanks !
I am just wondering what value at address 0x2D we can expect in such cases? I mean, the code I am referring to has the assignment EIMSK |= (1<<INT0); in main and there is no access to EIMSK before this. What default value we can expect in such cases?

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

s.chaudhary wrote:
Yeah, ok. Thanks !
I am just wondering what value at address 0x2D we can expect in such cases? I mean, the code I am referring to has the assignment EIMSK |= (1<<INT0); in main and there is no access to EIMSK before this. What default value we can expect in such cases?
The power-up initial value, which is typically listed in the manual right below the definitions of the register's bits. Look at the "Initial Value" line at the ends of various register definitions (

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

Yeah, got it. Thanks !

Thanks and regards,
Sandeep K Chaudhary,
University of Waterloo, Canada.

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

And by the way, your concern about writing to reserved bits is unfounded. These are simply read only bits, writing to them will do nothing.

Regards,
Steve A.

The Board helps those that help themselves.