Is it possible to store the float type results obtained from the code calculation in FLASH memory?

135 views Asked by At

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.

1

There are 1 answers

3
Mathieu On

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)