|
|
View previous topic :: View next topic |
Author |
Message |
freesat
Joined: 08 Feb 2011 Posts: 32
|
CCS is not working like GCC C works, need help. |
Posted: Sat May 10, 2014 4:09 pm |
|
|
Hi Guys,
I want some help to display formated number on 7seg display and LCDs, i have tried on GCC compiler and it works, the output formated number as string is correct, but i cant do the same on CCS.
PC GCC C Code working ( this results in '003' in command line after execute )
Code: |
void main() {
char strval[4];
char strfmt[4];
int decs = 3;
int val = 3;
sprintf( strfmt, "%%0%du", decs );
printf( strfmt, val );
}
|
CCS Code
Code: |
void myfunc() {
char strval[8];
char strfmt[4];
int8 zeroleft = 3
int8 value = 3
// using dinamic format string
sprintf( strfmt, "%%0%du", zeroleft );
sprintf( strval, strfmt, value );
// strval does not have '003' string
// but using fixed format works correct
sprintf( strval, "%03u", value );
// strval now have '003' string
}
|
Im doing something wrong? any sugestions to correct this or a better way to do that?
PS, can be a solution based on division by 10 to get each digit, including zero left dynamic format.
Thanks... |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Sat May 10, 2014 4:50 pm |
|
|
see
PRINTF()
in the CCS manual
formatting is well explained and VERY 'C standard compliant s well.
it is NOT however the same as the compiler you refer to. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9243 Location: Greensville,Ontario
|
|
Posted: Sun May 11, 2014 5:33 am |
|
|
just a note...
when 'porting' over code from another compiler, you should use the same variable names.
ie: GCC decs=3; CCS zeroleft = 3
It makes it a LOT easier to follow and locate possible problems.
I know, maybe a small point but at first read, to me , the programs are not the same....
cheers
jay |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sun May 11, 2014 7:53 am |
|
|
Just a warning to be careful when converting from GCC code.
The INT length in GCC is 32 bits. In CCS C it is 8 bits. _________________ Google and Forum Search are some of your best tools!!!! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sun May 11, 2014 8:11 am |
|
|
The simple answer to the 'key problem', is that CCS does not support the use of variables for the format string. Have to be _constants_.
Look in the manual:
"printf (cstring, values...)
cstring is a constant string or an array of characters null terminated.
Note that format specifies do not work in ram band strings.
"
A bit misspelt, but the meaning is clear...
The reason is fundamental. PIC's generally are small, so CCS parses the format string _at compile time_, not 'run time', and only includes the code actually used.
Now it is possibly 'silly' that CCS does not offer a variable based parser for larger chips, but that is the way that the language is written. There have been partial printf parsers published here, and more generically, CCS includes value to string conversion functions like itoa, to allow you to perform the conversions without printf.
Best Wishes |
|
|
freesat
Joined: 08 Feb 2011 Posts: 32
|
|
Posted: Sun May 11, 2014 5:41 pm |
|
|
I want to say thanks for answers, they are extremely professional and clear to understand.
Now i know compiler build code based on constant, and it will be fixed on final code, not dynamically.
So, any suggestion to do that using less code "Flash" possible? any samples?
PS.. I'm trying to format a number ( integer and floats ) and also put zero left when needed. |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Mon May 12, 2014 1:20 am |
|
|
The serious answer is 'get rid of the floats'.....
FP maths, is _large_. There is little done using them, that cannot be done using scaled integers. Faster, and less bulky. Using floats in a PIC, is a sure way to use a lot of ROM _pointlessly_....
No, you cannot use external memory to save code space. A _few_ PIC's allow external ROM to be used, but these are rare, and use a lot of pins.
Scale your numbers to fit into an integer range, and use integer maths. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|