Hands On AVR
Overview
This wiki page is dedicated to AVR development with focus on the ATtiny13.
Memory
The ATtiny13 has two main memories:
Program memory is 1K Bytes. It's organized as 512x16 Bits, because all AVR instructions are 16 or 32 bits wide.
Data memory is 64 bytes of SRAM (internal memory) + 64 I/O Registers + 32 general purpose registers.
ArduinoISP
Arduino can be used as an In-system programming (ISP) interface for the ATtiny and ATmega microcontrollers.
Here are step-by-step instructions on how to program an attiny13 with an LED blinking program:
- On the Arduino, the ArduinoISP code must be running. Open the Arduino IDE -> examples -> ArduinoISP and write.
- Build the circuit as shown above. Pins are documented in the source code. Use a 470Ω resistor between PB4 (that's pin 3 on the attiny13) and the LED.
- Install AVR toolchain: Windows, Mac
- Compile this code:
/*
hello, LED!
attiny13 Pins:
1: RESET (Arduino 10)
2: NC (not connected)
3: R (470Ω) to LED to GND
4: GND (Arduino Gnd)
5: MOSI (Arduino 11)
6: MISO (Arduino 12)
7: SCK (Arduino 13)
8: VCC (Arduino 5V)
*/
#define F_CPU 9600000 // Define software reference clock for delay duration
#include <avr/io.h>
#include <util/delay.h>
#define LED PB4 // Define led output on PB4
uint16_t d; // delay variable
/**
* delay
*
* @param ms duration in milliseconds
*
*/
void delay_ms(uint16_t ms)
{
for (uint16_t i = 0 ; i < ms ; i++) {
_delay_ms (1); // Loop delay
}
}
int main()
{
d = 250; // delay time in ms
DDRB |= (1 << LED); // Set direction to output for LED
for (;;) { // forever
PORTB ^= (1 << LED);
delay_ms(d);
}
return 0;
}
- run avrdude, make sure you choose the correct USB port and speed, i gave the program the name 'led.hex':
avrdude -P COM9 -b 19200 -c avrisp -p t13 -U flash:w:led.hex:i
- enjoy the blinking LED!
High Voltage Fuse Resetter
If the fusebits are messed up, the AVR can be factory resetted. This can be done with "high-voltage serial programming". Here is a excellent project to reset attiny13/25/45/85 chips: hvsp fuse resetter
tips:
- The schema on the website seems to have bugs, i fixed them, check my copy File:Hvsp-fuse-resetter-schema.pdf.
- The 7x4 LED module can be found on ebay. If the display doesn't work, you probably have to comment out "#define _REVERSE" in the code, this switches common anode/common cathode of the display.
AVR
more on AVR soon.