Many time we need to program our atmega with external programmer. This could be for even programming bootloader or to upload hex file in production. Today we are going to explain how to program atmega8 with external programmer. We are going to use AVR Dude which is also part of Arduino development environment. But we are going to use third-party GUI Application for this which is also open source. You can download AVRDUDESS here. You may use command line version if you feel comfortable but I will recommend using this GUI.

Understanding Lock bits

Fuse bits are always seems confusing. Lock bits are more confusing when it comes to program atmaga8 using external programmer. Lock bits are very important when we need code protection. In any microcontroller the most important part is how to protect our intellectual property? Which is obviously our written code or the data placed in EEPROM. For any microcontroller to be successful code protection is crucial feature. Mostly all microcontroller provide this feature but process became more and more complex according to the architecture of the microcontroller. Atmega8 provide two type of lock bits to protect code area. One type of bits are to protect code and eeprom data. The other part is to protect Bootloader related data.

Flash and EEProm Protection

According to the datasheet, there are two lock bits for code and EEPROM protection. LB1 and LB2 although they are two bits bit possible combinations are only three. The combination of LB2 programmed and LB1 not programmed is not supported. Please not here that making a bit HIGH(1) mean not programmed. If we make bit zero(0) it mean we had programmed that bit.

So in first mode when we make both bits 1. We left them not programmed. It mean there is no lock feature enabled and no protection is done. If we just program LB1 in second mode by making it zero. It means that we just disabled further programming FLASH and EEPROM. If we make both of these bits zero it means we had also disabled further programming as well as verification of the code through external programmer. One point to remember here is that we can still full erase microcontroller and use it just as before.

memory protection lock bits
Lock bits modes

Boot-loader Area Protection

The second provided group of protection or lock bits are for protecting boot-loader area. It forces SPM and LPM to stay away from Boot Loader section. It supports 4 modes and here is the table from datasheet explaining the behavior of these bits.

Lock bits for Bootloader area
Bootloader Area lock bits

If you press bit selector button on AVRDUDESS GUI then you will see following screen where you can individually select the fuse and lock bits.

Fuse bits selection screen in AVRDUDE GUI

Fuse Bytes

Atmega8 have two fuse bytes as you may see in above picture. These bytes are named with high fuse byte and low fuse byte respectively. We can program them accordingly and they specify a specific behavior. Here are tables taken from ATmega8 Datasheet

High Fuse byte

High fuse byte table
High fuse byte

Low fuse byte

Low fuse byte
Low fuse byte

You may need to program fuse bytes before you program lock bits in microcontroller. You need to be very careful when selecting and programming fuse bits.

By Abdul Rehman

My name is Abdul Rehman and I love to do Reasearch in Embedded Systems, Artificial Intelligence, Computer Vision and Engineering related fields. With 10+ years of experience in Research and Development field in Embedded systems I touched lot of technologies including Web development, and Mobile Application development. Now with the help of Social Presence, I like to share my knowledge and to document everything I learned and still learning.

One thought on “Lock bits of atmega8 using AvrDude”

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.