1

I am using Attiny1616 with Microchip Studio.

I want to store the float type results obtained from the calculation of the code in FLASH memory.

I have looked into PROGMEM, but PROGMEM does not support the float data type.

So I tried to convert the float type result to char type data using snprintf or dtostrf and save it, but it did not work.

This is my C code.

#include <avr/io.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {

    const char test[] PROGMEM = "Hello, world!";

    char buf[5] = "";
    float cal_result = 10.83;
    
    //dtostrf(cal_result, 5, 2, buf);
    snprintf(buf, 6, "%f", cal_result);

    const char out[5] PROGMEM = {buf[0],buf[1],buf[2],buf[3],buf[4]};
    
        while(1){   
    }

}

Thank you in advance.

7
  • You can write the byte representation of the float to flash. You are creating a string representation of the float which is not what you want. Commented Jan 10, 2024 at 11:34
  • @Takeru Tsuji, So const float PROGMEM cal_result = 10.83f; fails for you? Commented Jan 10, 2024 at 11:46
  • 1
    @chux-ReinstateMonica he wants to write a value from a calculation during runtime, PROGMEM wont help. He will need to erase a flash sector then write the float to flash. Commented Jan 10, 2024 at 11:48
  • 2
    Do you really need to use float? It will be very inefficient to do calculations with floating point types on low end 8-bit MCU. Most calculations can be done in pure integer types with little bit of adjustment. Commented Jan 10, 2024 at 13:50
  • 2
    Your MCU does not have a FPU so all floating point will get calculated by software floating point libraries. That's super-inefficient, similar to using *printf family of functions. This little code you posted has likely eaten up some 90% of your AVR's resources. Don't use PC stuff when programming ancient microcontrollers. Or alternatively, pick a modern microcontroller instead. Commented Jan 10, 2024 at 13:51

1 Answer 1

2

What you want to do is not possible with PROGMEM.

PROGMEM is a compiler directive that prevent to use RAM to store a constant value. Instead the program will read it directly from the flash each time it need it (I ignore optimizations here)

See 6.2 memory map on datasheet

  • A variable will be stored in 0x3800 – 0x3FFF range
  • A PROGMEM constant value will stored in 0x8000 – 0xBFFF range

If you want to store data computed during the program is running, you will have to use:

  • eeprom (low space, quite easy to use), see avr/eeprom.h
  • flash (more space, difficult to use (can only be written by page, has a limited number of writing), dangerous to use (you can wipe out your program accidentally)
Sign up to request clarification or add additional context in comments.

3 Comments

Is there any other way to write to Flash memory other than PROGMEM? If there is a way, can you provide the URL or an example of the code?
@TakeruTsuji you will have to do some reading, but that is common when dealing with embedded development ww1.microchip.com/downloads/aemDocuments/documents/OTH/…
I read the datasheet, but I couldn't understand it very well. I tried writing some code, but it doesn't seem to be able to write data to FLASH. ''' CCP = 0x9d; NVMCTRL.CTRLA = 0x06; NVMCTRL_ADDR = 0x8E00; NVMCTRL.DATA = 0xABCD; CCP = 0x9d; NVMCTRL.CTRLA = 0x04; '''

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.