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 support@ccsinfo.com

Substraction

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







Substraction
PostPosted: Wed May 11, 2005 4:25 am     Reply with quote

Hello,

how to do a substraction of 0x50 - 0x45 to have the result 0x05 and not 0x0b ?

Thanks a lot.
JohnKennedy



Joined: 12 May 2004
Posts: 38

View user's profile Send private message

PostPosted: Wed May 11, 2005 5:20 am     Reply with quote

Simply you can't as the answer is correct at 0x0b if you convert the hex to decimal and do the maths you will see this (80 - 69 = 11 which is 0x0b in Hex)

HTH

JFK
Ttelmah
Guest







PostPosted: Wed May 11, 2005 5:22 am     Reply with quote

You won't.
Decimal 50-45, gives 5. Hexadecimal, 0x50 (decimal 80), minus 0x45 (decimal 69), gives 0xB (decimal 11).
If you want five as a result, start with the numbers in decimal, not hex.
I think in fact the numbers you have, may be binary coded decimal (BCD), not hex. If so, there have been routines posted here on the forum in the past for doing BCD conversions, and what you need is to convert the incoming numbers to decimal, before performing the arithmetic, and then convert them back (if the values have to go back to the same chip).

Best Wishes
Joe
Guest







PostPosted: Wed May 11, 2005 6:04 am     Reply with quote

Ok thanks, i hope it is ok, in fact it is bcd exactly how to convert BCD to decimal ?

Is that correct
Code:
#define fromBCD(x) ((x & 0xF)+conv[(x>>4)])
?

Many thanks
Ttelmah
Guest







PostPosted: Wed May 11, 2005 6:34 am     Reply with quote

Yes. This is a routine I posted to do this. You need the array 'conv' defined as well before it'll work.

Best Wishes
Joe
Guest







PostPosted: Wed May 11, 2005 6:38 am     Reply with quote

Ok thanks i think i figured it out !

Thank you very much !
Joe
Guest







PostPosted: Wed May 11, 2005 6:44 am     Reply with quote

I forgot to ask something !

If i want to make operations on BCD i can't, isn't it ?
I have to convert it in decimal and then make operations on it and then re convert in BCD ? Is that right ?

I can't substract with BCD ?
Ttelmah
Guest







PostPosted: Wed May 11, 2005 7:25 am     Reply with quote

You could potentially write BCD maths functions. You would have to seperate out the digits, do the arithmetic on these, and then rebuild the final number. The other way of doing this, is the 'ADD adjust' operation, where you look at the result of the decimal arithmetic, and the carry bits, and work out how to adjust the result to give the required BCD value. This operation is a standard function on some processors (the Z80 for example), but does not exist on the PIC. BCD, has fallen from favour in recent years, so processor support is not common on modern chips. The simplest solution, would be something like:
Code:

int1 eflag;
int8 BCDAdd(int8 v1,int8 v2) {
   int8 temp;
   eflag=false;
   temp=(v1 & 0xF0)+(v2 & 0xF0);
   v1=(v1 & 0xF)+(v2 & 0xF);
   if (v1>9) {
       v1=v1-9;
       ++temp;
   }
   if (temp>0x90) {
      temp=temp-0x90;
      eflag=true;
   }
   return(temp+v1);
}

This returns the BCD sub of two BCD values, and sets 'eflag', if the result overflows the two digits.

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