Bit Math for the Microcontroller Programmer

Bit ManipulationMicrocontrollers have limited memory, processing power and output pins. For example, the Atmega328 runs on 16MHz with 32K of flash memory (where your compiled program resides) with 1K of EEPROM (data that isn’t lost when powered down). So, as you progress with your Arduino skills, you’ll start seeing code that looks like this:

DDRD = DDRD | B11111100; //Mark pins 2 to 7 as outputs without changing value of pins 0 and 1 which are outputs

The code given above is perfectly valid in the Arduino and the full explanation is available on the Port Manipulation reference page.

But don’t panic! This is just simple bit math. Thankfully, there’s an entire reference page that has been written just to address such concerns. If you’re new to manipulating bits, I would highly recommend going through the Bit Math tutorial by CosineKitty. Of course, it will take some time to get used to. But once you’ve worked with it, manipulating bits is no sweat. You don’t have to do the whole page at a go, but just do keep it close at hand once you start pushing the limits of the Blinky program. I’ve found it very useful and I’m sure you will to.

Link: Bit Math tutorial by CosineKitty

This entry was posted in Arduino and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Switch to our mobile site