View previous topic :: View next topic |
Author |
Message |
Xavier Guest
|
Trying to reduce code |
Posted: Sun Mar 23, 2003 11:59 am |
|
|
Hello everybody,
I am using a 16F870 with CCS PCM v3.138 and MPLAB
Here is a part of my code :
...
void Format(float Val)
{
int i;
sprintf(s,"\%06.1f",Val);
if (s[0] == '0') s[0] = '+';
for (i=0;i<6;i++) putchar(s[i]);
putchar(' ');
}
...
The aim is to format output like +002.1 or -238.2 ...
(this function is working well !)
But looking the .lst file, I see than the sprintf takes over 400 bytes.
Does somebody could help me to do the same function with more free code space ?
Thanks in advance.
Xavier
___________________________
This message was ported from CCS's old forum
Original Post ID: 12973 |
|
|
Steve H. Guest
|
Floats are BIG! |
Posted: Sun Mar 23, 2003 2:13 pm |
|
|
This probably won't help you now, but as you have seen floats are BIG. I always use fixed point math in my programs. That way I can do simple I/O routines and add the decimal point myself where needed. The user doesn't know as it looks like floating point to him, but it saves lot's of code. Fixed always tends to be smaller and faster.
Try it on your next project, you will never look back...
:-)
Steve H.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12977 |
|
|
Xavier Guest
|
Re: Floats are BIG! |
Posted: Sun Mar 23, 2003 3:23 pm |
|
|
Thanks Steve for your reply
Could you give me an example please ?
My programm only read ADC values from 2 channels and convert them in degres (0 to 360) with 10 bit resolution.
After this conversion, i send the values on RS232 with the format describe in my previous post.
+003.5 -0250.7 <crlf>
Whatever the values are, the output is always 16 bytes with sign forced.(6 bytes for each channel 2 spaces and CRLF)
Effectively 32 bits manipulation is very heavy but I don't know how to do it with 16 bits.
I would appreciate your experience.
Reguards
Xavier
___________________________
This message was ported from CCS's old forum
Original Post ID: 12980 |
|
|
Steve H. Guest
|
Re: Floats are BIG! |
Posted: Sun Mar 23, 2003 3:56 pm |
|
|
Check out this link...
You only need an integer value from 0 to 3600 That's will fit in an unsigned long (16 bits => 0-65535).
Check out this link...
Also check the CCS examples for some ways to output a fixed integer number and like this,
The code below took a channel number and displayed it like a floating point frequency. I could have used floating point throughout, but this is quicker - Plus no lingering roundoff errors...
void display_frequency(void)
{
unsigned long freq;
// Calculate actual frequency
freq = Channel*5 + 4000;
// Write frequency to display
first_line();
write_lcd("14");
write_lcd((int)(freq / 1000) + '0');
write_lcd('.');
write_lcd((int)((freq / 100) \% 10) + '0');
write_lcd((int)((freq / 10) \% 10) + '0');
write_lcd((int)(freq \% 10) + '0');
write_lcd(" MHz");
end of snippet...
Steve H.
___________________________
This message was ported from CCS's old forum
Original Post ID: 12983 |
|
|
Jim Mcbride Guest
|
Re: Floats are BIG! |
Posted: Mon Mar 24, 2003 9:44 am |
|
|
Great link. I also had a problem of running out of code space. Using 16bit pinters for memory cut my code used\% in half. Check out the help questions refering to RAM.
JIM
:=Check out this link...
:=
:=You only need an integer value from 0 to 3600 That's will fit in an unsigned long (16 bits => 0-65535).
:=
:=Check out this link...
:=
:=Also check the CCS examples for some ways to output a fixed integer number and like this,
:=
:=The code below took a channel number and displayed it like a floating point frequency. I could have used floating point throughout, but this is quicker - Plus no lingering roundoff errors...
:=
:=void display_frequency(void)
:={
:=unsigned long freq;
:=
:= // Calculate actual frequency
:= freq = Channel*5 + 4000;
:=
:= // Write frequency to display
:= first_line();
:= write_lcd("14");
:= write_lcd((int)(freq / 1000) + '0');
:= write_lcd('.');
:= write_lcd((int)((freq / 100) \% 10) + '0');
:= write_lcd((int)((freq / 10) \% 10) + '0');
:= write_lcd((int)(freq \% 10) + '0');
:= write_lcd(" MHz");
:=
:=end of snippet...
:=
:=Steve H.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13004 |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
Re: Floats are BIG! |
Posted: Mon Mar 24, 2003 12:58 pm |
|
|
:=Great link. I also had a problem of running out of code space. Using 16bit pinters for memory cut my code used\% in half. Check out the help questions refering to RAM.
-------------------------------------------------------
If you enable 16-bit pointers, it actually uses more ROM,
because it takes more instructions to use the larger pointers.
It doesn't actually reduce RAM usage. It enables the use
of RAM at address 0x100 or higher. So your total available
RAM is increased, and the percentage of "Ram used" will go
down.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13013 |
|
|
Jim Mcbride Guest
|
Re: Floats are BIG! |
Posted: Tue Mar 25, 2003 8:45 am |
|
|
:=:=Great link. I also had a problem of running out of code space. Using 16bit pinters for memory cut my code used\% in half. Check out the help questions refering to RAM.
:=-------------------------------------------------------
:=
:=If you enable 16-bit pointers, it actually uses more ROM,
:=because it takes more instructions to use the larger pointers.
:=
:=It doesn't actually reduce RAM usage. It enables the use
:=of RAM at address 0x100 or higher. So your total available
:=RAM is increased, and the percentage of "Ram used" will go
:=down.
Quite right, I meant I was running out of RAM space.
___________________________
This message was ported from CCS's old forum
Original Post ID: 13046 |
|
|
|