Connect with us

How to convert a decimal to 4bit binary in BASIC language?

Discussion in 'Hobby Electronics' started by sommes, Apr 22, 2006.

Scroll to continue with content
  1. sommes

    sommes Guest

    I am using BASCOM AVR and I would like to ask.

    How to convert a decimal to 4 bit binary digit.

    For example.

    Convert 7 to 0111
    Convert 8 to 1000
    Convert 9 to 1001

    Thank you very much
  2. Binary is Base 2 each bit in binary has the decimal weighted representation
    1, 2, 4, 8, 16

    2^0 = 1,
    2^1 = 2,
    2^2 = 4
    2^3 = 8
    2^4 = 16

    Eg to convert 9 to binary

    9 divided by 16 = 0, with 9 remainder (4th bit is 0)

    9 divided by 8 = 1 and 1 remainder (3rd bit is 1)

    1 divided by 4 = 0 and 1 remainder (2nd bit is 0)

    1 divided by 2 = 0 and 1 remainder (1st bit is 0)

    1 divided by 1 = 1 and 0 remainder (0th bit is 1) = 01001


    Let Num = the number to be converted to binary

    Let n = the number of bits you need (integer)

    let array of bits(0..n) (integer)

    Let rem = remainder (integer) = 0 (start at zero)

    let res = result (of modulus division)

    You can have a loop (for i = n to 0)


    algorithm.... not exactly basic code

    FOR i = n to 0

    res = Num mod 2^i *** mod is modulus
    divide function in basic

    rem = Num- [res * 2^i] *** compute the remainder

    *** get what's left over from the divide

    bit(i) = res *** store the result in the bit array.

    Num = rem *** do it over again with what's left over

    Next i

    *** display bit result ***

    For I = n to 0
    Print bit(i); ** display each bit individually
    next i
  3. Ken Taylor

    Ken Taylor Guest

    I'm not 100% sure that this is the same syntax as BASCOM uses, but you
    should be able to adapt this:
    'assume x is the decimal integer to be converted to binary.
    dim x as integer
    dim y as string
    a = 1
    y = ""
    do until x = 0
    if int (x/2) = x/2 then
    y = "0" + y
    y = "1" + y
    end if
    x = int(x/2)
    x = val(y)

    inelegant but it works. I needed to convert decimal numbers to a binary
    string representation which is why it's like this. There's undoubtedly a
    nicer way to do it.


  4. Ken Taylor

    Ken Taylor Guest

    This is what I meant by elegance, and it looks like it's actually BASCOM (As
    I said, I don't know it myself). I paused for a cup of tea before I posted
    my example so missed this. <sigh>


  5. sommes

    sommes Guest

    Thank you Joe, Ken and Alan. You guys are really helpful.

    I think I better explain what I am going to do.

    In this program, I require to read the 10bit ADC from AT90S8535 via 4 bits
    output from HT-12D(Decoder) and convert this 10 bits to display on three
    digit 7 segment display. A 74LS48 BCD to 7-segment converter is used. As
    Alan stated, a decimal can be used directly in BASCOM-AVR (I just found out
    today). For example, portd = 9, and the output will be 1001, which is
    correct value for BCD.

    I've finished the program, and working in the simulation, however, the 7
    segment can't display correctly as simulation, I think I didn't read the ADC
    value correctly.

    I knew It is little bit hard to understand, I will post my source code
    tomorrow (as saved in office computer).

    Thank you for reading my long message.
  6. cheers!

    Some times exlaining to others helps yourself understand the original topic
    a bit better.


  7. sommes

    sommes Guest

    Thank you Alan again.

    I've finished the 10 bits to 3 digit conversion with simple div and mod.

    I would like to ask, how can I get assemble the three lots of four bits from
    HT-12D into a ten (12) bit word to get the correct value from ADC via radio
    transmission and display on 7 segment ?

    I just wonder, should I not using all 4 bit in HT-12E, and only use 1 bit to
    transmit the digitial signal to avoid the assemble in HT-12D?

    Thank you very much
  8. sommes

    sommes Guest

    Thank you Jasen.

  9. sommes

    sommes Guest

    Thank you Alan. I will try to work it out.
  10. sommes

    sommes Guest

    Thank you Alan and lionel. Now I am having a trouble with HT-12E to send 3
    different 4bits in same time.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day