0

I think I forgot the reason for this SFR manipulation in microcontrollers; like, checking the TWSR in twi.

The technique for checking the values of the TWSR is by masking it with another variable or a value.

So, why not checking the TWSR directly in; for example, switch function for checking the values of TWSR?

Like, switch (TWSR)

instead of

uint8_t Status; Status = TWSR & 0xF8; switch(Status)

Or, substitute it in any function; like, Serial.println(TWSR); or digitalRead(TWSR);. DO I have to mask it? If so then why?

  • switch is not a function; it doesn't provide a result. switch is nothing else than an if-else in disguise. You can't change what switch do, nor re-implement it in any other way. A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case. – user31481 Nov 20 '17 at 8:47
  • bit 0 and bit 1 of the TWSR register specify the I2C clock speed, so they may differ depending on the speed you selected (Wire.setClock()) – Gerben Nov 20 '17 at 10:47
  • What I mean is that why I have to mask TWSR instead of substitute it in any function parameter? – R1S8K Nov 20 '17 at 12:29
  • 1
    You mask it to ignore bits you don't care about. In a switch, you'd have to list all of those as explicit cases which would be inefficient to express in source code, even if a very optimizing compiler might figure out how to condense the actual tests to ignore the irrelevant bits. – Chris Stratton Nov 20 '17 at 17:26
  • OK, then why don't just check for TWSR values even with bit 0 and bit 1 included in the test, because they have ..... or wait. I'm sorry, it must be masked because these two bits may change between different I2C clock speeds. – R1S8K Nov 21 '17 at 19:41
1

Why it's not possible to manipulate the TWSR directly? i2c c switch

Because your premise is wrong: it is routinely done to manipulate twsr directly.

As to the use of masks, well, because sometimes you only care about certain bits but not others. So you mask away those that you don't want.

Just that simple.

| improve this answer | |
  • OK, so all SFRs are accessible for direct manipulation. But this one is masked for unwanted bits. Thank you :) – R1S8K Nov 22 '17 at 5:46
0

The mask is a must, because bit 0 and bit 1 are I2C clock speed bits and they are changeable between different I2C clock speed configurations.

| improve this answer | |

Not the answer you're looking for? Browse other questions tagged or ask your own question.