4016 views|13 replies

142

Posts

0

Resources
The OP
 

C language float variable and string conversion occupy memory size problem [Copy link]

 
This post was last edited by dinghao1 on 2018-7-26 10:47 1. Without string conversion, the program size is 48B
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #define uchar unsigned char float fengsu; long guang; uchar str[10]; void display1() { guang=60000; // sprintf(str,"%5ld",guang); } void display2() { fengsu=12.34; // sprintf(str,"%5.2f",fengsu); } void main() { while(1) { display1(); display2(); } }
复制代码


This post is from 51mcu

Latest reply

The display part is made into a function, which can be called without floating point. The number of decimal places in the integer is specified.   Details Published on 2018-7-27 12:13
 

142

Posts

0

Resources
2
 
2. Add string conversion, program size 2594B
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #define uchar unsigned char float fengsu; long guang; uchar str[10]; void display1() { guang=60000; sprintf(str,"%5ld",guang); } void display2() { fengsu=12.34; sprintf( str,"%5.2f",fengsu); } void main() { while(1) { display1(); display2(); } }
复制代码


This post is from 51mcu
 
 
 

142

Posts

0

Resources
3
 
3. Float variables are not converted, and the program size is 2559B
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #define uchar unsigned char float fengsu; long guang; uchar str[10]; void display1() { guang=60000; sprintf(str,"%5ld",guang); } void display2() { fengsu=12.34; // sprintf(str,"%5.2f",fengsu); } void main() { while(1) { display1(); display2(); } }
复制代码


This post is from 51mcu
 
 
 

142

Posts

0

Resources
4
 
4. Delete the float variable and only convert long integers. The program size is 1110B #include
#include
#define uchar unsigned char float fengsu; long guang; uchar str[10]; void display1() { guang=60000; sprintf(str,"%5ld",guang); } void display2() { //fengsu=12.34; //sprintf(str,"%5.2f",fengsu); } void main() { while(1) { display1(); display2(); } }

This post is from 51mcu
 
 
 

142

Posts

0

Resources
5
 
Why does code size vary so much?
This post is from 51mcu
 
 
 

7422

Posts

2

Resources
6
 
Of course the code size will be larger if there are more sprintfs. This function itself is not small. Floating point is adapted through software on U without hardware floating point. You can't see it but the compiler does it for you behind the scenes.
This post is from 51mcu

Comments

Is there a smaller function that is suitable for converting variable numbers into characters and then displaying them on a 12864 LCD screen? The following is a display program english (2,4,5,str) that displays the 5 characters of str starting from the fourth character in the second line.  Details Published on 2018-7-26 16:32
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

142

Posts

0

Resources
7
 
freebsder published on 2018-7-26 14:44 Of course the code size will be larger if there are more sprintfs. This function itself is not small. Floating point is adapted through software on U without hardware floating point. You see...
Is there a smaller function suitable for converting variable numbers into characters and then displaying them through the 12864 LCD screen? Below It is a display program english(2,4,5,str) which displays the 5 characters of str starting from the fourth character of the second line
  1. write(0,0x01); english(2,1,15,table3) ;//Lighting guang=6000; guang=guang*10; sprintf(str,"%5ld",guang); english(2,4,5,str);
复制代码





This post is from 51mcu

Comments

Write one yourself. There should be quite a few on Baidu.  Details Published on 2018-7-26 18:17
 
 
 

4005

Posts

0

Resources
8
 
The floating point arithmetic library is not included if you are not using floating point numbers, so this is normal.
This post is from 51mcu

Comments

Is there a smaller function that is suitable for converting variable numbers into characters and then displaying them on a 12864 LCD screen? I originally displayed them one by one, but there is a lot of real-time data to display, so the program is too large and 51 can't fit in it. If I add a string conversion function and then display the string, the program will be smaller.  Details Published on 2018-7-26 18:03
 
 
 

7422

Posts

2

Resources
9
 
dinghao1 published on 2018-7-26 16:32 Is there a smaller function suitable for converting variable numbers into characters and then displaying them on the 12864 LCD screen? The following is a display program english (...
Write one yourself. Baidu, there should be some few.
This post is from 51mcu

Comments

Baidu found someone saying that using the sprintf function, the result was 2k larger than the original.  Details Published on 2018-7-26 18:05
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

7422

Posts

2

Resources
10
 
dinghao1 published on 2018-7-26 16:32 Is there a smaller function suitable for converting variable numbers into characters and then displaying them on the 12864 LCD screen? The following is a display program english (...
Write one yourself. Baidu, there should be some few.
This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

142

Posts

0

Resources
11
 
huo_hu published on 2018-7-26 17:15 The floating point arithmetic library is not included if you are not using floating point numbers, so it is normal to be small.
Is there a smaller function suitable for converting variable numbers into characters and then displaying them through the 12864 LCD screen? I originally displayed it one by one, and there was a lot of real-time data to be displayed. The program was too big, 51 I can’t take up more space, so I’ll add a string conversion function, and then the string display program will be smaller.
  1. write(0,0x01); english(2,1,15,table3);//illumination guang=1234; write( 0,0x93); write(1,0x30+guang/10000); write(1,0x30+guang/1000%10); write(0,0x94); write(1,0x30+guang/100%10); write (1,0x30+guang/10%10); write(0,0x95); write(1,0x30+guang%10);
复制代码
This post is from 51mcu
 
 
 

142

Posts

0

Resources
12
 
freebsder published on 2018-7-26 17:57 Write one yourself. If you search on Baidu, there should be quite a few.
Someone on Baidu said that using the sprintf function, the result was 2k larger than the original,
This post is from 51mcu
 
 
 

7422

Posts

2

Resources
13
 
dinghao1 published on 2018-7-26 16:32 Is there a smaller function suitable for converting variable numbers into characters and then displaying them on the 12864 LCD screen? The following is a display program english (...
Write one yourself. Baidu, there should be some few.
This post is from 51mcu
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

4005

Posts

0

Resources
14
 
The display part is made into a function, which can be called without floating point. The number of decimal places in the integer is specified.
This post is from 51mcu
 
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list