User forums > Embedded development

Setting AVR Fuses in C code

(1/2) > >>

mikejp56:
Hi All,
I am pretty new to CodeBlocks and Atmega 328P programming.
I have a simple code that blinks and LED and uses the default fuse settings for the 3 fuses in fuse.c. I would like to change the lfuse setting from 0x62 to 0xFF so I can use a crystal. How would I change ".low=LFUSE_DEFAULT" to use 0xFF? I have tried multiple things; .low=0xFF, etc. and I keep getting errors for the setting. I can change the lfuse setting in AVRDUDESS and AVRDude and the crystal setting works. I just cannot figure out the syntax for CodeBlocks.
By the way this is an awesome program for a non-programmer to learn on!
Thanks and regards.
mikejp56

PS Sorry if this is a cross post. I was advised that the original post was in the wrong place (General).

stahta01:
FYI: You likely are having problems with the compiler syntax instead of with Code::Blocks syntax.

Tim S.

mikejp56:
Hi stahta01,
Thanks for that. I tried googling but I came up pretty empty.
What I did figure out was that I need to put Fuses = and then some braces and then I cannot figure out how to enter the fuse parameters and the correct syntax. Any guidance would be greatly appreciated.
Thanks.
Regards,
mikejp56

stahta01:
You know what compiler you are using! I do not! Therefore, I cannot help you.

Tim S.

Pecan:
Does this make any sense?

--- Quote ---User
I am pretty new to CodeBlocks and Atmega 328P programming.
I have a simple code that blinks and LED and uses the default fuse settings for the 3 fuses in fuse.c. I would like to change the lfuse setting from 0x62 to 0xFF so I can use a crystal. How would I change ".low=LFUSE_DEFAULT" to use 0xFF?
ChatGPT
In Atmel AVR programming, the fuses control various settings of the microcontroller, including clock source, brown-out detection, and more. To set the low fuse to use an external crystal oscillator, you'll want to set the lfuse to 0xFF.

Here's how you can modify the .low=LFUSE_DEFAULT to set the lfuse to 0xFF:

c
Copy code
#include <avr/io.h>
#include <util/delay.h>

// Default fuses
FUSES = {
    .low = (LFUSE_DEFAULT & ~FUSE_CKDIV8 & ~FUSE_SPIEN & ~FUSE_BODLEVEL1 & ~FUSE_BODLEVEL0),
    .high = (HFUSE_DEFAULT & ~FUSE_BOOTSZ1 & ~FUSE_BOOTSZ0 & ~FUSE_SPIEN & ~FUSE_EESAVE),
    .extended = EFUSE_DEFAULT,
};

// Modified fuses
FUSES = {
    .low = 0xFF, // Sets the lfuse to use an external crystal oscillator
    .high = (HFUSE_DEFAULT & ~FUSE_BOOTSZ1 & ~FUSE_BOOTSZ0 & ~FUSE_SPIEN & ~FUSE_EESAVE),
    .extended = EFUSE_DEFAULT,
};

int main(void) {
    DDRB |= (1 << PB5); // Set PB5 (Arduino pin 13) as output

    while (1) {
        PORTB |= (1 << PB5);  // Turn on LED
        _delay_ms(1000);     // Delay for 1 second

        PORTB &= ~(1 << PB5); // Turn off LED
        _delay_ms(1000);     // Delay for 1 second
    }

    return 0;
}
In this code snippet, I've provided both the default and the modified fuses for your Atmega328P. The FUSES struct is used to define the fuse settings for the microcontroller.

Make sure to include <avr/io.h> for AVR specific I/O definitions and <util/delay.h> for the _delay_ms() function.

Also, ensure that you have the appropriate toolchain set up to program your Atmega328P with the new fuse settings. You'll need a programmer like AVRISP mkII or USBasp and a software like avrdude or Atmel Studio to set the fuses.

--- End quote ---

Navigation

[0] Message Index

[#] Next page

Go to full version