janosandi
Full Member level 4
- Joined
- Jan 23, 2010
- Messages
- 210
- Helped
- 2
- Reputation
- 4
- Reaction score
- 2
- Trophy points
- 1,298
- Activity points
- 2,788
Hi,
For hundreds: subtract 100 as long as the value is larger than 100. Loopcounter = hundreds
The rest: subtract 10 as long as the value us karger than 10. Loopcounter = tens
Rest: = units.
Klaus
Assuming from the ADC register names you are using a PIC, I think this will help:
Brian.
;This routine will return the number of decimal
;hundreds from an 8-bit binary
;Input: w
;Output: w
;RAM:2
;Cycles: 12-24
movlw 0xFB
GETHNDS movwf t1
clrf w2
gethnds_loop movlw .100
incf w2,f
subwf t1,f
btfsc STATUS,C
goto gethnds_loop
decf w2,w
movwf Hunds
goto GETTENS
;---
;This routine will return the number of decimal
;tens from an 8-bit binary
;Loop based, so it might take some time...
;It needs getones too
GETTENS movwf t1
clrf w2
gettens_loop movlw .10
incf w2,f
subwf t1,f
btfsc STATUS,C
goto gettens_loop
decf w2,w
movwf Tens
goto GETONES
;---
;This routine will return the number of decimal
;ones from an 8-bit binary
GETONES movwf w2
movlw .10
deltens_loop subwf w2,f
btfsc STATUS,C
goto deltens_loop
addwf w2,w
movwf Ones
return
You can use 'left alligment' and get result from ADRESH only. It will be 8 bit, but no noise suppression will be needed. It is a fast way to work with ADC if 8 bit is enought.
;This routine will return the number of decimal
;hundreds from an 8-bit binary
;Input: w
;Output: w
;RAM:2
;Cycles: 12-24
movlw 0xFB
clrf Hunds
clrf Tens
clrf Ones
clrf t1
GETHNDS movwf t1
clrf w2
gethnds_loop movlw .100
incf w2,f
subwf t1,f
btfsc STATUS,C
goto gethnds_loop
decf w2,w
movwf Hunds
goto GETTENS
;---
;This routine will return the number of decimal
;tens from an 8-bit binary
;Loop based, so it might take some time...
;It needs getones too
GETTENS movwf t1
clrf w2
gettens_loop movlw .10
incf w2,f
subwf t1,f
btfsc STATUS,C
goto gettens_loop
decf w2,w
movwf Tens
goto GETONES
;---
;This routine will return the number of decimal
;ones from an 8-bit binary
GETONES movwf w2
movlw .10
deltens_loop subwf w2,f
btfsc STATUS,C
goto deltens_loop
addwf w2,w
movwf Ones
return
8 bits in BCD are 2 digits (if there are possible combinations..).An 8-bit unsigned integer has a range of 0-255. This can be represented in three BCD digits.
8 bits in BCD are 2 digits (if there are possible combinations..).
& i need a solution in assembly i know the method but how to do it in assemly ?
;
;********************************************************************
; Binary To BCD Conversion Routine
;
; This routine converts the 8 bit binary number in the W Register
; to a 2 digit BCD number.
; The least significant digit is returned in location LSD and
; the most significant digit is returned in location MSD.
;
; Performance :
; Program Memory : 10
; Clock Cycles : 81 (worst case when W = 63 Hex )
; ( i.e max Decimal number 99 )
;
; Program: BIN8BCD.ASM
; Revision Date:
; 1-13-97 Compatibility with MPASMWIN 1.40
;
;*******************************************************************
;
LSD equ 10
MSD equ 11
;
call main
;
BinBCD clrf MSD
movwf LSD
gtenth movlw .10
subwf LSD,W
BTFSS STATUS,C
goto over
movwf LSD
incf MSD, F
goto gtenth
over retlw 0
;*******************************************************************
;
main movlw 63 ; W reg = 63 Hex
call BinBCD ; after conversion, MSD = 9 & LSD = 9
self goto self ; ( 63 Hex = 99 Decimal )
;
org 1FF
goto main
;
END
... and work out what you really want as a result.
In 8-bit mode the result can be 00000000 to 11111111 which is 0x00 to 0xFF in hexdecimal (two digits) or 0 to 255 in decimal which is three digits.
In 10-bit mode the result can be 0000000000 to 1111111111 which is 0x000 to 0x3FF in hexadecimal (three digits) or 0 to 1023 in decimal which is four digits.
The same principle applies, count how many times you can remove the decimal weight for the digit and when your result is zero or less, start counting how many time you can remove the next lower decimal weight FROM THE REMAINING VALUE.
Brian.
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?