Showing posts with label Programming AVR. Show all posts
Showing posts with label Programming AVR. Show all posts

Wednesday, January 24, 2024

Defining F_CPU in Microchip Studio

The macro F_CPU specifies the CPU frequency and is used by libraries such as <delay.h> to calculate the delay based on the frequency of the processor or by UART libraries to calculate baud rate.

Up until now I used to define F_CPU like this:

#define F_CPU    16000000UL

Defining this way will work but it could lead to issues and confusion when you have multiple files that define this macro. The ideal way is to define it in a single place. This could be a Makefile if you are using custom Makefiles or in the IDE project configuration.

Defining F_CPU in Microchip Studio

Open the project properties window in Project -> Properties (Alt+F7) then in the left panel select the Toolchain menu. Next in AVR/GNU C Compiler -> Symbols use the green + button to define a new symbol F_CPU=16000000UL. Of course the F_CPU value depends on your CPU frequency that in this example is 16000000 Hertz or 16MHz. The UL at the end stands for unsigned integer.

Defining F_CPU symbol in Microchip Studio
click to enlarge

That's it. Now all included files in your project will use a single F_CPU value without giving errors such as "F_CPU is not defined", "F_CPU redefined", etc.

Since this method of defining is not as obvious as the first one, it is easy to forged to do it when starting a new project but there is a simple solution for that - using preprocessor conditionals.

#ifndef F_CPU
	#warning	"F_CPU not defined. Define it in project properties."
	
#elif F_CPU != 16000000
	#warning	"Wrong F_CPU frequency!"
#endif

This macro can be placed at the beginning of the main.c file before any type of code. If the F_CPU is not defined it will output a warning. If the F_CPU is defined but is not the value specified in the macro, it will also output a warning. This is also helpful to see what the CPU frequency is and to ensure that the defined value in project properties is correct.

Thursday, January 4, 2024

How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer

After buying some ATtiny402 microcontrollers I've noticed that newer AVR models from Microchip are now using the UPDI interface for programming. Previously the programming was done using SPI or UART if a bootloader was present.

How to program an UPDI AVR microcontroller using avrdude and USB to serial programmer
 

Contents

 

What is UPDI and how do I use it?

UPDI stands for Unified Program and Debug Interface and is proprietary to Microchip. In many ways is similar to 1 wire UART. The main advantage is that it can be used for programming and also for debugging. Now there is no need for SPI, bootloader, debugWire... It's all Unified in one pin and one interface.

On certain devices such as ATtiny, the UPDI and Reset are on the same pin. In each case, the UPDI pin can also be used as a GPIO pin. When UPDI and Reset share the same pin, the functionality can be selected using the specific fuse. By default the fuse is set to select UPDI as a pin function. If you change the fuse and enable the Reset then you will need a 12V programmer to be able to program the microcontroller.

To prevent false triggering when the line is idle, it is recommended to have a pull-up resistor of at least 10k on the UPDI pin. Although some say that it works without problems even without a pull-up resistor so if you are using the UPDI pin also as a GPIO pin, you might consider not placing the resistor.

Source: https://microchip.my.site.com/s/article/AVR---Hardware-Design-Considerations-for-UPDI-pin.

Can i manually reset the microcontroller when UPDI is shared with RESET? 

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.

Thursday, January 7, 2021

ISP programming rig for microcontrollers

I had to program many types of microcontrollers over the years and so I was thinking why not build a simple programming rig to make things easier. This rig is for the In-system programming (ISP) method and not for the UART method.

How to easily upload the code to any microcontrollers using ISP rig

The board in the above image is not the programming rig but a digital clock shown as an example. Notice the 6 pin header near the cap. The pins are not soldered through hole but on SMD pads. This way it can be easily de-soldered after finishing the project and on some space constrained projects this is a must.

Monday, September 28, 2020

Using USBTinyISP programmer with Atmel Studio 7 | AVR programming

In the last article I talked about Programming any AVR microcontrollers using WinAVR and USBTinyISP but recently I found that USBTinyISP can be easily used together with Microchip Studio.

For microcontrollers that are using UPDI interface for programming, I have another tutorial here.

Using USBTinyISP programmer with Atmel Studio

Using an external programmer in Microchip Studio 7

Friday, September 4, 2020

Program any AVR microcontroller using WinAVR and USBTinyISP - Getting started with AVR tutorial for beginners

Nowadays Arduino is the platform of choice for programming AVR microcontrollers and for good reasons. But there are times when you want to have full control over what is added to your code. 

For example Arduino is enabling by default Timer0 for use in millis function and other functions and includes some interrupt routines that perhaps your project is not using and so adding to the code size or perhaps those interrupts can interfere with your code.Two main alternatives are WinAVR and Atmel Studio. This tutorial covers WinAVR because it's simpler to use for a beginner.

What you will need:

  • ATmega328P (used in this tutorial as an example)
  • USBTinyISP programmer 
  • WinAVR software (more on this later)

What you can learn:

  • how to program an AVR microcontroller using an In-System Programmer such as USBTinyISP and WinAVR
  • some bitwise operations for handling the registers

There are two main ways to program a microcontroller:

- ISP (In System Programming) using SPI protocol and a ISP programmer
- With a bootloader using UART protocol and a USB to Serial programmer. Some microcontrollers come with a bootloader already pre-programmed on them.
 
This tutorial will cover the ISP programming way.

Programming software

There are many tools for programming an AVR microcontroller such as Atmel Studio, PlatformIO, Eclipse with an AVR plugin, etc but the simplest and light weight solution that I found is using WinAVR.

Programming hardware

Apart from development software there is also the need of a hardware programmer that the software uses to communicate with the microcontroller and upload the code to it. Searching online for 'avr programmer' reveals lots of options. The most popular I believe is the USBTinyISP and is very cheap. There is also Atmel-ICE from Atmel. A bit more expensive but it has the benefit of being able to debug and see in real time what happens inside the microcontroller.


WinAVR and USBTinyISP - Getting started with AVR


Getting started with WinAVR