• AVR Freaks

Hot!Too little memory space because of Floats and Sprintf

Author
Killerpiraat
Starting Member
  • Total Posts : 68
  • Reward points : 0
  • Joined: 2020/03/09 07:40:17
  • Location: 0
  • Status: offline
2020/05/26 03:46:38 (permalink)
0

Too little memory space because of Floats and Sprintf

Hello,
 
I am designing a PCB with a microcontroller that controls a LCD display. I build it on a breadboard with the curiosity board HPC before i started designing the PCB where the uC is integrated. 
On the curiosity board there was a PIC16F18875 (14 kb of memory), but i already run out of space on that one. Because i am using floats and sprintf the memory is used up very quickly. On the PCB i will use the PIC16F15356 (24 kb) but due to circumstances and delivery problems i am forced to use the PIC16F15354 (7 kb). The program itself is not very big, so i should be able to make it work, but the problem is with the floats and the sprintf. They use SO MUCH memory.
 
For the floats i can come up myself with a solution, but is there a way to discard the sprintf and not use the entire library? I need sprintf to convert the data for the LCD, but again, SO MUCH MEMORY : '(
 
My teacher told me i can dive into the library and use only that specific piece of code (where sprintf is declared, STDIO.h?) and use that piece of program directly in my program (lets say the main)
#1

6 Replies Related Threads

    katela
    Super Member
    • Total Posts : 1861
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: offline
    Re: Too little memory space because of Floats and Sprintf 2020/05/26 04:02:18 (permalink)
    +1 (1)
    What kind of data you want to convert?
    Maybe you could consider using ltoa, itoa or ftoa (in C90 mode).

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/StudentCompanionSA
    #2
    ric
    Super Member
    • Total Posts : 28324
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: Too little memory space because of Floats and Sprintf 2020/05/26 04:05:28 (permalink)
    +1 (1)
    Killerpiraat
    For the floats i can come up myself with a solution, but is there a way to discard the sprintf and not use the entire library? I need sprintf to convert the data for the LCD, but again, SO MUCH MEMORY : '(

    XC8 is already doing most of that for you. It only includes the required parts of the sprintf library after analysing your code.
    Which mode are you using XC8 in? C90 or C99?
    Have you tried the other mode?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #3
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11982
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: Too little memory space because of Floats and Sprintf 2020/05/26 09:21:00 (permalink)
    +2 (2)
    sprintf doesn't use much code unless you're using floats.
    #4
    PStechPaul
    Super Member
    • Total Posts : 2915
    • Reward points : 0
    • Joined: 2006/06/27 16:11:32
    • Location: Cockeysville, MD, USA
    • Status: offline
    Re: Too little memory space because of Floats and Sprintf 2020/05/26 11:28:09 (permalink)
    +2 (2)
    In many cases you can use integer and long variables and integer math for most calculations, and then only convert to float when you need to display the value in the required units. You can also adjust external components to make things easier.
     
    For instance, if you are reading the voltage of a 12V nominal battery, you may have a 3:1 voltage divider so 0-15V becomes 0-5V for the ADC, and that would correspond to 0-1023 counts. But you could adjust the divider so that 15V gives just 1000 counts. You could take the sum of 15 samples so that you will have an integer value of 15,000, which would be the voltage in mV. If you need to display the value in volts on a four digit display, integer divide by 10, to get 1500, convert to a string, and simply add a decimal point as needed.

     
    #5
    NKurzman
    A Guy on the Net
    • Total Posts : 18898
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: offline
    Re: Too little memory space because of Floats and Sprintf 2020/05/26 12:22:10 (permalink)
    +7 (7)
    One way to display without using floats:
     
    uint16_t milivolts = 1234 ;
    sprintf(buffer "%u.%03u", millivolts/1000,millivolts%1000); // Display in volts with 3 decimals
    #6
    tomsedlack
    Super Member
    • Total Posts : 168
    • Reward points : 0
    • Joined: 2010/01/14 09:01:53
    • Location: 0
    • Status: offline
    Re: Too little memory space because of Floats and Sprintf 2020/05/27 04:35:25 (permalink)
    +1 (3)
    If the format and range of data is known, you can create your own "sprintf" like routines. 
     
    I do the integer trick mentioned previously (scale to milivolts, for example) and then convert using something like this:
     

     
    char *itov(unsigned int i) {

       // Return the voltage as X.XXV

        static char buff[0x06];
     
        buff[0x00]=((i%10000)/1000)+0x30;
        buff[0x01]='.';
        buff[0x02]=((i%1000)/100)+0x30;
        buff[0x03]=((i%100)/10)+0x30;
        buff[0x04]='V';
        buff[0x05]=0x00;

        return buff;
    }
     

     
    You can add other cases, formats, etc.
    post edited by tomsedlack - 2020/05/28 04:22:48
    #7
    Jump to:
    © 2020 APG vNext Commercial Version 4.5