We all know about how AVR ATmega32A chip is clocked. In all our previous lessons we have used 1 MHz internal clock to drive AVR chip. This is default clock setting with which ATmega chip been shipped from factory. Here in this lesson we’ll learn setting AVR Fuse Bits to use different clock speed for various applications. It’s important where we require very accurate and precise timing say for example to implement serial UART, USB Communication. We have fuse bit’s in AVR Microcontrollers. We can use these fuse bits to clock microcontroller with desired frequency.
Let’s first understand the basic concept and ideas associated with setting up an clock source (either internal or external clock). I highly recommend opening up datasheet of ATmega32A (or any your microcontroller) for the duration of this tutorial, and briefly skimming the chapter on system clock and clock option (Page 25) click here for ATmega32A datasheet.
What are AVR Fuse Bits?
As we all knows the AVR microcontroller have in general, three memory areas: FLASH, which is dedicated to program code, SRAM for run-time variable and EEPROM, which can be used by user code to store data that have to be preserved when MCU is turned OFF. Now the lock/fuses together form a fourth memory area available for programming. This holds a few bytes for fuses: Low Byte, High Byte.
Before we begin to analyze fuse and lock bits. We have to keep in mind that a bit set to ‘0’ means programmed and a bit set to ‘1’ means un-programmed. This is little bit tricky and very important to be noted before we proceed any further.
In this tutorial we want to show, how you can clock your microcontroller with different frequency (setting avr fuse bits). Typically, there are only two fuse bytes: a high one and low one. We’ll be focusing more on its usage. The practical example in this tutorial will allow you to understand concept thoroughly and you’ll be ready to experiment further by your own. We’ll cover Lock bits in future lesson. The location of specific fuse bits differs among all the fuse bytes depending on AVR chip used. SO be sure to write them down before setting them. As example we take ATmega32A microcontroller and here are all fuse bytes for it.
The default value of ATmega32/32A fuse bit is 0x99E1 i.e. high fuse: 0x99 and low fuse: 0xE1 with this default setting frequency is set to 1 MHz, Internal RC oscillator, startup time:6CK+64ms:
Disclaimer: This tutorial is for informational purpose; do fuse bits settings at your own risk.
Explanation of Each Fuse Bit:
BODLEVEL: The trigger level of BOD (Brown-out Detection) can be selected by this fuse bit. When programmed (0) the trigger level is 4V and when not programmed (1) the trigger level is 2.7V
BODEN: ATmega32A has on chip brown-out detection (BOD) circuit for monitoring the VCC level during operation by comparing it to fixed trigger level. When the BOD is enabled (BODEN programmed), and VCC decreases to a value below the trigger level, the Brown-out reset is immediately activated. When VCC increases above the trigger level, it starts the microcontroller again.
SUT1:0: These bits select a startup time. The default value of SUT1:0 results in maximum startup time (6CK & 65ms additional delay from RESET [Ref. Table:8-9, Page No: 30].
CLKSEL [3-0]: These bits used to select different clock options available. The default value of CLKSEL[3..0] is 0001 i.e. internal RC oscillator running at 1 MHz If you want to add external crystal then you need to change value according to [Table 8-1, Page No:26]. Some common fuse bits value given in the end of this article.
OCDEN: This pin is used to enable or disable on chip debugging. This allows a real time emulation of microcontroller when running in target system. By default it is disable as ‘1’ means not programmed.
JTAGEN: There is built in JTAG interfaces for debugging. It is enabled in new microcontroller. This is reason why some newbie say, “PORTC of ATmega32 not working as expected “ disable it if you are not using JTAG by making JTAGEN bit 1 (high).
SPIEN: 0 value (programmed) means serial programming of ATmega32/32A enabled. Don’t change this unless you have parallel programmer! Because once disabled ATmega32A can’t be programmed using serial programmer.
CKOPT: The CKOPT fuse selects between two different oscillator amplifier modes. When CKOPT is programmed, the oscillator output will oscillate with a full rail-to-rail swing on the output. When not programmed, the oscillator has a smaller output swing. If you’re using external oscillator, it is better to program CKOPT i.e. CKOPT = 0
EESAVE: If programmed (0) it will save EEPROM from erasing during chip erase else EEPROM would also be erased with flash.
BOOTSZ0 and BOOTSZ1: These bits are used to set the size of bootloader
BOOTRST: If BOOTRST bit is programmed (0), The device will jump on first address bootloader block.
Example: Setting AVR Fuse Bits
At this point you’re ready to clock your microcontroller with different frequencies. In this example, we’ll be generating beep using Piezo disc. And impact will be shown in terms of change in beeps. Let’s calculate fuse bits, for different clock frequency. Here is a wonderful program to calculate fuse bits for you AVR Fuse Calculator
Case 1: 1 MHz Internal Clock (default), Low Fuse: 0xE1 High Fuse: 0x99
Case 2: 8 MHz Internal Clock, Low Fuse: 0xE4 High Fuse: 0x99
Case 3: 16 MHz External Crystal, Low Fuse: 0xFF High Fuse: 0x99
We hope this tutorial will help you to understand AVR Fuses. We recommend you to play around AVR chip to burn fuses and see impact. Don’t afraid to try on your own and Feel free to post a questions /comments below.
Here is a video, which is step-by-step instruction guide. Also impact been demonstrated through live demo: Setting AVR Fuse Bits