WORKING WITH AVR REGISTERS
The "Register Summary" section of the AVR datasheet contains a huge table of all the Special Register names and Special Bit names. Lets take a quick look at the table and talk a bit about it.
Address | Name | Bit 7 | Bit 6 | Bit 5 | Bit 4 | Bit 3 | Bit 2 | Bit 1 | Bit 0 | Page |
---|---|---|---|---|---|---|---|---|---|---|
$3F ($5F) | SREG | I | T | H | S | V | N | Z | C | 10 |
The address field is the address within system memory in hex. The number within the brackets is the actual physical number within the AVR memory, the first number is the address renumbered to start at 0.
The Name field is the name of the register itself, in our case SREG which is short for the Status Register. All AVR register names are 1 to 8 characters in length and consist of capital letters and numbers.
Bit 7 - 0 is the name of the individual bits.
Page shows you the page number which gives you detailed information on the register.
In order to use names you have to define the "avr/io.h" file at the top of your program:
#include <avr/io.h>
Now, lets say we want to set the I bit (interrupt bit) we simply have to:
SREG |= (1 << I);
If you look above, you can see that the I bit is located 7th bit, we could just as easily set it by doing the following:
SREG |= (1 << 7);
If we want to see if the status of the N bit (Negative Flag bit) is set we can simply:
if (SREG & (1 << N) > 0) { // some code here }
You will never have to do anything but read or write an entire register or read or write an individual bite to/from a register. So if you understand binary and hex numbers, bit math and this lesson, you got the hard programming stuff down.
Cheers
Q