Recent Posts

Pages: 1 ... 4 5 6 7 8 [9] 10
81
Embedded development / Re: Setting AVR Fuses in C code
« Last post by mikejp56 on April 11, 2024, 02:24:32 pm »
Hi Pecan,
That is what I am looking for. Setting the fuses in the c code. I was not aware of the Fuses struct.
Where might I find docs of that sort?
Thanks again.
mikejp56
82
Embedded development / Re: Setting AVR Fuses in C code
« Last post by Pecan on April 11, 2024, 07:07:46 am »
User
Did you make up this solution or is it actually true?

ChatGPT
The solution I provided is based on typical Atmel AVR programming practices and should work for setting the low fuse to 0xFF to use an external crystal oscillator with an Atmega328P microcontroller. The code structure and the fuses settings are common in AVR programming.

Here's a breakdown of the provided code:

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

// Default fuses
struct Fuses {
    uint8_t low;
    uint8_t high;
    uint8_t extended;
};

Fuses fuses_default = {
    .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 fuses_modified = {
    .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;
}
The Fuses struct is used to define the low, high, and extended fuse settings.
fuses_default contains the default fuse settings.
fuses_modified contains the modified fuse settings with the low fuse set to 0xFF.
This code should be compatible with the AVR GCC toolchain, which is commonly used for Atmel AVR programming.

However, when changing the fuse settings, it's crucial to be cautious and ensure that the settings are appropriate for your specific hardware setup and requirements. Incorrect fuse settings can prevent the microcontroller from working correctly.

You'll also need to use a programmer like AVRISP mkII or USBasp and a software like avrdude or Atmel Studio to set the fuses on your Atmega328P microcontroller.

Always make sure to double-check the fuse settings and consult the Atmega328P datasheet and the fuse bit settings in the avr-libc documentation to ensure the correct configuration for your needs.
83
Embedded development / Re: Setting AVR Fuses in C code
« Last post by Pecan on April 11, 2024, 06:57:59 am »
Be careful. Validate the code before using it.
Chatty can make things up ya know.

User
Can you translate that to c++
ChatGPT
Certainly! Here's the code translated to C++:

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

// Default fuses
struct Fuses {
    uint8_t low;
    uint8_t high;
    uint8_t extended;
};

Fuses fuses_default = {
    .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 fuses_modified = {
    .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 C++ version, I've used a struct to define the Fuses and initialized two instances: fuses_default and fuses_modified. The .low, .high, and .extended members of the Fuses struct correspond to the low, high, and extended fuse bytes, respectively.

The rest of the code remains the same. Make sure you have the AVR GCC toolchain installed to compile and upload this C++ code to your Atmega328P microcontroller.




84
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.
85
Embedded development / Re: Setting AVR Fuses in C code
« Last post by stahta01 on April 11, 2024, 02:47:09 am »
You know what compiler you are using! I do not! Therefore, I cannot help you.

Tim S.
86
Embedded development / Re: Setting AVR Fuses in C code
« Last post by mikejp56 on April 11, 2024, 02:07:24 am »
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
87
Embedded development / Re: Setting AVR Fuses in C code
« Last post by stahta01 on April 11, 2024, 01:58:15 am »
FYI: You likely are having problems with the compiler syntax instead of with Code::Blocks syntax.

Tim S.
88
Embedded development / Setting AVR Fuses in C code
« Last post by mikejp56 on April 11, 2024, 01:37:27 am »
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).
89
Hi Miguel,
Thanks, I'll post future questions like this in the Embedded Forum.
90
Plugins development / Re: Code completion using LSP and clangd
« Last post by kakas on April 10, 2024, 11:23:55 pm »
I should be the one thanking you for this amazing plugin ;D

In the coming Nightly, you will also be able to Alt-LeftMouseClick on the red/green warning/error box in the margin to apply any suggested fix.
Hmmm... I might have to keep an eye out for nightly build after this.
Pages: 1 ... 4 5 6 7 8 [9] 10