CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Large Math Problems?

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Tre



Joined: 03 Jun 2005
Posts: 21

View user's profile Send private message

Large Math Problems?
PostPosted: Mon Jun 06, 2005 10:57 pm     Reply with quote

I am working with the ADXL210 example code. I actually have an ADXL202E so I am modifying the code slightly. I have however, run into some problems with the actual calculations.

I left ADXL210_SCALING_FACTOR alone defined at 100,000

signed long accelerometer_read(void) { // Returns hundreths of a g
signed int32 value = (signed int32)ADXL210_SCALING_FACTOR;

printf("Value0: 0x%X%X%X%X\r\n", make8(value, 3), make8(value, 2), make8(value, 1), make8(value, 0));
printf("t1: %lu, t2: %lu, zg: 0x%X%X%X%X, ppg: %lu\r\n", adxl_t1, adxl_t2, make8(adxl_zerog, 3), make8(adxl_zerog, 2), make8(adxl_zerog, 1), make8(adxl_zerog, 0), adxl_ppg);
value *= (signed int32)adxl_t1;
printf("Value1: 0x%X%X%X%X\r\n", make8(value, 3), make8(value, 2), make8(value, 1), make8(value, 0));
value /= adxl_t2;
printf("Value2: 0x%X%X%X%X\r\n", make8(value, 3), make8(value, 2), make8(value, 1), make8(value, 0));
value -= adxl_zerog;
printf("Value3: 0x%X%X%X%X\r\n", make8(value, 3), make8(value, 2), make8(value, 1), make8(value, 0));
value /= adxl_ppg;
printf("Value4: 0x%X%X%X%X\r\n", make8(value, 3), make8(value, 2), make8(value, 1), make8(value, 0));

delay_ms(1);

#asm nop #endasm
return(value);
}

This is the output that I get from the function
Value0: 0x000186A0
t1: 4683, t2: 8431, zg: 0x0000C350, ppg: 125
Value1: 0x1BDD7BE0
Value2: 0x0000D858
Value3: 0x00001508
Value4: 0x0000002B

As you can see, Value0 is correct, 0x000186A0 is indeed 100,000d. However, as soon as that is multiplied by 4683, the PIC is returning 0x1BDD7BE0 when it should actually be 1BE9B0E0. This multiplication results in an error of 800000d.

I have tried all of the casts that I can possibly think to add. I am using a PIC18F2550.

Thanks,
Drew
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 06, 2005 11:26 pm     Reply with quote

I got rid of all your make8() stuff, and just used %LX. I made a test
program and compiled it with PCH vs. 3.188 and also PCM 3.225 and
it worked OK. Results:

t1: 4683, t2: 8431, zg: 0000C350, ppg: 125
Value1: 1BE9B0E0

Code:
#include <18F452.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000) 
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#define ADXL210_SCALING_FACTOR 100000L

//========================
void main()
{
signed int32 adxl_t1 = 4683;
signed int32 adxl_t2 = 8431;
signed int32 adxl_zerog = 0x0000C350;
signed int32 adxl_ppg = 125;

signed int32 value = (signed int32)ADXL210_SCALING_FACTOR;

printf("t1: %ld, t2: %ld, zg: %LX, ppg: %ld\r\n", adxl_t1, adxl_t2, adxl_zerog, adxl_ppg);

value *= (signed int32)adxl_t1;

printf("Value1: %LX\r\n", value);

while(1);
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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