Where I describe the process of developing code for the Microchip ATtiny13A, starting at the chip level.
Introduction#
This is the first in a series of posts describing how to begin developing at the chip level (as compared to board level). In this series, I’ll be using a Microchip ATtiny13A, which is a minimal version of an ATmega328P, the microcontroller used in the Arduino Uno R3.
The chip has the following:
- 8 pin plastic dual-in-line package (PDIP), which is easy to use in a breadboard
- low-power CMOS 8-bit microcontroller, great for battery-powered projects
- 1K Bytes of Flash program memory, enough for small projects
- 64 Bytes EEPROM
- 64 Bytes Internal SRAM
- 6 general purpose I/O pins (GPIO)
- 4 channel 10-bit ADC
- 1 8-bit timer/counter
- debugWIRE On-chip Debug System
- In-System Programmable via SPI Port
- Internal Calibrated Oscillator
Comparison to the ATmega328P (Arduino Uno R3)#
| Feature | ATmega328P | ATtiny13A |
|---|---|---|
| Architecture | 8-bit AVR RISC | 8-bit AVR RISC |
| Flash Memory | 32KB | 1KB |
| RAM (SRAM) | 2KB | 64 bytes |
| EEPROM | 1KB | 64 bytes |
| GPIO Pins | 23 | 6 |
| Pin Count | 28/32 | 8 |
| Timer/Counters | 3 (Two 8-bit, One 16-bit) | 1 (8-bit with prescaler) |
| PWM Channels | 6 | 2 |
| ADC | 10-bit, 8 channels | 10-bit, 4 channels |
| Operating Voltage | 1.8V - 5.5V | 1.8V - 5.5V |
| Max Clock Speed | 20 MHz | 20 MHz |
| Communication | USART, SPI, I²C | SPI |
| Analog Comparator | Yes | Yes |
| Watchdog Timer | Yes | Yes |
| debugWIRE | Yes | Yes |
| Power Modes | 6 modes | Multiple low-power modes |
| Brown-out Detection | Yes | Yes (programmable) |
Why is this comparison important?
It is helpful to understand that this chip is significantly less capable than the chip used by the Uno. That said, it perfect for projects which need a “little bit of intelligence”, and perhaps, three inputs and three outputs.
Repository#
The best way to understand how to develop code for the ATtiny13A is to use this repository. It contains many examples as well as explanations as to how to program using a programmer such as the Microchip SNAP or the ATMEL-ICE. The final bit of advice is to use Bloom along with avr-gdb, to do your development.
Development Code#
Per the repository…
“This repository provides example programs in C (ANSI C99) AVR-LibC and AVR assembly language which support programming the ATtiny13A. In order to use this framework, you need to install the GNU avr tool chain appropriate for your computer (Linux, macOS, or Windows).
All example programs live in the examples folder. A subfolder will either contain a C language, assembly language or mixed-C/assembly example. A C language or mixed-C/assembly will use main.cas the principal program. Assembly language examples are named with an asm_ prefix (e.g. asm_blink) and use main.S as their principal program. The same root Makefile will build all three kinds, as it auto-detects whether to link freestanding (assembly) or with the C runtime. The standard make commands such as make flash, make size, etc. work on all examples. See docs/assembly_examples.md for the assembly examples index.”
With only 1KB of FLASH (or 512 AVR instructions), its important to use assembly language where you can, to save space. That said, its nice to use the C language at times for easier development. There are also situations where assembly language enables capabilities which are easily managed using C.
Serial Example#
A great example is the serial interface. In C, the serial interface was limited to a flaky 9600 baud. It worked, however, not consistently and it wasn’t rock solid. Once I re-wrote the serial program in assembly code, the code was rock-solid at 9600 baud. I also realized that a simple serial interface was required, there is little need (and no space) for an atoi/itoa (ascii to integer/integer to ascii) routines. The new version has the following (C API):
// Initialise TX/RX pin directions and idle state.
void init_serial(void);
// Transmit one hex byte at 9600-8-N-1.
void char_write(uint8_t c);
// Transmit one hex word at 9600-8-N-1.
void word_write(uint16_t c);
// Block until one hex byte is received at 9600-8-N-1.
uint8_t char_read(void);
// Write program memory text (ASCII) to console
void flash_write(uint16_t addr);The last routine, uses program memory to output text, saving the precious 64-bytes of RAM.
Make Commands#
There are multiple options for make, the tool used to compile, link and load files on to the ATtiny13A. All of these options work for the three types of files, automatically, a C language file, an assembly language file and a mixed C and assembly language file.
make compile - compile only (Arduino verify)#
This command make compile is the same as the Arduino verify command. Its a good command to use when you simply want to confirm the code you are working on, will compile.
make flash - show program size and flash to board (Arduino upload)#
This command make flash is the same as the Arduino upload command. It will compile the code (if necessary) then upload it to the ATtiny13A. I rarely use this specific command, as I prefer to use avr-gdb to load and debug my programs.
make clean - delete all non-source files in folder#
When a bug doesn’t seem to go away, it is helpful to delete all of the non-source files in the folder. This command make clean deletes the non-source files only in the folder. If you want a full clean, use the command make all_clean, as this will delete all of the Library object files as well. This is the best solution to “problems which don’t go away”, start fresh and recompile!
make clean_all - run clean in every examples/ folder#
This command, make clean_all is great for cleaning up all of the folders to have a clean repository. It won’t necessarily fix problems, it simply removes all non-source files from every folder.
make build_all - build every examples/ folder#
Rarely used, however, valuable for finding problems which might stem from a global change is make build_all. This executes a build for every example.
make complete - clean, compile and size#
This command make complete is the command I use the most. It ensures there is a full rebuild of the target and it reports the size of the elf file upon completion.
make verbose - upload with programming information for debugging upload#
If you are experiencing problems uploading to the ATtiny13A, use this command make verbose. It will show much more information as to the avrdude upload process.
make env - print active env.make variables#
Another great debugging command is make env as it simply prints the active environment variables. Very helpful to see if the proper USB port is being activated.
make size - print size information of elf file#
This command make size will provide the size of the executable file, main.elf.
make help - print this message#
The last command is sometimes the best one to use, make help will print all of the options for make.