Saturday, March 30, 2013

Arduino Function Optimization

Adventures with Arduino: From 41 instructions to 1
http://www.rorydriscoll.com/2011/02/01/adventures-with-arduino-from-41-instructions-to-1/

I/O Operation for AVR

AVR ports are labeled port A though port D on most DIP packages (look at the datasheet for your AVR to find the right port.) For instance, for port B:

You write to port B with the PORTB register
You read (input) port B with the PINB register

The input or output status of each bit in the port can be controlled with the DDR (Data Direction Register), so that each pins in a single port can serve different purposes.

Set port B, pin 2 for input (this clears bit DDB2 in the DDR):

DDRB &= ~_BV(DDB2);

To get the value on port B, pin2:

val = PINB & _BV(PB2);

This ANDs the PINB register with the bit value of pin 2 (PB2)--masking out the other unwanted bits. 



Dr. Dobbs,
Al Williams 
DigtialWrite Optimization
http://v4.drdobbs.com/blogs/embedded-systems/240148309

No comments:

Post a Comment