FloatToStr vs. sprintf problem

General discussion on mikroC PRO for PIC32.
Post Reply
Author
Message
Pecas001
Posts: 3
Joined: 03 Jan 2019 17:40

FloatToStr vs. sprintf problem

#1 Post by Pecas001 » 08 Jan 2021 11:30

Hi everybody,

I'm working on a code, this is very simple, but I have interfaced a problem.
The device is a PIC32MX460F512L, 80MHz, I'm using only UART1.
I would like to store the MinCurrent Value in the internal flash memory, and if it is needed, read it out, and send to the PC via UART. As you can see, the MinCurrent value is 3.9.
The writing and reading back of this to/from the Flash is working good, without any problem, I got the expected value. My problem is, that if I want to send this via UART, the FloatToStr routine gives good result, and I get on UART what I want.
In contrast, the sprintf gives me always zeros, and I don't know why. If I use sprintf with an exact declared float, it is also working well. Can you please have a look on my code and try to solve this issue?

Thanks,
Zoli

Here you can see my complete code:

#include "stdint.h"

float ReadFlashMemory(unsigned long FlashReadAddress, int FlashArea);
char txt[20];
float MinCurrent=0;

void main() {

AD1PCFG = 0xFFFF;
UART1_Init(256000);
Delay_ms(100);
MinCurrent=ReadFlashMemory(0x9d001000,0);
FloatToStr(MinCurrent, txt); //sprintf(txt,"%.5f",MinCurrent);
UART1_Write_Text(txt);
UART1_Write(13);
UART1_Write(10);

}

float ReadFlashMemory(unsigned long FlashReadAddress, int FlashArea){

unsigned long Data;
unsigned long* FlashReadData;
float DefaultValue=0;
float ActualValue=0;

switch (FlashArea) {
case 0: DefaultValue=3.9; break;
}

FlashReadData = FlashReadAddress;
Data= *FlashReadData;

if(Data==0xFFFFFFFF)
{
Flash_Erase_Page(FlashReadAddress);
Flash_Write_Word(FlashReadAddress,*(uint32_t*)&DefaultValue);
}

ActualValue= *(float*)FlashReadData;
return ActualValue;

}

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: FloatToStr vs. sprintf problem

#2 Post by filip » 14 Jan 2021 12:42

Hello,

Have you been able to sort out this issue ?

Regards,
Filip.

Pecas001
Posts: 3
Joined: 03 Jan 2019 17:40

Re: FloatToStr vs. sprintf problem

#3 Post by Pecas001 » 19 Jan 2021 11:59

Hello Filip,

no, unfortunately this is still hanging.

Regards,
Zoli

Post Reply

Return to “mikroC PRO for PIC32 General”