Attiny45 LED Blink with AVR GCC winAVR compiler

Attiny13 microcontroller many advantages. One of them is the tiny size which can be useful in application that requires small size electronics pcb footprint. But it has a major disadvantage which is that of tiny 1KB Flash memory. So the program code has to be small to fit into its flash memory or program memory. But if you want small footprint and larger flash memory, the Attiny45 is the best alternative. Attiny45 has 4KB flash memory so you can write larger program to do something useful. Also many uses Arduino and Arduino libraries and they won't fit into Attiny13 or it will be difficult to do so. But with Attiny45 it is possible to write larger program and use Arduino libraries. Last tutorial showed how to blink a led with Attiny13 using AVR GCC winavr compiler. In this Attiny45 AVR tutorial, it is shown how to blink a LED with Attiny45. This gives an introduction to Attiny45 to learn the I/O pins of the Attiny45.

Below is main technical difference between Attiny13 and Attiny45.

Key Technical Differences
FeatureATtiny13ATtiny45
Flash Memory1 KB (Very tight)4 KB (Plenty for most tasks)
RAM (SRAM)64 Bytes256 Bytes
EEPROM64 Bytes256 Bytes
Max Clock Speed9.6 MHz (Internal)20 MHz (External) / 8 MHz (Int)
Timers1 (8-bit)2 (One 8-bit, one High Speed 8-bit)
USI (Serial/I2C)None (Requires bit-banging)Hardware USI (Supports I2C/SPI)
ADC Resolution10-bit10-bit

Here we will use AVR-GCC winavr compiler which allows us to create small size firmware. The advantage of using AVR-GCC winavr compiler is that we can get closer to the Attiny45 hardware. The following is the circuit diagram for blinking a LED.

Attiny45 led blink circuit diagram

1. Circuit Diagram Explanation

The provided schematic (created in Proteus) is a complete, minimal blinking system. It demonstrates that the ATtiny45 requires only a single power rail and very few external components to operate.

Key Connections:

  • 1. The Power Supply (VCC and GND):

    • Pin 8 (VCC) is connected to a 5V DC rail.

    • Pin 4 (GND) is connected to the common ground (0V). This provides the essential operating voltage for the internal CMOS circuitry. * Critically, you have included a 100nF (C1) decoupling capacitor between VCC and GND. This capacitor acts as a local energy reservoir, filtering out high-frequency noise and stabilizing the microcontroller's power during high-current operations, preventing glitches.

  • 2.The LED Output Circuit (PB0):

                Pin 5 (PB0) has been configured in the software as a push-pull digital output. * This pin                         drives a 220Ω (R1) current-limiting resistor connected to the anode of a standard LED. The                   cathode of the LED connects to ground. The resistor is mandatory; without it, the ATtiny45's                 output pin would attempt to source excessive current, likely destroying both the LED and the                     microcontroller.

2. LED Blinking Code Explanation

The C code uses standard AVR-GCC bitwise operations to directly manipulate the microcontroller’s hardware registers. This is far more efficient and uses less memory (perfect for 8-pin chips) than using a generic Arduino function.


#ifndef F_CPU
#define F_CPU 1000000UL // Assumes ATtiny45 default 1MHz clock
#endif

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
    // Set PB0 as an OUTPUT pin.
    // 'DDRB' is the Data Direction Register for Port B. 
    // This line performs a bitwise OR (|) to set only the bit 0 of DDRB HIGH,
    // leaving other pins unchanged. A '1' in DDRB means "Output."
    DDRB |= (1 << PB0); 

    while (1) {
        // Toggle the LED ON
        // 'PORTB' controls the logical high/low state of the output pins.
        // This line performs a bitwise OR to set only PB0 HIGH.
        PORTB |= (1 << PB0);
        _delay_ms(500); // Stall execution for 500 milliseconds (0.5 seconds).

        // Toggle the LED OFF
        // This line uses a bitwise AND (&) with a bitwise NOT (~) operator
        // to set only PB0 LOW while preserving the other pins on Port B.
        PORTB &= ~(1 << PB0);
        _delay_ms(500); // Wait another 500ms before repeating the loop.
    }
}

Code Flow:

  1. Definitions: The code defines the F_CPU (the operating frequency) as 1MHz to ensure the _delay_ms() function is accurate for a factory-default ATtiny45.

  2. Setup (DDRB): Inside main(), it writes a '1' to the specific memory location that controls the direction of Pin PB0, setting it as an output.

  3. Infinite Loop (while(1)): It enters a loop that will run forever. It switches the pin state between logical HIGH (LED ON) and logical LOW (LED OFF), inserting a standard 0.5-second delay between each state change.

Related links

Post a Comment

Previous Post Next Post