61
Embedded development / Re: Setting AVR Fuses in C code
« Last post by Pecan on April 11, 2024, 06:57:14 am »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.