Skip to content

Instantly share code, notes, and snippets.

@koepnick
Last active January 31, 2021 01:23
Show Gist options
  • Save koepnick/b07ea05a37872228cca5ef53f3ad7ab1 to your computer and use it in GitHub Desktop.
Save koepnick/b07ea05a37872228cca5ef53f3ad7ab1 to your computer and use it in GitHub Desktop.
attiny85_interrupt_bitmasking_example.cpp
// Hopefully this will make some amount of sense.
// PCIE in the ATTiny85 headers simply expands to 5, but pretend for a moment
// that it is an unknown constant
GIMSK |= (PCIE << 6); /* <-- Use this! It's confusing as hell, especially given that the OR operator
acts more like addition in that:
00100010 | [34]
10000010 [130]
________
10100010 [162]
Follow the columns and ignore the base 10 integers to the right
How can `34 | 130 == 162`? What kind of god would allow that?!
Would a loving god permit `34 & 130 == 2`?!
If it helps, think of it this way:
Take 1 (00000001), shift it to the left 7 spaces (10000000). This represents (in this
case) INT0 as found on the ATTiny85 datasheet¹. The first operand of the OR statement is
irrelevant and could be completely unknown to us. We're just making sure that the final
result (whatever it may be) has a 1 in the 7th spot. So that:
???????? |
10000000
________
1???????
¹ As an example, the full bitmask for GIMSK (and this is **not** authoritative, always check
your datasheets) is:
Index: 7 6 5 4 3 2 1 0
---- INT0 PCIE ----- ----- ----- ----- -----
Access Capability: R RW RW R R R R R
Initial Value: 0 0 0 0 0 0 0 0
Don't be afraid to use a different tool to conceptualize this.
I find that interactive REPLs are way, way better for tinkering
python3
>>> print(f"{1:#010b}")
0b00000001
>>> print(f"{1 << 7:#010b}")
0b10000000
*/
// GIMSK = 0b01000000; <-- This is an anti-pattern. It would work fine in this particular case since
// PCIE is a constant integer 5. But in other cases where the originating bitmask may be dynamic,
// you will lose any of the bit information that had been on the preexisting variable.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment