Hardware Reference
In-Depth Information
Figure 6-3. R-R2 ladder setup
The sensor code implements a basic binary counter and introduces the use of programming an Arduino using the
AVR registers. The use of the registers in some cases can simplify the code and make it smaller, but it also increases
the complexity of the program. Four registers will need to be manipulated for this code: DDRB , DDRD , PORTB , and PORTD .
The first four letters of these names refer to the register's type, and the last letter designates which set of pins on the
Arduino is being referenced. All the ports discussed are going to be 8 bits in size (or 2 nybbles).
D , this refers to pins 0 through 7.
If the register descriptor is followed by a
B , then it refers to pins 8 through 13, with the last two bits being unusable on
anything being referenced to B .
If followed by a
Correlating the binary to the pin starts at the lowest significant bit as read, so to turn on pin 0 the Arduino PORTD
will be equal to 0b00000001 .
DDR x is the data direction register, which tells the pins whether to be input(0)
or output(1) . This is done by setting the DDR x equal to a byte of data, such as DDRD =
0b11100011 , which will tell pins 7,6,5,1, and 0 to be outputs and pins 4, 3, and 2 to be inputs.
Setting pins by this method is the same as calling the pinMode(pin, direction) function for
each pin in the setup() function. If serial functions are still required, the two lower bits on
xxxx D must be left alone, making the whole byte unavailable.
The
PORT x register equal to a byte allows the groups of pins to be turned on or off within
one line of code, depending on the bytes. In contrast, if a variable is set to equal a PORT x ,
then the contents of the register are read, and, depending on the mode that is set in the DDR x ,
will determine where the bits of data come from: internally for output(1) and externally for
input(0) .
Setting the
Search WWH ::




Custom Search