This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Register's reserved bits

Suppose you want to write something to a 8 bit (or 16 bit) register named REG.

Suppose that REG consists of:

Bit 7-6: option 1 (read/write)

Bit 5: Reserved. Always read as 0. (read only bit)

Bit 4 - 2: option 2 (read/write)

Bit 1: Reserved. Always read as 0. (read only bit)

Bit 0: option 3 (read/write)

supposte i want to set all the options.

Can i write:

REG = 0xDD;

or since  bit 5 and bit 1 are reserved read only bits, do i have to write:

REG |= 0xDD;

?

I think this is important because if you can not use the "direct assign", if you want to clear option 1 and set option 2 e 3, you have to do it in 2 steps (&= and |=).

Until now I have always used  "the assign" but now I wonder if it's correct.

Regards,

Carloalberto

  • If the reserved bits contain a 'must be written as ...' tag, you should respect this (sometimes, a bit has a meaning but not a usefule one, only a destructive one, so it isn't documented). Other reserved bits have no effect at all, but may get an effect on other devices or later implementations. If it is marked as r/o, then it doesn't matter what you write to thsi bit.

    In your example, it doesn't matter, both operations have the same effect.

    However, keep in midn that a '=' operation will clear bits as well as set them. So in case of your option 1, you cannot go back from both bits set to only one bit set by an |= operation. And an &=~0xc0 to clear the bits first would result an intermediate state with both bits clear. an isntruciton like REG = (REG&~0xc0) | 0x80; would be the best then.

    However, if the options are individual bits (like option 3) and you only want to change this one without changing the others, using |= or &=~ to set or clear this bit is the cleanest way and also shows your intention "I want to set/clear this option and keep the others" rather than "I want to reconfigure everything"

    But if you indeed want to do a base configuration, no matter what was there before, a direct assignment is the best way. But you shoudl use the predefined ymbols and OR them to th efinal value, rather than use hexadecimal values:

    REG = OPTION1_3|OPTION2_7|OPTION3;

  • thank you for your explanation !

    Jens-Michael Gross said:

    If it is marked as r/o, then it doesn't matter what you write to thsi bit.

    And i suppose you can also do a direct assignment even if the "bit" is market as reserved rw-1, as far as it is not marked as "must be written as".

    Right?

  • Carloalberto Torghele said:
    And i suppose you can also do a direct assignment even if the "bit" is market as reserved rw-1

    "rw-1" means that is is read/write and initially (after a PUC) set to 1. If an unused bit is marked this way, I'd consider it to be jsut a memory cell that is set on PUC and otherwise can be written and read like RAM.
    So yes, a direct assignment to the whole register shouldn't be a problem then. With or without this bit set.

  • Ok thank you!

    You are the best as always!

**Attention** This is a public forum