division include floating point...

Status
Not open for further replies.

meera83

New Member
i m using PIC16F877, Assembly language..

if i want to do math division, how should i do to make sure the LCD will also show floating point like 1/4=0.25(two decimal point)...

i had a division program, but the LCD didn't show result in two decimal point..1/4=0..

thanks..
 
hi meera,
Are you using the microchip.com floating point subroutines?

Can you post the code that you are using, maths and LCD driver/conversion subr?
 
You also need to consider if you really need floating point?, most applications work fine with integers - much faster, much smaller, and more accurate - simply insert the decimal point in the display routines.

For example - don't use dollars and cents ($4.23), use just cents instead (423 cents) - this is how spreadsheets do maths for money, and for the same reasons.
 
I agree with Nigel. Scale your values and stick with Integer arithmetic. It's simpler. Then insert the decimal point manually.

Take 12-bit two's compliment sign extended Dallas 18B20 Temperature Sensor data, for example. The 12-bit binary data represents °Celcius in 0.0625° increments, or put another way, °Celcius*16. By scaling the formulas you can get away with a simple 8x16 multiply routine, a simple divide-by-16 routine, and a simple binary-to-bcd routine to provide decimal output to 1 decimal place (you still have to insert the decimal point manually).
Code:
;******************************************************************
;
;  BC2DC - convert binary °C*16 to decimal °C*10 in D3:D0 vars
;
;  formula is: °C*10 = (10*(Celsius*16))/16
;                    = (10*(TempH:TempL))/16
;
;  D3:D0 result '-250' to '1250' (°C*10)
;
BC2DC
        movlw   d'10'           ; multiplier 10                   |B0
        call    Multiply        ; do 10 x TempH:TempL             |B0
        goto    Div16           ;                                 |B0

;******************************************************************
;
;  BC2DF - convert binary °C*16 to decimal °F*10 in D3:D0 vars
;
;  formula is: °F*10 = (18*(Celsius*16)+(320*16))/16
;                    = (18*(TempH:TempL)+( 5120))/16
;
;  D3:D0 result '-670' to '2570' (°F*10)
;
BC2DF
        movlw   d'18'           ; multiplier 18 (9/5*10)          |B0
        call    Multiply        ; do 18 x TempH:TempL             |B0
;
;  add (32 * 10 * 16) to result
;
        movlw   low (.320*.16)  ; add (320 * 16) to Product       |B0
        addwf   ProdL,F         ;                                 |B0
        skpnc                   ;                                 |B0
        incf    ProdH,F         ;                                 |B0
        movlw   high (.320*.16) ;                                 |B0
        addwf   ProdH,F         ;                                 |B0
        skpnc                   ;                                 |B0
        incf    ProdU,F         ;                                 |B0
;
;  divide result by 16
;
Div16
        movlw   d'4'            ;                                 |B0
        movwf   Count           ;                                 |B0
DivNext
        clrc                    ; clear C                         |B0
        rrf     ProdH,F         ;                                 |B0
        rrf     ProdL,F         ;                                 |B0
        decfsz  Count,F         ; all done?                       |B0
        goto    DivNext         ; no, branch, else                |B0
;
;  check for negative number result
;
        bcf     TempH,7         ; clear "negative" flag           |B0
        btfss   ProdH,3         ; negative result?                |B0
        goto    Bin2Dec         ; no, branch, else                |B0
;
;  convert it to a postive number
;
        bsf     TempH,7         ; set "negative" flag             |B0
        movf    ProdL,W         ; temperature low                 |B0
        sublw   h'00'           ; two's complement it             |B0
        movwf   ProdL           ; ^ is that a verb (grin)?        |B0
        movf    ProdH,W         ; temperature high in W           |B0
        skpc                    ; borrow?  no, skip, else         |B0
        incf    ProdH,W         ; temperature high + 1 in W       |B0
        sublw   h'00'           ; two's complement it             |B0
        movwf   ProdH           ;                                 |B0
;
;  Peter Hemsley's 12-bit Bin_to_BCD (ProdH:ProdL binary input
;  and D3:D0 unpacked BCD output)
;
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…