View previous topic :: View next topic |
Author |
Message |
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
Countless problems |
Posted: Tue Nov 07, 2006 12:03 am |
|
|
Hi there it doesn't seem like my posts are getting to the forum so i'll try again. My setup: 16f877 - 20mHz, AD7730 - 5v Ref, 600g Tedea Huntleigh Loadcell.
Code: |
int32 info;
float Factor;
Factor = 5/2^24
info = AD7730_Read(Data_Reg,24);
printf("Data_Hex: %lX", info);
printf("Data_Dec: %lu", info);
result = factor * info;
printf("Result: %1.9f", result);
|
The problem i'm having is that the result is displayed wrong, if i calculate it manually it is correct, i tried type conversion but to no avail. any suggestions?
Then, the AD7730 boasts a 230 000 Count Peak-Peak Resolution, but 2^24 = 16777216????? The same with the loadcell, it claims to offer 30 000 divisions long term precision. What does that mean?
Thanx in advance |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 07, 2006 12:23 am |
|
|
This is isn't Basic. In the C language, '^' is the bitwise exclusive-OR
operator. |
|
|
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
oops! |
Posted: Tue Nov 07, 2006 1:10 am |
|
|
Thanx PCM, typed top part of code from memory real code is right. any other suggestions though? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 07, 2006 1:22 am |
|
|
Quote: |
The problem i'm having is that the result is displayed wrong, if i calculate
it manually it is correct, |
Post the sample value for 'info' that you tested. Show the result that
the program displayed, and show the result that you got with manual
calculations.
Also post your compiler version. |
|
|
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
|
Posted: Tue Nov 07, 2006 2:42 am |
|
|
OK here goes.
Sample Value: info = 0x21C178 = 2212216 decimal
Manual:
factor * info = 2.98023223876953125E-7 *2212216 = 0.659
PIC:
factor * info = 2.98023223876953125E-7 *???????? = approx 17
Could it be that the pic interprets the value of info wrong when doing the calculation? i did a printf of the factor, info(in hex) and info(in dec) and they are all correct. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 07, 2006 3:02 am |
|
|
Quote: |
Sample Value: info = 0x21C178 = 2212216 decimal
Manual:
factor * info = 2.98023223876953125E-7 *2212216 = 0.659
PIC:
factor * info = 2.98023223876953125E-7 *???????? = approx 17
|
I compiled the test program shown below with PCM vs. 3.249.
It works. It displays the following output:
Quote: |
Result: 0.659291776
|
Code: |
#include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//============================
void main()
{
int32 info;
float Factor;
float result;
Factor = 5.0/16777216.0;
//info = AD7730_Read(Data_Reg,24);
//printf("Data_Hex: %lX", info);
//printf("Data_Dec: %lu", info);
info = 0x21C178;
result = Factor * info;
printf("Result: %1.9f", result);
while(1);
} |
|
|
|
crystal_lattice
Joined: 13 Jun 2006 Posts: 164
|
|
Posted: Tue Nov 07, 2006 4:04 am |
|
|
Thanx PCM, forgot that my compiler is quite old, (Exchange rate to South African rands makes the new version a bit expensive... ) My IDE version 3.43, PCW PCM 3.188. i appreciate your help/reminder. Any sugestions on the matter of the calculation of the resolution?? |
|
|
Ken Guest
|
|
Posted: Tue Nov 07, 2006 8:50 am |
|
|
Factor = 5/2^24
Everything on the right is integer, and will be done in integer math, then the final result converted to float and assigned to Factor.
Is that an "exclusive or"?
Not sure about the operator precedence, but 5/2 is 2, not 2.5; use decimal points and parens to make sure the compiler knows what you want to do.
Hope this helps,
Ken |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Tue Nov 07, 2006 12:42 pm |
|
|
Quote: | Thanx PCM, forgot that my compiler is quite old, PCM 3.188 |
I was able modify my test program to make it work with vs. 3188
by casting the 'info' variable to a float. Add the cast shown in bold
below, and it should start working for you:
Quote: | result = Factor * (float)info; |
With the cast, I get this output from the program:
Quote: | Result: .659291744 |
|
|
|
|