I'm glad it worked
Not sure, though, what was wrong with Your setup, or code.
Bitwise manipulation allows You to SET or CLEAR (set HIGH or LOW) any single pin of microcontroller without affecting other pins, as opposed to switching whole section of pins with Your code.
I'll try to explain, so bear with me
PORTC |= (1 << PORTC0) translates into:
PORTC = PORTC | (1 << PORTC0)To expand on the above bitwise operators work in this way:
To SET required bit You use | (OR) operator.
| (OR) operator needs at least one
1 to produce
1, if that does not make sense - read on...
00001010 |
00001101
=
00001111;10100101 |
01010110
=
11110111;<< (shift left) operator shifts all bits by given number to the left
00000001 << 5 = 00100000;
00000001 << 1 = 00000010;
01010101 << 1 = 10101010;All mnemonics (memorable names for ports and pins) in our original line
PORTC |= (1 << PORTC0) actually means:
- PORTC is a set of pins which belong to C block in a microcontroller; it is by default set 0 (0x00 in HEX; 0b00000000 in binary) every time You start microcontroller.
- 1 in binary is 0b00000001.
- PORTC0 simply means pin 0 of port C.
Our line
PORTC = PORTC | 1 << PORTC0 now translates to:
PORTC = 00000000 | (00000001 << 0) =
= 00000000 | 00000001 =
= 00000001;Can You see now that this way You do not affect other pins apart from the one You need?
To CLEAR required bit You use & (AND) operator and ~(NOT) operator:
PORTC &= ~(1 << PORTC0), which translates to
PORTC = PORTC & ~(1 << PORTC0).
& (AND) operator requires two
1s to produce
1...
00000101 &
11110110
=
00000100;~ (NOT) operator inverts all bits changing all 1s to 0s and all 0s to 1s.
~00000000 is equal to 11111111;
~01010101 is equal to 10101010;
~00000001 is equal to 11111110;Lets say that Your LED is shining now meaning that Pin 0 of Port C is set HIGH, therefore PORTC = 0b00000001;
Our line
PORTC = PORTC & ~(1 << PORTC0) now translates to:
PORTC = 00000001 & ~(00000001 << 0) =
= 00000001 & ~(00000001) =
= 00000001 & 11111110 =
= 00000000;Once again, You can CLEAR one pin only without having effect on others.
Info provided here is based on
Bitwise Operators in C and C++: A Tutorial by
Alex Allain.
Hope this was clear enough to understand.