Need help converting a 4 bit binary number to 4 separate values


Closed Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,621


    Did you find this post helpful? Yes | No

    Default Re: Need help converting a 4 bit binary number to 4 separate values

    Hi,
    Here's a couple of ideas:
    Code:
    Value VAR BYTE
    A VAR Value.0
    B VAR Value.1
    C VAR Value.2
    D VAR Value.3
    A,B,C,D will then be treated as BIT variables which may or may not work for your intended purposes. If it doesn't then perhaps:
    Code:
    Value VAR BYTE
    A VAR BYTE
    B VAR BYTE
    C VAR BYTE
    D VAR BYTE
    
    Convert:
      A = (Value & %00000001)                     ' A will be either 0 or 1
      B = (Value & %00000010) >> 1
      C = (Value & %00000100) >> 2
      D = (Value & %00001000) >> 3
    RETURN
    If you're writing to a display and what it's actually looking for is the ASCII representation of the numerical digit '1' (instead of the binary value 1) then just add 48 to the value:
    Code:
    Convert:
      A = (Value & %00000001) + 48              ' A will be either 48 ('0') or 49 ('1')
      B = ((Value & %00000010) >> 1) + 48
      C = ((Value & %00000100) >> 2) + 48
      D = ((Value & %00001000) >> 3) + 48
    RETURN
    /Henrik.

  2. #2
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Need help converting a 4 bit binary number to 4 separate values

    Thanks Henrik,

    I'll give those a try.

    When I get this working it will have...
    • 8 digit 7-segment display of 8 bit binary number in the form of 1's and 0's
    • 8 individual LED's to represent an 8 bit binary number in the form of High and LOW (on/off)
    • 2 digit alphanumeric display to represent Hex number
    • 4x20 LCD using BIG digits to display Decimal number

    All controlled by a 16F690.

    Will count up/down from 0-255
    Each of the above listed display formats will show the same number in a different way.
    A POT will increase/decrease/stop counting speed
    2 buttons to change counting direction or single count when pot is turned all the way to stop.

    My intention is to have a way to show the same number in DEC / HEX / BIN so that students can see the relationship between the different number formats.
    Oh Yeah, and to have something sort of Rube Goldberg-ish and kind of geeky to spark interest in electronics and programming.

    I'll try and post the code and a video when I get it finished.

    thanks
    Last edited by Heckler; - 22nd June 2015 at 16:22.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

Similar Threads

  1. Replies: 10
    Last Post: - 8th March 2015, 19:26
  2. Changing output binary number sequence? is it possible?
    By wdmagic in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 3rd January 2013, 00:47
  3. Replies: 7
    Last Post: - 11th September 2010, 00:32
  4. Convert long binary number to ASCII Decimal
    By Joe Rocci in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 9th July 2009, 13:58
  5. Averaging 16 bit values without using 32 bit math
    By sirvo in forum mel PIC BASIC Pro
    Replies: 2
    Last Post: - 5th October 2007, 22:18

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts