How to Store Float Calculation Results in FLASH Memory on PIC16F877A Using MPLAB X IDE?

Hello everyone, I’m currently trying to work on a project with a PIC16F877A microcontroller using MPLAB X IDE, and I’ve run into a bit of a challenge. I need to store the results of floating-point calculations in the program memory (FLASH). I thought the __flash qualifier might be the solution, but it turns out it doesn't directly support float data types. To get around this, I tried converting the float results into strings using sprintf and ftoa, hoping to store them in FLASH that way. Unfortunately, it hasn’t worked as expected. This’s my code:
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

void main(void) {
const char test[] __flash = "Hello, world!";

char buf[8] = "";
float cal_result = 25.67;

// ftoa(cal_result, buf);
sprintf(buf, "%.2f", cal_result);

const char out[8] __flash = {buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]};

while(1) {
// Main loop
}
}
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>

void main(void) {
const char test[] __flash = "Hello, world!";

char buf[8] = "";
float cal_result = 25.67;

// ftoa(cal_result, buf);
sprintf(buf, "%.2f", cal_result);

const char out[8] __flash = {buf[0], buf[1], buf[2], buf[3], buf[4], buf[5], buf[6], buf[7]};

while(1) {
// Main loop
}
}
Does anyone have any suggestions on how I can successfully store float results in FLASH memory on this microcontroller? Or is there a better approach for managing this type of data?
Solution:
Storing floats directly in FLASH on the PIC16F877A is tricky , consider scaling your float to an integer and store it in FLASH. This saves memory and avoids the complexities of storing non-supported data types directly.
Jump to solution
4 Replies
Solution
wafa_ath
wafa_ath3mo ago
Storing floats directly in FLASH on the PIC16F877A is tricky , consider scaling your float to an integer and store it in FLASH. This saves memory and avoids the complexities of storing non-supported data types directly.
Enthernet Code
Enthernet Code3mo ago
@Dtynin Storing floating-point results directly in FLASH memory on the PIC16F877A microcontroller is challenging because it doesn't support the __flash qualifier for float data types. A practical solution is to convert the float to a storable format, such as a fixed-point integer or a byte array. One approach is to scale the float I.e multiply by 100 and store it as an integer, allowing you to store the scaled value in FLASH and later retrieve and convert it back to float by dividing. Another approach is to convert the float into a byte array and store each byte individually in FLASH. It's important to note that writing to FLASH at runtime has limitations and may require special routines due to the hardware restrictions of the microcontroller.
Dtynin
Dtynin3mo ago
@wafa_ath yeah thanks, I did just that...
Dtynin
Dtynin3mo ago
thanks bruh @Enthernet Code for the response.
Want results from more Discord servers?
Add your server