+ Post New Thread
Results 1 to 19 of 19
  1. #1
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    Splitting Hex Result to 3 seperated Values [PIC]

    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 andre_teprom; 15th March 2016 at 03:02. Reason: added microcontroller tag

    •   Alt14th March 2016, 04:34

      advertising

        
       

  2. #2
    Full Member level 4
    Points: 1,679, Level: 9
    xenos's Avatar
    Join Date
    May 2015
    Location
    127.0.0.1
    Posts
    203
    Helped
    78 / 78
    Points
    1,679
    Level
    9

    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.



  3. #3
    Advanced Member level 4
    Points: 7,444, Level: 20
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,328
    Helped
    314 / 314
    Points
    7,444
    Level
    20

    Re: help splitting Hex Result to 3 seperated Values

    Basicaly, he needs an value to string converter
    But have not enought words to say that
    Love me or hate me. All infractions will be ignored.


    1 members found this post helpful.

  4. #4
    Full Member level 4
    Points: 1,679, Level: 9
    xenos's Avatar
    Join Date
    May 2015
    Location
    127.0.0.1
    Posts
    203
    Helped
    78 / 78
    Points
    1,679
    Level
    9

    Re: help splitting Hex Result to 3 seperated Values



    1 members found this post helpful.

  5. #5
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    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



  6. #6
    Super Moderator
    Points: 63,074, Level: 61
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    10,449
    Helped
    3380 / 3380
    Points
    63,074
    Level
    61

    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:
    http://www.piclist.com/techref/micro...adix/index.htm

    Brian.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   Alt14th March 2016, 21:46

      advertising

        
       

  7. #7
    Super Moderator
    Points: 42,813, Level: 50
    Awards:
    Most Frequent Poster

    Join Date
    Apr 2014
    Posts
    8,756
    Helped
    2113 / 2113
    Points
    42,813
    Level
    50

    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



  8. #8
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    Re: help splitting Hex Result to 3 seperated Values

    Quote Originally Posted by KlausST View Post
    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 - - -

    Quote Originally Posted by betwixt View Post
    Assuming from the ADC register names you are using a PIC, I think this will help:
    http://www.piclist.com/techref/micro...adix/index.htm

    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 by janosandi; 15th March 2016 at 04:47.



  9. #9
    Super Moderator
    Points: 63,074, Level: 61
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    10,449
    Helped
    3380 / 3380
    Points
    63,074
    Level
    61

    re: Splitting Hex Result to 3 seperated Values [PIC]

    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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.



    •   Alt15th March 2016, 10:05

      advertising

        
       

  10. #10
    Advanced Member level 4
    Points: 7,444, Level: 20
    Easyrider83's Avatar
    Join Date
    Oct 2011
    Location
    Tallinn, Estonia
    Posts
    1,328
    Helped
    314 / 314
    Points
    7,444
    Level
    20

    re: Splitting Hex Result to 3 seperated Values [PIC]

    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.
    Love me or hate me. All infractions will be ignored.



  11. #11
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    re: Splitting Hex Result to 3 seperated Values [PIC]

    Quote Originally Posted by Easyrider83 View Post
    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



  12. #12
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    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



  13. #13
    Advanced Member level 5
    Points: 9,919, Level: 23

    Join Date
    Nov 2012
    Posts
    1,853
    Helped
    407 / 407
    Points
    9,919
    Level
    23

    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.



  14. #14
    Advanced Member level 2
    Points: 3,775, Level: 14

    Join Date
    Dec 2015
    Location
    Madrid, Spain
    Posts
    686
    Helped
    166 / 166
    Points
    3,775
    Level
    14

    Re: binary to BCD convert routine fix help

    Quote Originally Posted by c_mitra View Post
    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..).



  15. #15
    Advanced Member level 5
    Points: 9,919, Level: 23

    Join Date
    Nov 2012
    Posts
    1,853
    Helped
    407 / 407
    Points
    9,919
    Level
    23

    Re: binary to BCD convert routine fix help

    Quote Originally Posted by CataM View Post
    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.



    •   Alt15th March 2016, 17:27

      advertising

        
       

  16. #16
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    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 ?



  17. #17
    Super Moderator
    Points: 22,672, Level: 36
    andre_teprom's Avatar
    Join Date
    Nov 2006
    Location
    Portugal
    Posts
    6,897
    Helped
    846 / 846
    Points
    22,672
    Level
    36
    Blog Entries
    5

    Re: binary to BCD convert routine fix help

    Quote Originally Posted by janosandi View Post
    & 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.
    --------------------------------------------------------------------------------------------------
    Part of the world that you live in, You are the part that you're giving ( Renaissance )


    1 members found this post helpful.

  18. #18
    Super Moderator
    Points: 63,074, Level: 61
    Achievements:
    7 years registered
    Awards:
    2nd Helpful Member
    betwixt's Avatar
    Join Date
    Jul 2009
    Location
    Aberdyfi, West Wales, UK
    Posts
    10,449
    Helped
    3380 / 3380
    Points
    63,074
    Level
    61

    re: Splitting Hex Result to 3 seperated Values [PIC]

    ... 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.
    PLEASE - no friends requests or private emails, I simply don't have time to reply to them all.
    It's better to share your questions and answers on Edaboard so we can all benefit from each others experiences.


    1 members found this post helpful.

  19. #19
    Full Member level 4
    Points: 2,239, Level: 10
    Achievements:
    7 years registered

    Join Date
    Jan 2010
    Posts
    210
    Helped
    2 / 2
    Points
    2,239
    Level
    10

    Re: Splitting Hex Result to 3 seperated Values [PIC]

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

    Quote Originally Posted by betwixt View Post
    ... 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



--[[ ]]--