AVR, C and Arduino
Our chip today: ATtiny44
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
- Small: 14 pins
- Cheap: ~0.5โฌ
- Same family as most Arduino chips, AVR manufactured by Microchip formerly Atmel.
- The Arduino UNO uses the bigger ATMega range, mostly the ATMEGA328P.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
The Arduino abstraction
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Follow the instructions to use Arduino with the ATtiny family.
How does Arduino turns this in to real AVR C?
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Learn more
The "real" thing
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Let's look at the datasheet ATtinyX4 Datasheet
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Memory
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Registers
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
What a byte is?
Lenght |
Name |
Example |
1 |
Bit |
0 |
8 |
Byte |
10110101 |
Let's think on them as collection of 8 switches:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Addressing registers
- DDRx register: Configures pins as input or output Do we want the pin for a button or an led? (1=OUTPUT and 0=INPUT)
- PORTx register: Read data from pins Is the button pressed or not? (1=HIGH and 0=LOW)
- PINx register: Write data to pins Turn on or off the led. (1=HIGH and 0=LOW, on INPUT pins 1=PULLUP)
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Name |
Bit 7 |
Bit 6 |
Bit 5 |
Bit 4 |
Bit 3 |
Bit 2 |
Bit 1 |
Bit 0 |
0b |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
PORTA |
PORTA7 |
PORTA6 |
PORTA5 |
PORTA4 |
PORTA3 |
PORTA2 |
PORTA1 |
PORTA0 |
PDDRA |
DDRA7 |
DDRA6 |
DDRA5 |
DDRA4 |
DDRA3 |
DDRA2 |
DDRA1 |
DDRA0 |
PINA |
PINA7 |
PINA6 |
PINA5 |
PINA4 |
INTA3 |
PINA2 |
PINA1 |
PINA0 |
Test this with your Arduino IDE as before:
Now, let's do the examples using plain AVR C as on the FabAcademy examples:
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
To test the following examples in plain AVR C without Arduino download the template here with the make file included
Bits manipulation
Bit manipulation is the act of algorithmically manipulating bits or other pieces of data shorter than a word. In digital computer programming, a bitwise operation operates on one or more bit patterns or binary numerals at the level of their individual bits. Wikipedia
Bit Shifting
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Bit Masking
PORTA = 0b00000000;
OR operation:
PORTA = 0b01000000;
PORTA = (1 << 6) | PORTA
Let's apply bit masking in our example:
Macros
In the C preprocessor a macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of the macro.
Is the same as this:
Let's turn our bit masks in to macros:
Let's turn our Macros in to Neil's style macros. First just the functions:
And then the pins names also:
Are you telling me the Arduino is doing all this things for me?
Yes, check the source code

How to install?
For Linux and Mac
Linux $ sudo apt-get install gcc-avr binutils-avr avr-libc avrdude
Mac $ brew tap osx-cross/avr
$ brew install avr-gcc avrdude --with-usb
Compiler
GCC, the GNU Compiler Collection, turns your C and C++ code in to machine instructions your microcontroller can use. It's fully Open Source is somehow the father of Linux and the Open Source movement.
- Compile the code: avr-gcc
$ avr-gcc -mmcu=attiny44 -Wall -Os -DF_CPU=1000000 -I./ -o hello.blink.44.out hello.blink.44.c
- Convert the code: avr-avr-objcopy
$ avr-objcopy -O ihex hello.blink.44.out hello.blink.44.hex
- Check the size of the code: avr-size
$ avr-size --mcu=1000000 --format=avr hello.blink.44.out
Programmer software and hardware
- Upload the code using your fabISP / tinyISP
$ avrdude -p t44 -P usb -c usbtiny -U flash:w:hello.blink.44.hex

Fuses
Fuses are configuration parameters, or like the chip's BIOS. They control things like which oscillator to use, and what speed to run at (ie. the internal 8MHz oscillator, or an external crystal), brownout detection, and the size of the boot flash.
For example, you added a 20Mhz external crystal to your microcontroller PCB. In this case we will need to set the fuses for exteternal clock at 20 Mhz.
There are 3 bytes of permanent storage in the chip called fuse low byte, fuse high byte and fuse extended byte. These bytes are called fuses and can be reprogrammed as many times as you want and determines the behaviour of the chip. To do that, their value is not erased when the chip is powered off or reprogrammed.
We can set the fuses using avrdude as on the exemple below where -U hfuse:w:0xDF:m
refears to the high byte and -U lfuse:w:0xFF:m
to the low byte.
$ avrdude -c usbtiny -p attiny44 -U hfuse:w:0xDF:m -U lfuse:w:0xFF:m
How do I know the values of the fuses?
All the information about the fuses is on the microcontroller datasheet (see: 21.2 Fuse Bytes, pg. 142). However we have tools to make our live easier as the Fuses generator
On the Arduino IDE when we set the Clock Speed parameter under the Tools menu, the software will internally update the fuses when we upload the code.
Make files
When you need to deal with multiple configurations and commands when compiling a software, you can use the make command on Linux/Max for automatizing this task.
$ make
reads automatically a Makefile file in the folder where you launch it (it should be the folder where your project can be found). Otherwise, you can specify it with $ make -f filename
, especially if you have more Makefiles with different names.
Read more on Fab Academy and Make Files
Example Makefile
Download the test code here
Support Videos
Bits and bytes
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Arduino and registers
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ
Reading a datasheet for an AVR chip
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More โ