• Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

converting 99 decimal to 0x99 HEX etc.. - newbie question (MPLAB, PIC 16F870)

Status
Not open for further replies.

Schwuppes

New Member
Hi everyone!

I'm new to PIC programming and kind of stuck on a little problem.
I have written a code that adds or subtracts number from each other and then outputs the result to PortC. The output number ranges from 0 - 99.
However PORTC is connected to a 7 - segment HEX display. So for example if I output the number 99 (decimal) to PortC the 7 segment will only display 0x63... i need to do this with any number from 0 -99 decimal.
I've been trying to figure out a way of getting around this but no result so far...
I have had two ideas (i.e subtracting .10 then incrementing a register and loading the nibbles in PORTC) The other idea was to make a lookup table with 99 entries... but that seems a bit silly and not exactly very "elegant"

I hope someone here has an idea.

cheers!
 

Pommie

Well-Known Member
Most Helpful Member
I think this should do what you require but it is untested,

Code:
;start with value in W
		clrf	Decimal10
Count10s	incf	Decimal10,F
		addlw	0f6h;		-10
		btfsc	STATUS,C
		goto	Count10s
		addlw	0ah
		decf	Decimal10,F
		swapf	Decimal10,F
		iorwf	Decimal10,W
;new value is now in W
Mike.
 
Last edited:

Schwuppes

New Member
Cheers Mate!

Its not quite working, When I arrive at that routine with for example 41 (decimal) in W after going through that subroutine the resulting new W value is 0x50h instead of 0x41h. :confused: I've tried since this morning to fix this problem, but I just can't figure it out. :(
 

ericgibbs

Well-Known Member
Most Helpful Member
Cheers Mate!

Its not quite working, When I arrive at that routine with for example 41 (decimal) in W after going through that subroutine the resulting new W value is 0x50h instead of 0x41h. :confused: I've tried since this morning to fix this problem, but I just can't figure it out. :(
hi,
This is a routine for converting a 16bit Binary number to 5 ASCII characters.
You can modify it for use with an 8 bit value, or just load 00 in the high byte.
I have used 0x00 and 0x99 as a test value in 'main'

When you transfer the ASCII characters to the LCD you can insert a decimal point into the displayed value [if required]

Im sure Mike will correct the earlier routine, but this will keep you going until then.:)
 

Attachments

Last edited:

Mike - K8LH

Well-Known Member
Here's a couple alternatives to supplement Mike's and Eric's;

Mike, K8LH

Code:
        radix   dec
b2d99
        clrf    temp            ; W = binary input, 0x00..0x63
sub10   incf    temp,F          ;
        addlw   -10             ;
        bc      sub10           ;
        addlw   10              ;
        decf    temp,F          ;
        swapf   temp,F          ;
        iorwf   temp,W          ; W = packed bcd 00..99
Code:
        radix   dec
bin2bcd
        clrf    tens            ; isochronous bin2bcd, 00..99     |B0
        addlw   -80             ; W = W - 80                      |B0
        rlf     tens,F          ; shift in 2^3*10 bit             |B0
        btfss   tens,0          ; borrow? no, skip, else          |B0
        addlw   80              ; W = W + 80                      |B0
        addlw   -40             ; W = W - 40                      |B0
        rlf     tens,F          ; shift in 2^2*10 bit             |B0
        btfss   tens,0          ; borrow? no, skip, else          |B0
        addlw   40              ; W = W + 40                      |B0
        addlw   -20             ; W = W - 20                      |B0
        rlf     tens,F          ; shift in 2^1*10 bit             |B0
        btfss   tens,0          ; borrow? no, skip, else          |B0
        addlw   20              ; W = W + 20                      |B0
        addlw   -10             ; W = W - 10, now W = "ones"      |B0
        rlf     tens,F          ; shift in 2^0*10 bit             |B0
        btfss   tens,0          ; borrow? no, skip, else          |B0
        addlw   10              ; W = W + 10, now W = "ones"      |B0
        swapf   tens,F          ;                                 |B0
        iorwf   tens,W          ; W = packed bcd 00..99           |B0
 
Last edited:

Schwuppes

New Member
Hi everyone!

Sorry for the late reply, I've been very busy and forgetting alot of things lately. :eek:
Just wanna say thanks and my code is working now, I got alot out of your input! :) Very helpful as always.
I took what Pommie did and changed and added a little bit onto it to get exactly what I needed.


CONVERT1


MOVF PORTCTEST,W
clrf DECIMAL10


Count10s incf DECIMAL10,F
addlw 0f6h; -10
btfsc STATUS,C
goto Count10s
;MOVF DECIMAL10,W
;MOVWF DECIMAL102
;SWAPF DECIMAL102,F
;MOVLW .1
;SUBWF DECIMAL10
swapf DECIMAL10,F
iorwf DECIMAL10,W

RETURN


CONVERT2

;MOVF DECIMAL102,W
MOVWF CONVERT1S
CLRF CONVERT1S2
;MOVLW .01
;MOVWF CONVERT1S2
C1 INCFSZ CONVERT1S
GOTO C2
GOTO COMBINE

C2 CLRW
MOVLW .01
ADDWF CONVERT1S2
GOTO C1
RETURN


COMBINE

SWAPF DECIMAL10,F
MOVLW .1
SUBWF DECIMAL10,F
SWAPF DECIMAL10,F

MOVLW .246
ADDWF CONVERT1S2,W
BTFSS STATUS,C
GOTO DIS
GOTO ZERO

;SWAPF DECIMAL10,F
;MOVF DECIMAL10,W
;IORLW B'00001111'


SWAPF CONVERT1S2,W
IORLW B'00001111'
MOVWF CONVERT1S2
MOVLW B'00001000'
SUBWF CONVERT1S2,F
MOVLW B'00000001'
SUBWF CONVERT1S2,F
 
Status
Not open for further replies.

Latest threads

EE World Online Articles

Loading
Top