NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Code question: Setting bits/pins high or low

January 13, 2010
by Solorbob
Solorbob's Avatar

I'm working my way through the coding examples, and I'm not really sure on the syntax and/or understanding of how to set pins or bits high or low.

I'm looking for someone to point me to a resource, so I can read up and understand what is going on here. I can read through the code, so I know what it is going on, but I couldn't code it myself.

Here is an example of what I'm talking about from the LED Array code.

inline void ledarray_set(uint8_t i, uint8_t j, uint8_t onoff) { if(i < ROWS && j < COLS) { if(onoff) { la_data[j] |= (1<<i); <-- I know this is setting the bit(aka row) of column j to high(1) } else { la_data[j] &= ~(1<<i); <-- This is setting it low(0) } } }

Thanks, Shawn

January 13, 2010
by Solorbob
Solorbob's Avatar

Oops.. Hard to read the above example. Let's try again...

inline void ledarray_set(uint8_t i, uint8_t j, uint8_t onoff) {

if(i < ROWS && j < COLS) {

if(onoff) {

  la_data[j] |= (1<<i);<-- I know this is setting the bit(aka row) of column j to high(1)

} else {

  la_data[j] &= ~(1<<i); <-- This is setting it low(0)

}

}

}

January 13, 2010
by N3Roaster
N3Roaster's Avatar

In C, the | operator does a bitwise OR operation (not to be confused with || which does OR with the entire variable). Similarly, the & operator does a bitwise AND operation (again, not to be confused with &&). Either of these can be placed in front of = to combine the operation with assignment to a variable. To use the first line, this is basically a short hand notation for

la_data[j] = la_data[j] | (1 << i);

The << operator is a bit shift operator. In this case it's taking a 1 and shifting that left i positions. Looking at it in binary notation, the 1 is really 00000001 and if i is, for example 7, we end up with 10000000 (the 1 is seven places farther to the left).

Putting it together for the first line, you're taking the value of la_data[j] and ORing it with a value with a 1 in an arbitrary position. This means that any bits in the original value that were 1 will still be 1 and the bit that was 1 in (1<<i) will also be 1 in the newly assigned value.

In the else portion for setting a bit to 0, we have the ~ operator. This performs a NOT operation. In this case, we create a value with a 1 in some position, then the NOT reverses all of the bits so we have all 1s except for one bit which contains a 0. Combining this with the AND assignment, we end up with a value that only has a 1 in a bit if there was both a 1 in the original value and in the value produced by ~(1<<i). In other words, you're taking the original value and setting an arbitrary bit to 0.

January 14, 2010
by Solorbob
Solorbob's Avatar

Thank you N3Roaster. That is a lot there to digest. Time to re-read it again.

Post a Reply

Please log in to post a reply.

Did you know that you can use a transistor to interface between different voltage levels of digital logic? Learn more...