About to set fuse bits for the first time

Go To Last Post
12 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

It has been a great month of transitioning from the Arduino to a standalone ATmega8 for me!

I am finally ready to try setting fuse bits on my AVR, so that I can use an external clock source. I have read a lot about this procedure, and I think I am ready. I would, however, appreciate an expert to double check my understanding (as I have read lots of stories of people bricking their beautiful AVRs).

So here are some details:

- I have an ATmega8-16PU (datasheet link: http://www.atmel.com/images/doc2...)
- I want to try to implement V-USB, so I require an external crystal to set the clock speed above what the internal clock offers
- I have chosen to use a 16MHz crystal
- I will connect the crystal to XTAL1 and XTAL2 with 22pF ceramic capacitors

Here are details about my understanding of setting the fuse bits:

- CKOPT will be set to 0 (programmed)
- CKSEL0 will be set to 1
- CKSEL3..1 will be set to 111 (datasheet says 101, 110, or 111 ... any reason to choose one over the other?)
- SUT1..0 will be set to 11 (to maximize start up time)

Now, I am not sure what convention is followed with my AVR when it comes to settings bits. At the top of page 217 of the datasheet, it gives a table for the low fuse byte. I don't know if it is following LSB0 or MSB0, and I don't want to feed a reversed value into the byte. Can someone help me with that, or better yet, help me find that info in the datasheet?

So I know the following for the low fuse byte (non bold means that is the default value):

BODLEVEL = 1
BODEN = 1
SUT1 = 1
SUT0 = 1
CKSEL3 = 1
CKSEL2 = 1
CKSEL1 = 1
CKSEL0 = 1

And for the high fuse byte:

RSTDISBL = 1
WDTON = 1
SPIEN = 0
CKOPT = 0
EESAVE = 1
BOOTSZ1 = 0
BOOTSZ0 = 0
BOOTRST = 1

Last but not least, this is the command I will enter into avrdude. Note, this is under the assumption that the format is LSB0:

avrdude -c usbasp -p m8 -U lfuse:w:0b11111111:m
avrdude -c usbasp -p m8 -U hfuse:w:0b11001001:m

Phew, that was fun. I would really, really appreciate any feedback. Thank you!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As always it's probably worth reading Recovering from a "locked out" AVR to know what you might be up against.

The key thing really is don't change RSTDISBL so always read fuses first, see how your programming software represents them then only change the state of selected bit (avoiding RSTDISBL) then write back,

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
As always it's probably worth reading Recovering from a "locked out" AVR to know what you might be up against.

The key thing really is don't change RSTDISBL so always read fuses first, see how your programming software represents them then only change the state of selected bit (avoiding RSTDISBL) then write back,

Thanks! How do I change only the bits I select though?

Reading through your linked post now, looks great!!

Last Edited: Mon. Aug 27, 2012 - 07:01 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well say you read high fuse and it is 0xCB then the key thing is that bit 7 is never changed to 0 so don't program any value below 0x80. If you wanted to change the state of bit 1 then 0xCB would become 0xC9

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Well say you read high fuse and it is 0xCB then the key thing is that bit 7 is never changed to 0 so don't program any value below 0x80. If you wanted to change the state of bit 1 then 0xCB would become 0xC9

Oh I see what you mean. I thought there was some special way to program only a specific bit.

How about if I use binary values instead of hex to set the fuse bits? See the code at the bottom of my first post in this thread. I find that approach much easier visually, and harder to make mistakes with.

What do you think?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi capcom,

Before I forget... In case you haven't encountered it yet, you should check out the Engbedded Fuse Calculator site/app. It's very helpful for getting the proper fuse setting values and even gives you the appropriate AVRDUDE command-line options to do so -- very helpful and convenient! :)

I've never used V-USB on ATMega8 MCUs, but I've used it successfully on ATMega16 and other MCUs. I checked your fuse settings and they all seem fine to me. For reference only, here is the AVRDUDE command I use to program (with my AVRISP2 programmer hardware, not your USBASP device!) an ATMega16 MCU's fuses for V-USB operation (with a 12 MHz external crystal):

avrdude -p atmega16 -c avrisp2 -P usb -U lfuse:w:0xff:m -U hfuse:w:0xC9:m

If you plug those hex fuse settings into the Engbedded website with "ATmega16" selected, it should help confirm that your settings will work with V-USB.

I'd actually never used the binary format when setting fuses, but a readback of my fuses with binary output format enabled convinced me that your LSB assumption is (unsurprisingly) correct, FWIW.

Although using binary format is undoubtedly convenient now, while you're (wisely) being very careful with fuse settings, I suspect that you may find using hex format will be easier as you gain confidence, especially if you use the Engbedded Fuse Calculator app much.

Good luck with your fuses and with V-USB and don't hesitate to ask for help with either if you encounter difficulties.

Regards,
Bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi Bill,

Thanks for your kind answer, I really appreciate it. Yes, I do see the convenience factor associated with using hex values, and will definitely use them in the future.

Thanks for that link too. I was using it initially, but realized I had no idea what any of the words meant. So I took a leap of faith and spent a day and half reading the relevant aspects of the datasheet (I am new to electronics, and I find datasheets a little daunting at the moment). Turns out, it was really helpful, and I feel I have a pretty good understanding of how fuses and clocks work now! But I digress.

Just to let you guys know, I successfully changed the fuse bits, and everything seems to be perfect! Woohoo! I have it running at 16MHz.

On to the next step with V-USB.

I think I will write up a little tutorial on my blog to help newbies understand clock settings, fuse bits, and the associated concepts once I gain a little more confidence.

Thanks again Bill and Clawson!

P.S. For future readers of this thread, FWIW here is the avrdude command I used:

avrdude -c usbasp -p m8 -U lfuse:w:0b11111111:m -U hfuse:w:0b11001001:m
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Here's a link to a little tutorial I created to help others out with this procedure:

http://www.treehouseprojects.ca/...

Enjoy!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Glad to hear it all worked out, capcom.

Nice tutorial too! A couple of trivial corrections:

  • "(16 MHz is < 1 MHz)" -- obviously, you mean ">" there :); frankly, I think the weird way that the datasheet states this test lends itself to confusion, but maybe that's just me :)
  • "blink at200 milliseconds" -- missing a space in there
Glad to see you're already contributing to the cause of educating new AVR users -- nice work!

Regards,
Bill

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey Bill,

Thanks, I'm very glad too!

And thanks a lot for pointing out those silly mistakes! I didn't get enough time to edit it yet, but really appreciate you pointing them out.

I hope to write tutorials of greater significance than this one in the near future too!

Last Edited: Wed. Aug 29, 2012 - 11:45 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I hope to write more useful tutorials in the near future too

If you do consider adding them to the Tutorial Forum here. While some tutorials there just have a link to another site we discourage that as sites die after a few years some times. better is to format the text the way you want then print to PDF (CutePDF will do this in the absence of a better alternative) and you can then attach such a PDF to a post in the Tutorial Forum.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
Quote:

I hope to write more useful tutorials in the near future too

If you do consider adding them to the Tutorial Forum here. While some tutorials there just have a link to another site we discourage that as sites die after a few years some times. better is to format the text the way you want then print to PDF (CutePDF will do this in the absence of a better alternative) and you can then attach such a PDF to a post in the Tutorial Forum.

I sure will! I don't think the one I wrote is worth much to most people on AVR Freaks (so I didn't consider posting it there), but I thought it would be a good idea to document my experience anyways, whilst getting some practice in tutorial writing.

When I write tutorials in the future, I will definitely follow your advice, thanks!