Searching \ for 'Help please:- 32 bit addition and max min.' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: techref.massmind.org/techref/index.htm?key=bit+addition+max
Search entire site for: 'Help please:- 32 bit addition and max min.'.

Truncated match.
PICList Thread
'Help please:- 32 bit addition and max min.'
1997\07\23@082407 by GERRY COX

flavicon
face
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.

Thanks in advance,
Gerry Cox
spam_OUTgcoxTakeThisOuTspamdek.com

1997\07\23@094236 by STEENKAMP [M.ING E&E]

flavicon
picon face
> 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

1997\07\23@094801 by Bob Fehrenbach

picon face
GERRY COX <.....gcoxKILLspamspam@spam@DEK.COM> wrote:
>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?
>

;*******************************************************************
;  Two byte add.   Standard PIC instruction set does not have an add
;  with carry instruction.
;  At exit, if C = 1, result is greater than 2 bytes.
;  Usage example:  add_word buff, new_data
;  Note: Zero flag NOT valid at exit
;*******************************************************************

add_word: macro aaa, bbb
  movf    (bbb+1), w
  addwf   (aaa+1), f
  movf    (bbb), w
  skpnc
  incfsz  (bbb), w
  addwf   (aaa), f
  endm



  Below is the companion subtract function.  Below that is a
  multibyte subtract.  You can use a similar technique to get
  a multibyte add.


;*******************************************************************
;  Two byte subtract.
;  At exit, if C = 1, result is positive.
;  Usage example:   sub_word new_count, old_count
;  Note: Zero flag NOT valid at exit
;*******************************************************************

sub_word: macro aaa, bbb
  movf    (bbb+1), w
  subwf   (aaa+1), f
  movf    (bbb), w
  skpc
  incfsz  (bbb), w
  subwf   (aaa), f
  endm


;*******************************************************************
;Multi byte subtract.    For example, subtract number from total:


  movf    n_1, w      ;subtract least significant byte
  subwf   t_1, f
                      ;2nd byte
  movf    n_2, w      ;subtract with borrow
  skpc
  incfsz  n_2, w
  subwf   t_2, f
                      ;3rd byte
  movf    n_3, w
  skpc
  incfsz  n_3, w
  subwf   t_3, f
                      ;4th byte
  movf    n_4, w
  skpc
  incfsz  n_4, w
  subwf   t_4, f



--
Bob Fehrenbach    Wauwatosa, WI     bfehrenbspamKILLspamexecpc.com

More... (looser matching)
- Last day of these posts
- In 1997 , 1998 only
- Today
- New search...