Atmega328p, Unable to set default fuses (AVRDUDE)

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

So i've been working through the "Make: AVR Programming" book, and there is a section (Section 5 with the UART sample program) that I was getting really goofy Serial data back from the AVR. I did some digging and it looked like it was probably some mismatch with what my CPU frequency was (For reference the SE question was asked here: Serial Output returns wrong ASCII)

Anyways, the book itself doesn't use a 16mhz Crystal and I had probably bought this atmega with a preloaded bootloader or something........because i've been programming it with AVRDUDE and the 16mhz Crystal for the past programs. Taking out the Oscillator gives the initialization failed, rc=-1 error.

 

So my guess was..ok I need to set the efuse (or maybe it was lfuse?), since everything lined up besides that one (it was showing 0x07 and I was trying to set it to 0xFF as shown by the "Default" Atmega328p fuse calculator: http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega328p)

Despite that I would get an error here back from AVRDUDE:

avrdude: verification error, first mismatch at byte 0x0000 0xff != 0x07

So im probably a bit lost. FWIW the "UART.c" file is defined here: https://github.com/hexagon5un/AVR-Programming/blob/master/AVR-Programming-Library/USART.c(The other code is in chapter 5 "serialLoopback.c"). Also I made sure baudrate was 9600. But from looking at that other post....im wondering that it's maybe my CPU Frequency was off?

Basically I think I need to make it where:

 

  1. I can program without the Crystal , since the book uses the internal crystal (Im using the Pocket AVR programmer from Sparkfun: https://www.sparkfun.com/products/9825)
  2. Figure out what I need to set my CPU Frequency to?
  3. Figure out why I can't set the fuse manually

 

I admit that I am still new, so fuses im probably not as experienced on. But this is just what i've tried from my research.

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

I fixed it by just setting the F_CPU to 1000000UL and changing the code a bit according an answer found on the web. I guess that leads me to the question: If it's programmed to use the 16mhz crystal and I set the F_CPU to 10mhz....does that actually work? How exactly does it just "reduce" it's speed. I assume use some sort of internal divider or something? I didn't think that would work with an external crystal?

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

The short answer is that you aren't really using the 16MHz.  [which is quite different than 16mHz]  Look in the datasheet for Default Clock Source.

Mercfh wrote:
If it's programmed to use the 16mhz crystal

Yes, if it >>is<< "programmed" to use the crystal.  This involves setting fuses for clock source, as described in the datasheet.  You can take your infinite-value toolchain with its free compiler and set F_CPU to whatever you want -- but the AVR chip doesn't know about that.  (If you set F_CPU to 123 will your AVR run at 123Hz?)

 

I have no idea what "unable to set default fuses" means.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

Last Edited: Tue. Oct 30, 2018 - 04:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Start by showing us your setup, post a clear picture, are you using a breadboard and bare chip or some commercial board (arduino uno?)

What are the current fuse settings?  H:  L:  E:

 

Getting USART comms to work is fairly simple if you are using a xtal.   Also, how are you looking at the serial I/O?   USB serial TTL cable?

 

Jim

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

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

F_CPU doesn't set the speed. It informs avr-libc of the speed you are running at. If your code runs fine with  F_CPU=1000000UL, this means you are running at 1MHz, therefore the fuses are set to use the internal oscillator (8MHz) with 8x divisor.

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

Im using a bare breadboard and the Sparkfun AVR Programmer. I had pulled the chip off an old arduino so that's why I assumed it was running at 16MHz. How would I find out what my fuses are set to? (I'll update once im at home)

 

Also the other reason I assumed it was running at 16MHz is that I have an external oscillator (16MHz one) attached, and im unable to actually program to the chip unless it's attached.

 

I will take a picture of the breadboard when I get home.

Last Edited: Tue. Nov 6, 2018 - 04:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Mercfh wrote:
Also the other reason I assumed it was running at 16MHz is that I have an external oscillator (16MHz one) attached,

 

No it has an external xtal w/ two caps or external resonator w/caps built in, the chip will not run with out this part unless you "inject" a clock from an external source into xtal1 pin.

Once you do that, you can erase the chip and reset fuses to factory default so it runs on the internal R/C osc.

 

Jim

 

 

Click Link: Get Free Stock: Retire early!

share.robinhood.com/jamesc3274

 

 

 

Last Edited: Tue. Nov 6, 2018 - 05:05 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well, if you can program, then you can also read the fuses, with avrdude and your programmer. Are you using avrdude for programming?

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

I am using AVRdude for programming, btw this is what my board looks like:

 

https://imgur.com/a/CJKeO82

 

the Metal Cap says "SD16.00000" on the top, which I assume to be 16MHz. When I program my code via the Sparkfun programmer, AVR reads out as: (This is an Atmega328p)

 

avrdude.exe: safemode: lfuse reads as 62
avrdude.exe: safemode: hfuse reads as D9
avrdude.exe: safemode: efuse reads as 7
avrdude.exe: safemode: Fuses OK

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

So now you go to a fuse calculator like this: http://www.engbedded.com/fusecalc/ and see what those fuses mean.

I'll tell you: these are factory defaults (probably a brand new chip you just bought), the 328p is running from the internal oscillator (8MHz) divided by 8, as I suspected in post #5. So you shouldn't need the crystal to program the chip.

 

edit: Regarding your original post, note that efuse = 0x07 or 0xFF is the same, because for this chip and fuse only the low 3 bits matter.

Last Edited: Wed. Nov 7, 2018 - 09:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Maybe thats where I get screwed up, I kept seeing people have 0xFF and thought mine was messed up. FWIW it worked without the external crystal so that's good...not sure why it didn't work last time.

Last Edited: Thu. Nov 8, 2018 - 03:48 PM