The substract code I posted is bad, I don't know what happened. Here is the fixed version: On Mon, 7 Oct 2002, Peter L. Peres wrote: *>On Sun, 6 Oct 2002, Joaquim J. Peixoto wrote: *> *>*>This is not a direct solution to your problem but an attempt to rethink it. *>*> *>*>Why add in BCD? It seems unnatural on a PIC. If you are using a PIC, then *>*>the natural method would be binary addition. *> *>The natural way to store data in a micro with limited space and limited *>maths is packed BCD. Then you need half the storage space of unpacked BCD, *>and no base conversions. *> *>In C the code is like: *> *>uchar pbcd; // packed bcd accumulator *>bit carry; // carry in and out, normally LSB in some register, or *> // use the one in STATUS *> *>// add one packed BCD byte to another, returns carry out *>uchar pbcd_add( uchar addend ) { *> *> pbcd += addend; // this is clear *> if(carry) // so is this *> ++pbcd; *> *> if((pbcd & 0x0F) > 0x09) // need daa lsbyte *> pbcd += 0x06; *> *> carry = 0; // assume no carry out *> if((pbcd & 0xF0) > 0x90) { // need daa msbyte *> carry = 1; *> pbcd += 0x60; *> return(1); // carry *> } *> return(0); *>} *> *>// substract a packed BCD number from another *>void pbcd_sub( uchar subtrahend ) { *> *> pbcd -= subtrahend; *> if(carry) *> --pbcd; *> *> if((pbcd & 0x0F) > 0x09) // need to saa lsbyte *> pbcd -= 0x06; // fixed *> *> carry = 0; *> if((pbcd & 0xF0) > 0x90) { // fixed *> carry = 1; *> pbcd -= 0x60; // fixed *> return(1); *> } *> return(0); *>} -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body