|
|
View previous topic :: View next topic |
Author |
Message |
tren Guest
|
Floating point to ascii conversion |
Posted: Tue Mar 02, 2004 3:41 pm |
|
|
Im trying to find the remainder of 0.5.. I believe it would be in the REMB0, REMB1 register if I were using the math16.inc file and assembly. But, I have no idea were to find it using the CCS compiler.. Im also using a PIC18F4320.
floatvalue = (floatvalue * 1000);
int32value = (int32)floatvalue;
int32value = (int32value / 10);
ie. with values:
floatvalue = (12.345 * 1000);
int32value = 12345
int32value = (12345 / 10);
thus int32value=1234.. BUT WERE IS THE 0.5 stored..
Thanks in advance |
|
|
Ttelmah Guest
|
Re: Floating point to ascii conversion |
Posted: Tue Mar 02, 2004 3:56 pm |
|
|
tren wrote: | Im trying to find the remainder of 0.5.. I believe it would be in the REMB0, REMB1 register if I were using the math16.inc file and assembly. But, I have no idea were to find it using the CCS compiler.. Im also using a PIC18F4320.
floatvalue = (floatvalue * 1000);
int32value = (int32)floatvalue;
int32value = (int32value / 10);
ie. with values:
floatvalue = (12.345 * 1000);
int32value = 12345
int32value = (12345 / 10);
thus int32value=1234.. BUT WERE IS THE 0.5 stored..
Thanks in advance |
Two possible approaches.
The compiler calls a generic 'division' function, and the same function is used for the % operation (which would give you the required remainder). The simplest approach is just to use %, and call the routine again. The downside, is that this takes a long time.
A while ago, I posted, that you can 'cheat' this for int16 (you would have to look at the assembler for int32 to see if the same is true). Basically, if you define a function as having an int32 return, and then do not actually return data from the function, you receive 'back', the contents of the compilers 'scratch' registers. So if you code:
Code: |
int32 dummy(int16 val1, int16 val2) {
int16 unused;
unused=val1/val2;
}
|
Then the int32 value returned from the function contains the contents of scratch0..scratch3, returned by the division.
If you return the value in a union, with access to the individual bytes, you can then put these back together to form both the remainder, and the result of the division.
When I looked into it, the registers were not in the order perhaps expected, with the division result in scratch1 & 2, and the remainder in scratch0 & 3.
Now it may be possible to extend this to dealing with int32 values.
Basically if you look at the call made for the int32 division function, and for the modulus, if both do use the same routine, the same 'trick' may be applicable.
Best Wishes |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Tue Mar 02, 2004 4:01 pm |
|
|
PrintF is one of the strengths of the CCS compiler. You can print using a float and specify the number of decimal places to use. That should help if you are really doing a "Floating point to ascii conversion". |
|
|
Guest
|
|
Posted: Wed Mar 03, 2004 1:26 am |
|
|
void lcd_int32_ascii(BYTE cursor, BYTE sig_dig, int32 ivalue){
BYTE dec_point, i, n, num_chars;
dec_point = cursor - sig_dig;
lcd_send_cntrl(0x04);
lcd_send_cntrl(cursor);
if(ivalue<1000000){
if(ivalue<100000){
if(ivalue<10000){
if(ivalue<1000){
if(ivalue<100){
if(ivalue<10){
num_chars = 1;
goto next;
}
num_chars = 2;
goto next;
}
num_chars = 3;
goto next;
}
num_chars = 4;
goto next;
}
num_chars = 5;
goto next;
}
num_chars = 6;
}
next:
for(i=0; i<num_chars; ++i){
n = ((ivalue % 10) + 0x30);
ivalue = (ivalue / 10);
if(cursor == dec_point){
lcd_send_byte('.');
}
lcd_send_byte(n);
cursor--;
}
lcd_send_cntrl(0x06);
}
So there it is. Lets say you have 1234 and have decided to display 1 digit to the right of the decimal point. 1st 4 is sent to the lcd, then the dec point, then 3, etc. Values up to 6 digits in length can be printed. I dont know if this is the best way to this but it works. |
|
|
|
|
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
|