The ATtiny13’s default clock source setting is the Internal RC Oscillator running at 9.6MHz with clock divider /8 (fuses L=0x6A and H=0xFF; see fuse calculator). This short article explains how to downclock the ATtiny13 by swtiching to Internal RC Oscillator running at 128kHz without clock divider (fuses L=0x7B and H=0xFF) and still have a control to program AVR chip.
Example Code
/* main.c */ #include <avr/io.h> #include <util/delay.h> #define LED_PIN PB0 int main(void) { /* setup */ DDRB |= _BV(LED_PIN); // set LED pin as OUTPUT /* loop */ while (1) { PORTB ^= _BV(LED_PIN); // toggle LED pin _delay_ms(500); // wait 0.5s } }
Compiling & Burning
At first, you need to compile the code with required F_CPU value (128kHz). Nextly, you can burn the code into a chip using avrdude and by enabling Slow-SCK function of USBasp programmer (JP3). If you’re getting a warning about programmer’s firmware update then take a look at How to update AVR USBasp firmware to latest version.
avr-gcc -std=c99 -Wall -g -Os -mmcu=attiny13 -DF_CPU=128000 -I. -o main.o main.c avr-objcopy -j .text -j .data -O ihex main.o main.hex avrdude -p attiny13 -c usbasp -B 1024 -U flash:w:main.hex:i -F -P usb
Setting Fuse Bits
Now, you can update the fuse bits with the command bellow.
avrdude -p attiny13 -c usbasp -U hfuse:w:0xFF:m -U lfuse:w:0x7B:m
Final Words
By setting the chip to run at low frequency clock source, the computational power is proportionally smaller but also power consumption is decreasing what gives a bunch of posibilites for a battery powered applications.