> I have a register pair, ADC_H and ADC_L , which contain a 16 bit value > representing an ADC sample. I want to take successive samples and > add them into a 32Bit accumulator. ACC_3, ACC_2, ACC_1, ACC_0. > Speed is essential so code needs to be short. I am having trouble > with the 16bit / 32 bit Add that I attempted to write. It does funny things > for example when I add FFFF successively. I have written the rest of > the loop and the ADC control part but just can't get the accumulate to > work. > > Would someone please give me a clue on how to do the add and how to handle > the > carrys. Or maybe send a snippet of code that will help? > > Also I need to find the Maximum positive value and the Minimum negative > value of the > register pair ADC_H and ADC_L. The value in this pair represents a signed > number > from +32767 to -32768. Again code speed is important. > Remember that you have to sign extend the 16 bit value to 32 bits, before you add them to ACC. Here is a piece of code adapted from an earlier post by John Payson that should work: ; ACC(32) = ACC(32)+ADC(16) (Signed) clrf Temp,f ;Default to positive btfsc ADC_1,7 ;Test sign bit decf Temp,f ;ADC is negative movf ADC_0,w addwf ACC_0,f movf ADC_1,w btfsc C incfsz ADC_1,w addwf ACC_1,f movf Temp,w ;Get sign extension byte btfsc C incfsz Temp,w addwf ACC_2,f clrf Temp2,f rlf Temp2,w addwf Temp,w addwf ACC_3,f Looks like about 17 cycles. Niki