0

From my research this afternoon, it doesn't seem readily possible.

What I have going on is an array with 9 elements, each element is one byte in length. However, each of those bytes is subdivided into four fields of two bits because those fields control LEDs. Therefore, one byte can control 4 LEDs. For example:

1st byte: 00 01 10 00 where LED1/4 are off, LED2 is on, and LED3 is blinking. LED1 LED2 LED3 LED4

I want to be able to write information into those pairs of bits without disturbing the other bits in that same byte.

Is this possible in C?

What leads me to believe this is not possible is this post here stating that I can only increment the pointer by one byte at a time.

Additionally, I tried to implement this but i could not get it to work.

  • A byte is sometimes referred to as "the smallest addressable unit of memory", so it won't be possible regardless of the language (though one can write to a byte such that only a single bit changes). – Scott Hunter Jul 06 '23 at 18:40
  • Writing to specific bits is done using `&` and `|` operators along with a mask. – Barmar Jul 06 '23 at 18:41
  • XY problem. Instead of asking about your real problem, that is, how to control LEDs, you are asking about a problem with your proposed solution. You cannot write individual bits, but you certainly can conrol LEDs. – n. m. could be an AI Jul 06 '23 at 18:42
  • To turn on a bit, you can read the byte where it resides, `|` that with a value corresponding to the bit you wish to set, then write back the byte. To turn a bit off, you do the same except instead of `|`'ing with the bit, you `&` with a mask that has all bits except the one you care about set to on. – 500 - Internal Server Error Jul 06 '23 at 18:43
  • @Barmar `&` and `|` select *what* you write to the bits, not *which* bits you write. You always write at least a byte. – n. m. could be an AI Jul 06 '23 at 18:45
  • @n.m.willseey'allonReddit That's why you also have to combine it with masking, which allows you to keep the rest of the byte the same. – Barmar Jul 06 '23 at 18:46
  • It's all explained in the duplicate question I linked to. – Barmar Jul 06 '23 at 18:47
  • Note that some MPU registers are write-only (or read something different), and you cannot use the read-modify-write cycle. Instead, you must store a copy of the register value in memory, read-modify-write that variable, and write-only to the target register. – Weather Vane Jul 06 '23 at 18:59
  • 1
    @ScottHunter There are HW architectures allowing addressing single bits and there are C language extensions allowing to use it for these architectures. https://stackoverflow.com/questions/10082229/is-there-a-way-to-address-a-single-bit-in-c – Eugene Sh. Jul 06 '23 at 19:21
  • Logically you could, physically you couldn't. You can copy a byte of 8 bits to a register, and you can replace the same byte 8 times with the bits toggled you want to make it seem like you're writing 1 bit at a time, however in practice, you'd be writing 8 bits at a time. There might be things that do accept single bits - for example some weird CPU architecture or some kind of boolean flag in an fpga... – Owl Jul 06 '23 at 22:51

0 Answers0