Floating point division in pic

Discussion in 'Programmer's Corner' started by RG23, Apr 14, 2011.

  1. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
  2. DumboFixer

    Active Member

    Feb 10, 2009
    219
    34
    If you post the code you tried, people will be able to give more help.
     
  3. John P

    AAC Fanatic!

    Oct 14, 2008
    1,634
    224
    This doesn't sound like floating point. Dividing a 16 bit quantity by an 8 bit one?
     
  4. RG23

    Thread Starter Active Member

    Dec 6, 2010
    301
    2
    I used the following division subroutine

    Code ( (Unknown Language)):
    1.  
    2. LOOP501:
    3.  
    4. RLF AARGB1, F ;and shift in next bit of result///////AARGB1:AARGB0 16 bit dividend
    5. RLF AARGB0, F
    6. RLF REMB0, F
    7. RLF count301, F ;save carry in counter////////////count301 is initialised to 16
    8.  
    9. MOVF count209, W ///////count209 is the 8 bit divisor
    10. BTFSS CARRY
    11. INCFSZ count209, W
    12. SUBWF REMB0, W ;keep that byte in W untill we make sure about borrow
    13. SKPNC ;if no borrow
    14. BSF count301, 0 ;set bit 0 of counter (saved carry)
    15. BTFSC count301, 0 ;if no borrow
    16. GOTO UOK46LL ;jump
    17. MOVF REMB0, W ;read high byte of remainder to W
    18. ;to not change it by next instruction
    19. UOK46LL
    20. MOVWF REMB0 ;store high byte of remainder
    21. bcf CARRY ;copy bit 0 to carry
    22. RRF count301, F ;and restore counter
    23. DECFSZ count301, f ;decrement counter
    24. GOTO LOOP501 ;and repeat loop if not zero
    25.  
    26.  
    27. RLF AARGB1, F
    28. RLF AARGB0, F
    29. RETURN
    30.  
     
    Last edited by a moderator: Apr 14, 2011
  5. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    If you don't need need top speed a very simple and reliable way is by successive subtraction, (it's simpler than it sounds) you just keep subtracting the 8bit number until it's done. And count the number of subtractions.

    Code ( (Unknown Language)):
    1.  
    2. // example; 1000 / 20
    3. i = 1000;
    4. result = 0;
    5. while(i >= 20)
    6. {
    7.   i -= 20;
    8.   result ++;
    9. }
    10. // when it gets to here you have the result
    11.  
     
Loading...