Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Splitting Hex Result to 3 seperated Values [PIC]

Status
Not open for further replies.

janosandi

Full Member level 4
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
hello Guys
i Need to split An HEX value which i've got it from ADC as a result To seperated values to drive 3 7segment's in Assembly Language
For Example(got 0x65 which means 101 So i want to get 1 and 0 and 1 in seperated Variables)
i've searched alot of confusing tutorials about assembly Dividing ways But i've got more confused

and BTW what is the ADRESL and ADRESH of ADC converter ?

thx for help
 
Last edited by a moderator:

Re: help splitting Hex Result to 3 seperated Values

ADRESL is the low byte (8 least significant bits) or the ADC value
The high byte is the ADRESH.

You can use division and modulo or
use loop with subtraction (with 100 till <=99 to find hundreds digit, with 10 till <=9 to find decs digit and the rest will be the last digit) or
you can search for conversion from int to bcd and send values to 7segs.
 

Re: help splitting Hex Result to 3 seperated Values

Basicaly, he needs an value to string converter :thinker:
But have not enought words to say that :)
 
Re: help splitting Hex Result to 3 seperated Values

**broken link removed**
 
Re: help splitting Hex Result to 3 seperated Values

yes right that's wht im looking for but how to do tht in assembly ?

thx Guys

- - - Updated - - -

i've found alot of methods to do it but it didnt work
 

Re: help splitting Hex Result to 3 seperated Values

Assuming from the ADC register names you are using a PIC, I think this will help:


Brian.
 

Re: help splitting Hex Result to 3 seperated Values

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
 

Re: help splitting Hex Result to 3 seperated Values

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

can u plz send me an example plz?

- - - Updated - - -

im reading the microchip app note about math routines right now

- - - Updated - - -

Assuming from the ADC register names you are using a PIC, I think this will help:


Brian.

i've fonnd a routine which is wht i need but its not working as it must do
would you plz help me to fix it ?
im not tht good with math
Code:
;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
i've put the value 0xFB to w as input & i've got 2 hundreds but the GETTENS is not ok

im woring on it now hope to understand wht the problem is

- - - Updated - - -

The GETHnds routine loop run twice & get the right values then it is do it for third time & the w is 3 then decreased by one but the t1 is 0xCF now
what is the risponsible of effecting STATUS ?
plz Help
 
Last edited:

If you are using ADRESH and ADRESL it would appear you are not using the correct methods. The result will be 10 bits wide (2 in ADRESH and 8 in ADRESL) so you should be using routines for the next highest multiple of bits, in this case 16. You need a 16-bit binary to 4 digit BCD converter. That should convert all values between zero (000 BCD) to 0x3FF (1023 BCD).

Brian.
 

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.
 

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.

Ican't use this method as i guess coz if i have FF or 255 i cant read it
 

binary to BCD convert routine fix help

i was looking for a solution to seperate HEX value into 3 digits to use it in my program
& i dont know about numbers & finally i've got it i need binary to bcd converter routine in my code & have found a simple one & the method is easy but its not working
i've tried to fix it but i couldnt

any help will b appriciated

Code:
;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
 

Re: binary to BCD convert routine fix help

An 8-bit unsigned integer has a range of 0-255. This can be represented in three BCD digits.

I did not understand your code.

Let x is the number to be converted.

x-x/10 will give you the remainder; this is the least significant BCD digit.

Let x -> x/10;
x-x/10 will give the middle BCD digit.

Let x -> x/10;
x-x/10 will be the most significant BCD digit.
 

Re: binary to BCD convert routine fix help

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..).
 

Re: binary to BCD convert routine fix help

8 bits in BCD are 2 digits (if there are possible combinations..).

That will be called hex, and not BCD. BCD values are represented in 4 bits, true, but contain 0-9 only. They are therefore called binary coded decimal.
 

Re: binary to BCD convert routine fix help

& i need a solution in assembly i know the method but how to do it in assemly ?
 

Re: binary to BCD convert routine fix help

& i need a solution in assembly i know the method but how to do it in assemly ?

Draw a flowchart of the process, read instructions set in datasheet, and then write the code, piece by piece.
 
... 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.
 
i've found another routine at microchip's application Notes but again its not working
any one can help with this ?
as i guess it must work
Code:
;
;********************************************************************
;                  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

- - - Updated - - -

im working on the code again with the instruction set trying to understand how it works line by line

thx Guys

- - - Updated - - -

i've found the problem with this code of microchip which its my fault
it was for 16c54 & im using 16f877 & i've changed LSD & MSD files to 20 & 21 & now its doing what its supposed to do

thx Guys All

John

- - - Updated - - -

... 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.

what i was looking for was wrong so i need binary to Decimal converter not bcd
the binary to BCD 2 digits its ok now & im working on the second one now

thx
John
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top