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

some div problem

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



Joined: 04 Jun 2006
Posts: 35

View user's profile Send private message

some div problem
PostPosted: Wed Sep 06, 2006 4:43 am     Reply with quote

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







PostPosted: Wed Sep 06, 2006 7:24 am     Reply with quote

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
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