What is Fuse Bit & Lock Bits in AVR Microcontroller?





 Introduction

The fuse bit is a unique feature of ATMEL's AVR microcontroller. There are some fuse bits with specific meanings inside each type of AVR microcontroller, and their characteristics are EEPROM that can be erased and written multiple times. By configuring (programming) these fuse bits, the user can set some characteristics, parameters and I/O configuration of the AVR fixedly, and of course, it also includes the locking (encryption) of the running code on the chip. Fuses are an extremely important part programming a chip, but are rarely explained thoroughly. You only need to set them once, but if you don't do it right, it's a disaster!


 What is Fuse Bits?
You know about flash, eeprom and RAM as parts of the chip. What I did not mention is that there are also 3 bytes of permanent (by permanent I mean that they stick around after power goes out, but that you can change them as many times as you'd like) storage called the fuses. The fuses determine how the chip will act, whether it has a bootloader, what speed and voltage it likes to run at, etc. Note that despite being called 'fuses' they are re-settable and don't have anything to do with protection from overpowering (like the fuses in a home).Beginners are often puzzled about fuses. AVR chips use fuses to set the clock, startup time, enable some functions, BOOT area settings, and of course the security bit that is the most troublesome for beginners. Chips are cumbersome. In order to minimize the power consumption of the MCU, it is necessary to understand some bit settings.

 AVR microcontrollers’ memory consists of :

1-FLASH section that contains code
2-SRAM section for the runtime variables
3-EEPROM
4- lock/fuse bits for storing the MCU configuration.

ATMEGA328P fuse bit setting we are taking here?

        You can check this fuse setting using AVR website as given Link AVR® Fuse Calculator – The Engbedded Blog


Atmega328P Fuse bit setting 


you can set it using ARVDUDE  software  as  see in below screen  

  • Reset Disable   :  BOOTRST 

     This fuse turns the Reset pin into a normal pin instead of a special pin. If you turn this fuse on you cant program the chip using ISP anymore. I would suggest you never set this fuse unless you really mean to.

By default, chips that come from the factory have this turned off (that is, Reset is enabled)

       
  • Boot Size Fuses   :  BOOTSZ0,BOOTSZ1 
             The Boot Reset Fuse decides if the Reset Vector is placed in the first location of the Application section, or in the Boot section. Programming this fuse will place the Reset Vector in the first location of the boot section, selected by the BOOTSZ Fuses. The Boot Size Fuses select the size and start address of the Boot section. Boot section is only available in devices with SPM instruction.
    

  •  Brown-out Detect (BOD) : BODEN,BODLEVEL0,BODLEVEL1
         A “brown-out” happens when the voltage drops and your clock radio may stop working: the power voltage is too low to run reliably at the predefined. ex: ATMEGA128 can run as fast at 20MHz but only if the supply voltage is between 4.5V and 5.5V. If the voltage decreases below 4.5V, the AVR may behave erratically, erasing or overwriting the RAM and EEPROM, it may also start running random piece of the flash program, all in one an action with unpredictable results, nothing you would want your hardware to do. To keep it under control, set the brownout voltage to 4.3V. If the voltage goes below this threshold, the chip will turn off until the voltage returns. It will then reset and start over. If the chip is meant to run at 5V, set the brown-out to 4.3V. If the chip can run as low as 3.3V you can set the brown-out to 1.8V. If the chip is a ‘low voltage compatible’ chip such as theAtmega128 (which can run as low as 1.8V if its clocked at 4MHz or less) then you can set the brownout to 1.8V. You can read more in the datasheet.

By default, chips that come from the factory don't have brown-out detect however I suggest you set it if you can. If you have a bootloader or are storing data in the EEPROM you must set the BOD!

Its Works with    BODLEVEL0,BODLEVEL1,BODLEVEL2

      
 


  •     JTAG Interface Enable  :  JTAGEN 
             JTAG Interface you can enable and disable using bit 
          

  •   On Chip Debug Enable   :    OCDEN

           
            If the On-chip debug system is enabled by the OCDEN Fuse and the chip enter Power down or Power save sleep mode, the main clock source remains enabled. In these sleep modes, this will contribute significantly to the total current consumption.

  •     Serial program downloading (SPI) enabled :  SPIEN
 if this fuse is not enabled, the part must be programmed using high-voltage, and the SPI interface will not be usable (so no USBASP for you). Be careful about disabling this fuse, as without a high voltage programmer, your AVR might become unusable. Also see the Fuse bit doctor below, to learn how to reset the fuses and “revive” your avr.

  •     Preserve EEPROM memory through the Chip Erase    cycle :  EESAVE
                The EEPRPOM memory is preserved during chip erase if the EESAVE fuse is programmed

  •  Watchdog timer always on:  WDTON
   
            The Watchdog Timer is clocked from a separate On-chip Oscillator which runs at 1MHz. This is the typical value at VCC = 5V.  By controlling the Watchdog Timer prescaler, the Watchdog Reset interval can be adjusted

  •  Clock output on PORTE7:  CKOUT
         (available for some AVRs only) produces a square wave of the same frequency of the clock input (internal, external, crystal, etc) on a given pin (D2, B0, etc). This is useful if you’re debugging the clock rate, or if you want to use the clock to drive another chip.

  •  Clock selection  :  CKSEL0,CKSEL1,CKSEL2,CKSEL3
The first option is how the chip is clocked. Every CPU uses a clock, The clock keeps track of time for the chip, in general one assembly code instruction is run every clock cycle.

External Clock means that a square wave is being input into the CLOCK-IN pin by using a clock generating chip. This is uncommon and usually you won’t be needing it.

Internal Clock sets the avr to use the internal oscillator that is not very precise but good for most projects that don’t have fine timing requirements. You should know that this clock varies with temperature and the power supply voltage. You can chose from a 8MHz, 4MHz or 128KHz clock. The 128KHz clock is for very low power applications where the low speed helps conserve power. By using the internal clock, you don’t need to wire up a crystal so we can use the XTAL1 and XTAL2 pins for various other purposes.

The Clock Source can be either of the following:
External Clock, Internal 8MHz clock, Internal 4MHz clock, Internal 128KHz clock, External Crystal (0.4-0.9 MHz), External Crystal (0.9MHz - 3.0MHz), External Crystal (3.0MHz - 8.0MHz) 
or External Crystal (8.0MHz +)

External Crystal option is used when you need a special clock rate, like 8MHz or 12MHz or a high precision clock that will not vary with temperature or power supply voltage fluctuations. In this case you need to connect an external crystal or oscillator.



  •  Select Start Up Time : SUT0, SUT1
        The Startup Time is just how long the clock source needs to calm down from when power is first applied. Always go with the longest setting 14CK + 65ms unless you know for a fact your clock source needs less time and 65ms is too long to wait.
By default, chips that come from the factory have the Internal 8 MHz clock with 14CK + 65ms Startup.


The Clock Startup can be either of the following:
14CK + 0 ms, 14CK + 4 ms, 14CK + 65 ms
.


  •   CLOCK DIVIDE :   CKDIV8

         (available for some AVRs only), causes the chip to divide the clock rate by 8. So if the clock source is set to Internal 8MHz and you have this fuse set, then you’ll really be running at 1MHz. By default, this is turned on.

Default Clock Source 

The device is shipped with CKSEL = “0001” and SUT = “10”. The default clock source setting is therefore the Internal RC Oscillator with longest startup time. This default setting ensures that all users can make their desired clock source setting using an In-System or Parallel Programmer.

 LOCK BITS :

All AVR devices contain two Lock bits named LB1 and LB2. Programming these (“0”) will add protection to the contents written to Flash and EEPROM memories according to
Table 1 below. The level of protection is divided in three modes, where mode 1 offers no
protection and mode 3 offers maximum protection. It is possible to move to a higher
mode of protection simply by reprogramming the Lock bits. The AVR allows changing
"high" bits to "low", but not the other way around. It is not possible to change a "low"
Lock bit to a "high", thus lowering the level of protection is not possible. To clear the
Lock bits, a complete Chip Erase is required, which erase the Flash memory.

  
In addition to LB1 and LB2, AVR devices with Self-Programming capabilities will have
four additional Lock bits: BLB01, BLB02, BLB11, and BLB12. These bits control the
restrictions on the use of the LPM and SPM instructions