PIC 16F877A toggle half of the leds when button is pressed
You should do this:
PORTB ^= flashingleds; (instead of &quot;=&quot;)
With flashingleds being 0x0f. You only have to set ones, where You want to toggle, no need for changing this variable every time.
Whay happens is, that when You do ~0x0f You get 0xf0, so toggling half to on and half to off. When You do what I suggest - You will be xoring half of the port, so this is what was needed.
Another approach could be to set half of the port:
PORTB |= flashingleds
and in the next cycle:
PORTB &amp;= ~flashingleds.
Look that I'm never directly writing to port by &quot;=&quot; - always or, xor, and. This is used to not disturb the other half of port.
Reading of port state is ok, checked in RM.