View Single Post
  #15   Spotlight this post!  
Unread 05-10-2005, 17:22
dcbrown dcbrown is offline
Registered User
AKA: Bud
no team
Team Role: Mentor
 
Join Date: Jan 2005
Rookie Year: 2005
Location: Hollis,NH
Posts: 236
dcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud ofdcbrown has much to be proud of
Re: 16 bit math on PIC


I think for the 12F, the code could be done as follows. There isn't a ADDWFC (add w/carry) as there is on the 18F so I suppose you just test the Carry bit in the status reg directly?). Anyway, I might have flipped the value of the carry bit but I believe it is asserted to 1 upon carry out and asserted to 0 for a borrow - but I might have them mixed up so make sure you test this before using it. Just one way, I'm sure there are lots of others.

Code:
Where a is at address 0x20/0x21
      b               0x22/0x23
      c               0x24/0x25

:                   c = a+b;
     MOVF   0x20, W           ; a.lo              -> W
     ADDWF  0x22, W           ; W + b.lo          -> W, set carry bit status
     MOVWF  0x24              ;                      W -> c.lo
     MOVF   0x21, W           ; a.hi              -> W
     BTFSC  STATUS,C          ; test carry bit in STATUS[0x03].<0>
     ADDLW  1,    W           ; W + Carry in bit  -> W
     ADDWF  0x23, W           ; W + b.hi          -> W
     MOVWF  0x25              ;                      W -> c.hi

:                  c = a-b;
     MOVF   0x20, W           ; a.lo              -> W
     SUBWF  0x22, W           ; W - b.lo          -> W, set borrow bit status
     MOVWF  0x24              ;                      W -> c.lo
     MOVF   0x21, W           ; a.hi              -> W
     BTFSS  STATUS,C          ; test carry bit in STATUS[0x03].<0>
     SUBLW  1,    W           ; W - Borrow        -> W
     SUBWF  0x23, W           ; b.hi - W          -> W
     MOVWF  0x25              ;                      W -> c.hi