Electronics PIC, 24 Bit Addition - Help Required

Discussion in 'Modding' started by SteveyG, 3 Apr 2004.

  1. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    I'm either having a bit of trouble with my 24-bit addition routine or there's something else wrong.

    Basically I want to add 0F 42 44 to whatever is in the GPR's HI, MID and LO

    At the moment I'm using this code, but I'm not sure if it's correct or not, so if anyone can help re-write any errors I'd be very grateful:

    Code:
    			
    	MOVLW	0x44                           ; LSB VALUE TO ADD
    	ADDWF 	LO,F		                ; ADD IT TO THE REMAINDER ALREADY IN LSB
    	BTFSC	STATUS,0		; 
    	INCF 	MID,F		             ; CARRY. LSB OVERFLOWED, SO INC MID
    
    
    	MOVLW 	0X42			; MID VALUE TO ADD
    	ADDWF 	MID,F		             ; ADD 0x42 TO THE REMAINDER ALREADY IN MID
    	BTFSC	STATUS,0		;
    	INCF 	HI,F		             ; CARRY. MID OVERFLOWED, SO INC MSB
    
    	MOVLW 	0X0F			; GET MSB VALUE 
    	ADDWF 	HI,F		             ; LOAD IN MSB
     
  2. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    yup looks sound.

    two tips, the simulator in MPLAB will awnser this question quicker than posting, also when posting always use symbols (ie C for carry rather than the address).

    One slight issue u might have with the code, is initilisation of mid and high, when you use INCF they could have any number in them including FF, and u would lose a carry on the INCF.

    see www.piclist.com source code library for more on maths routines.
     
  3. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    One thing to mention, its only ZERO thats effected by INCF/DECF and related instructions, CARRY isn't touched. (so test on ZERO, as for these ops CARRY = not ZERO
     
  4. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    This is the problem I think I may be having issues with. How would you get around this problem? By adding 1 instead of incrementing the MID value then testing for carry again?
     
  5. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    erm no its straight forward enough, the big question is how important overflow (one or more carrys) in the hi byte is.

    Code:
    	MOVLW	0x44                           ; LSB VALUE TO ADD
    	ADDWF 	LO,F		                ; ADD IT TO THE REMAINDER ALREADY IN LSB
    	BTFSS	STATUS,C		; 
    		GOTO	MIDADD	
    	INCF 	MID,F		             ; CARRY. LSB OVERFLOWED, SO INC MID
    	BTFSC	STATUS,Z		; Has CARRY occured?
    		INCF HI, F		; Yes, yes it has
    
    MIDADD:
    	MOVLW 	0X42			; MID VALUE TO ADD
    	ADDWF 	MID,F		             ; ADD 0x42 TO THE REMAINDER ALREADY IN MID
    	BTFSC	STATUS,C		;
    		INCF 	HI,F		             ; CARRY. MID OVERFLOWED, SO INC MSB
    
    	MOVLW 	0X0F			; GET MSB VALUE 
    	ADDWF 	HI,F		             ; LOAD IN MSB
    
     
  6. SteveyG

    SteveyG Electromodder

    Joined:
    23 Nov 2002
    Posts:
    3,049
    Likes Received:
    8
    Thanks for your help. It's sometimes difficult to think when tired!! :baby:
     
  7. TheAnimus

    TheAnimus Banned

    Joined:
    25 Dec 2003
    Posts:
    3,214
    Likes Received:
    8
    Your okay until the numbers start dancing, hey they do that a lot for me (cuas i'm specail like the clineman kid down the road) :)
     

Share This Page