|
|
|
Course Materials
|
|
MUSIC 250A / CS377C
Human Computer Interaction Theory and Practice:
Designing New Devices |
AVRGCC Syntax
A quick guide to some of the C syntax that is specific to avr-gcc.
For more examples, see Harold Leitner's Appplication Notes
- Data Types - it is very important to keep track of how big your variables are. The following are defined in
inttypes.h . When used in your program, they are created in RAM.
Data Type |
Length (bits | bytes) |
Value Range |
Uint8_t |
8 | 1 |
0 to 255 |
Int8_t |
8 | 1 |
-128 to 127 |
Uint16_t |
16 | 2 |
0 to 65535 |
Int16_t |
16 | 2 |
-32768 to 32767 |
Uint32_t |
32 | 4 |
0 to 4294967295 |
Int32_t |
32 | 4 |
-2147483648 to 2147483647 |
Uint64_t |
64 | 8 |
0 to 1.8*1019 |
Int64_t |
64 | 8 |
-9.2*1018 to 9.2*1018 |
- There is another set of data types defined in the AVRLIB in
global.h , that sometimes easier to use. You will find them more commonly in our demo programs.
Data Type |
Length (bits | bytes) |
Value Range |
u08 |
8 | 1 |
0 to 255 |
s08 |
8 | 1 |
-128 to 127 |
u16 |
16 | 2 |
0 to 65535 |
s16 |
16 | 2 |
-32768 to 32767 |
u32 |
32 | 4 |
0 to 4294967295 |
s32 |
32 | 4 |
-2147483648 to 2147483647 |
u64 |
64 | 8 |
0 to 1.8*1019 |
s64 |
64 | 8 |
-9.2*1018 to 9.2*1018 |
- Setting and Clearing Bits - remember that setting a bit is setting it high or to 1, and clearing a bit is making it low or 0.
void sbi(u08 register, u08 bit)
Sets a bit in a register. For example, to set the 0th bit of Port D, you can use:
sbi(PORTD,0);
or
sbi(PORTD,PD0);
or
sbi(PORTD,PIND0);
void cbi(u08 register, u08 bit)
Clears a bit in a register. For example, to clear the 2nd bit of Port B, you can use:
cbi(PORTB,2);
etc...
- Writing to and Reading from registers
void outb(u08 port, u08 val)
Writes the byte val to a register. Note! In the Leitner documentation and some old demos you may see void outp(u08 val, u08 port) . This syntax is being phased out so try to avoid it.
outb(PORTD,0x0F);
would write 0x0F or 00001111 to Port D.
u08 inb(u08 port)
Returns a byte containing the value in a register. As with outp / outb , you may sometimes see inp , but try to avoid using it.
u08 status;
status = inb(PIND);
is the correct read the value from the Port D pins into the variable status .
Note the difference between PIND and PORTD . PIND is read-only and gives the value at the physical pins on Port D. PORTD is a read/write register that is normally used for writing. You cannot use:
status = inb(PORTD); // DON'T DO THIS IF YOU WANT TO READ THE PORT D PINS
For more details, and descriptions of these registers, please see page 111 in the I/O section of the Mega163 Spec.
back
|