AVR ATtiny4313 in Bascom, GCC and Assembler

This project explores the functionalities of the AVR ATtiny4313 microcontroller using three different programming environments: Bascom-AVR, GCC, and Assembly language. The focus is on utilizing timers and interrupts to achieve tasks like blinking an LED.

Hardware Setup:

The project utilizes an ATtiny4313 microcontroller mounted on a breadboard. A crystal oscillator with two capacitors sets the system clock frequency. Capacitors are also used for voltage filtering. Programming is done through a programmer like AVRPROG USB V2, which connects to the PC’s USB port and communicates with the microcontroller. The programmer requires drivers and documentation readily available online.

Blinking LED:

The most basic example involves blinking an LED connected to pin PD5. The project demonstrates this functionality in all three programming environments.

Stack and Program Termination (Assembly Specific):

In the assembly language version, the stack pointer (SP) is initialized with the end of data memory (RAMEND) after the begin label. This is crucial for subroutine calls and returns. Since the ATtiny4313 has limited RAM (256 bytes), only the lower byte (SPL) is relevant. The higher byte (SPH) can be initialized to zero, though it might work even if left as it is.

To terminate program execution in assembly, the provided disassembly of the GCC version reveals a jump instruction to the program counter (PC) with the current instruction address. This essentially creates an infinite loop, effectively halting further execution.

Timers and Interrupts:

Timers are hardware components used for generating precise delays or timing events. The ATtiny4313 comes pre-programmed with fuses to use an internal oscillator. However, for better stability, an external crystal oscillator is recommended. The crystal frequency is used by the microcontroller’s clock, with the option for further division using a pre scaler.


Interrupts are a mechanism that allows the microcontroller to temporarily pause its current execution and respond to an external event. These events can be triggered by timers, external signals, or other sources. The sei and cli instructions enable and disable interrupts globally, respectively. Individual interrupts can also be controlled.

Timer/Counter Modes and Clock Selection:

The project delves into the different modes available for Timer/Counters on the ATtiny4313. These modes determine how the timer operates and generates interrupts. Additionally, the clock selection allows specifying the source that drives the timer’s counting.

Interrupt Mask and Control Registers:

The project explains the registers responsible for managing interrupts. The Timer/Counter Interrupt Mask register (TIMSK) enables or disables specific timer interrupts, while the Timer1 Clock Control Registers (TCCR1A and TCCR1B) configure the timer’s mode and clock source.

Pre scaler Discussion:

The GCC examples set the system clock to 8 MHz with a pre scaler of 1. However, the project highlights that achieving the desired blinking frequency (between 0.5 Hz and 5 Hz) could be accomplished without pre scaling by adjusting the timer’s I/O pre scaler or top value for counter overflow or compare match interrupts (as done in the assembly programs). Pre scaling essentially reduces the clock frequency, which might not be necessary in this case.

Blinking LED with Timer Interrupts:

The final section explores two variations of blinking an LED using Timer1 interrupts:

  • Counter Overflow Interrupt: This method utilizes Timer0 for the blinking frequency and Timer1 for the duration of the LED’s on/off state. Both timers generate interrupts on overflow.
  • CTC (Clear Timer on Compare Match) Interrupt: This approach sets a compare value in the OCR1A register and configures Timer1 in CTC mode. An interrupt is triggered when the timer’s counter matches the compare value, effectively resetting the timer and creating the blinking pattern.

The project with the ATtiny4313 demonstrates the versatility of timers and interrupts for precise timing control in embedded systems programming. It showcases the same functionality using three different programming environments, allowing users to choose the one that best suits their needs and experience level.

Follow this link for complete project: AVR ATtiny4313 in Bascom, GCC and Assembler

About The Author

Ibrar Ayyub

I am an experienced technical writer holding a Master's degree in computer science from BZU Multan, Pakistan University. With a background spanning various industries, particularly in home automation and engineering, I have honed my skills in crafting clear and concise content. Proficient in leveraging infographics and diagrams, I strive to simplify complex concepts for readers. My strength lies in thorough research and presenting information in a structured and logical format.

Follow Us:

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top