Recent Posts

Pages: 1 2 3 4 [5] 6 7 8 9 10
41
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.




42
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.
43
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.
44
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
45
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.
46
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).
47
Hi Miguel,
Thanks, I'll post future questions like this in the Embedded Forum.
48
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.
49
The "Embedded development" subforum is more suitable, but your question should receive more answers in an AVR-specific forum.
50
Hi Miguel,
In which section would AVR programming not be OT?
And thanks again for the prompt response.
Regards,
mikejp56.
Pages: 1 2 3 4 [5] 6 7 8 9 10