Tip #8 Data Types and Sizes

In addition to reducing code size, selecting a proper data type and size will reduce execution time as well. For AVR 8-bit, accessing 8-bit (Byte) value is always the most efficient way.

The table below shows the difference between 8-bit and 16-bit variables.

Table 1. Example of Data Types and Sizes
16-bit variable 8-bit variable
C source code
#include <avr/io.h>
int main(void)
{
uint16_t local_1 = 10;
 do {
PORTB ^= 0x80;
} while (--local_1);
}
#include <avr/io.h>
int main(void)
{
uint8_t local_1 = 10;
do {
PORTB ^= 0x80;
} while (--local_1);
}
AVR Memory Usage

Program: 94 bytes (1.1% Full)

(.text + .data + .bootloader)

Data: 0 bytes (0.0% Full)

(.data + .bss + .noinit)

Program: 92 bytes (1.1% Full)

(.text + .data + .bootloader)

Data: 0 bytes (0.0% Full)

(.data + .bss + .noinit)

Cycle counter 90 79
Compiler optimization level -O2 -O2
Note: The loop will be unrolled by compiler automatically with –O3 option. Then the loop will be expanded into repeating operations indicated by the loop index, so for this example there is no difference with –O3 option enabled.