|
|
View previous topic :: View next topic |
Author |
Message |
butterfly
Joined: 04 Jun 2006 Posts: 35
|
some div problem |
Posted: Wed Sep 06, 2006 4:43 am |
|
|
i'm using pic16f877a. And want to do:
i have signed integer and value;for example;
int16 value,result;
int16 zerovalue=0x01f0;
int8 step=0x03;
value = Read_ADC();
value=value-zerovalue;//// i want to learn if it is negatif
result=div(step,value);
whatever value negatif or pozitif, is result pozitif?
i could not understand that
wuold you mind giving me hand? |
|
|
Ttelmah Guest
|
|
Posted: Wed Sep 06, 2006 7:24 am |
|
|
You do not have a 'signed integer'. Your declaration of the integers, is using the _unsigned_ type. This is the default. So if (for instance), you have a value of '0x100', and then perform the subtraction 0x100-0x1F0, you will 'get' the positive result 65296, The arithmetic has 'wrapped', but cannot represent the negative value (since the integers are unsigned), so you get the result + 65536...
Use signed values:
Code: |
signed int16 value,result;
signed int16 zerovalue=0x01f0;
int8 step=0x03;
value = Read_ADC();
value=value-zerovalue;//// i want to learn if it is negatif
//It now will be negative, since the data type is signed.
|
There is also though a 'caveat' your division, is performing int8 arithmetic, using an int8, and an int16 value. The 'div' function, also does not expect a int16 result declaration, but a div_t result declaration, to contain both the quotient, and remainder. At present, you are dviding '3', by a value that is almost certainly much larger, so will get a garbage return...
Best Wishes |
|
|
|
|
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
|