Monday, October 23, 2023

Pin interrupt library for AVR microcontrollers

This library can help to easily configure a pin interrupt on AVR microcontrollers without the need to check the datasheet every time. At the moment only PCINT pins are supported.

Pin interrupt library for AVR devices

In this blog post, I will explain how to use interrupts on AVR microcontrollers. Interrupts are a powerful feature that allow the microcontroller to respond to external or internal events without constantly polling for them. Interrupts can improve the performance and efficiency of your code, as well as enable new functionalities.

What are interrupts?

An interrupt is a signal that causes the microcontroller to temporarily stop its current execution and jump to a special function called an interrupt service routine (ISR). The ISR performs the necessary actions to handle the interrupt, and then returns to the original program flow. The ISR can be triggered by various sources, such as:

  • External pins (EXTINT)
  • Pin changes (PCINT)
  • Timers/Counters (TIMER)
  • Serial communication (USART, SPI, TWI)
  • Analog-to-digital conversion (ADC)
  • Analog comparator (AC)
  • Watchdog timer (WDT)
  • EEPROM ready (EE READY)
  • Store program memory ready (SPM READY)

Tuesday, October 3, 2023

stepperCon library for controlling stepper motor drivers using timer interrupt on AVR devices

The stepperCon library allows you to control multiple stepper motors at the same time. It can interface with any stepper motor driver that can take inputs such as step and direction. The library also has some extra useful functionalities such as motor coordination using Bresenham line algorithm - useful for controlling 3 axis motors for example, free spin - where the motor keeps spinning, and positioning the motor at a certain degree from 1 to 360 by finding the shortest path.

The maximum speed (step rate) on a 16MHz microcontroller is 25kHz, however the maximum practical speed is 6.25kHz or 8.33kHz depending on the motor, load, supply voltage and micro-step resolution. A higher voltage can yield higher RPM and with 1/32 micro-step resolution the step rate can be 25kHz as oppose to 6.25kHz when using 1/8 micro-step resolution.

Even with multiple motors running at the same time, the speed can be maintained and that is because the speed profile is segmented and calculated while the interrupt routine (ISR) is not executing steps. So the time spent inside the ISR is low since the calculations for acceleration and deceleration are not done there. Only the stepping is done inside the ISR. Having an interrupt to generate the steps is preferred over a function in the main loop where other code could delay the stepping function thus causing motor stuttering.

The stepper library includes the micros library that is used to trigger a timer interrupt every 40us and as a perk you also have a way to keep track of time in your project with a 40us time resolution.

 

stepperCon library characteristics

  • interface with multiple stepper motor drivers
  • up to 3 coordinated motors for X, Y and Z (can be extended for more)
  • maximum stepping rate speed of 6.25kHz (or 8.33kHz if the motor can have high acceleration). Speeds are given for a 16MHz CPU. With higher CPU clock the stepping rate will also be higher.
  • angular positioning function that finds the shortest path from current angle to target angle
  • perpetual motion
  • timer interrupt driven
  • individual settings for acceleration and deceleration

Monday, August 7, 2023

Plotting data from a logic analyzer - CSV and LibreOffice Calc

Sometimes it is useful to visualize data from a logic analyzer in a graphical way but unfortunately not many applications can do that so I came up with a solution involving CSV data and LibreOffice Calc. Here I am using Logic from Saleae but any software that can export the data to a CSV file can work.

Plotting data from a logic analyzer

Usage

First thing to do is to export the data from the logic analyzer software to a CSV file format. In Logic 2 this can be easily achieved by going to File -> Export Data or by pressing CTRL + SHIFT + S.

Exporting CSV in Logic 2 from Saleae

Saturday, June 10, 2023

Breadboard Development Board for AVR microcontrollers | Breaduino

A development board is an essential tool when working with projects that involve a microcontroller and most of the time you will also need a breadboard and a power supply. So why not combining all these in a single product?

Meet Breaduino - a custom development board build around ATmega328PB microcontroller. If you are a beginner in electronics or microcontrollers, building a development board is not only rewarding but also educational.

Breadboard Development Board for AVR microcontrollers | Breaduino

The Breaduino dev board is designed to be used with two breadboards where it outputs three voltages: 12V, 5V and 3.3V. It includes a TFT display, micro SD card socket, input current measurement and a rotary encoder for user interaction. On the right side of the case is space for a small logic analyzer and/or a 9V battery. All these are optional modules. Even the breadboard is optional so the board can be used standalone. Since this is a customizable board it can be modified to fit your needs.

Breadboard Development Board for AVR microcontrollers | Breaduino

Sunday, August 28, 2022

IR remote control library for AVR microcontrollers

This library can be used for sending or receiving remote controller codes using a microcontroller. The supported protocols at the moment are NEC and RC-5 since these are the most commonly used.

The IR library is very easy to use and it needs a 16-bit timer for both sending and receiving. Timer 1 is used for this purpose. Before diving into the code, let's see how a remote controller works and take a closer look at their protocols.


 

Contents

 

How a remote controller works

A remote control is using optical communication to send wireless data to a receiver device. For this purpose, the infrared light was chosen. The data is transmitted on top of a carrier frequency that is usually 38kHz. There are many schemes of encoding the data because there are many manufacturers of consumer products. The most commonly used protocols are NEC, RC-5, RC-6, Sony. 

Sunday, August 7, 2022

UART library for AVR microcontrollers using interrupts

This is a UART library that is made for AVR microcontrollers that can be used for serial communications.

UART is a type of serial interface, as opposed to a parallel interface. A parallel interface can work at higher speeds but the disadvantage is that it needs multiple input/output lines. Other examples of serial interfaces are SPI and I2C.

UART library for AVR microcontrollers using interrupts

Features

  • Custom Baud rate
  • Asynchronous or synchronous modes
  • Supports serial frames with 5, 6, 7, 8, or 9 data bits
  • Odd, even or no parity
  • Error detection
  • Multi-processor communication mode used to address multiple devices on the same serial bus
  • Double speed asynchronous communication mode

Thursday, August 4, 2022

Tutorial on how to program an AVR ATmega328PB microcontroller using Atmel Studio and a bootloader

In this tutorial you will be learning how to use Microchip Studio (previously known as Atmel Studio) to program an AVR microcontroller over UART using the Optiboot bootloader. The hardware necessary is very inexpensive. All you need is an ISP (In System Programming) module such as USBTinyISP (around 3$) and an USB to Serial adapter that is around the same price and you can even build it yourself if you wish.