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

S

sommes

Jan 1, 1970
0
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
 
J

Joe G \(Home\)

Jan 1, 1970
0
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
print
 
K

Ken Taylor

Jan 1, 1970
0
sommes said:
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
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
else
y = "1" + y
end if
x = int(x/2)
loop
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.

Cheers.

Ken
 
K

Ken Taylor

Jan 1, 1970
0
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>

Ken

Joe G (Home) said:
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
print






sommes said:
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
 
S

sommes

Jan 1, 1970
0
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.
 
J

Joe G \(Home\)

Jan 1, 1970
0
cheers!

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

JG

Ken Taylor said:
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>

Ken

Joe G (Home) said:
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
print






sommes said:
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
 
S

sommes

Jan 1, 1970
0
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
the
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
 
S

sommes

Jan 1, 1970
0
Thank you Jasen.


Jasen Betts said:
7 is 0111
8 is 1000
9 is 1001

write 7 to an io port and the outputs will look like 00000111
those last 4 digits are are the 7

if you want the bits to come in some other position on the IO port
you'll need to shift them (eg by multiplying by 2 if bascom doesn't have a
shift operator or function)

7x2 is 14, and 14 is 00001110
8x2 id 16, and 16 is 00010000

etc...

multiplying by 4 8 ir 16 will shift them even further....

Bye.
Jasen
 
S

sommes

Jan 1, 1970
0
Thank you Alan and lionel. Now I am having a trouble with HT-12E to send 3
different 4bits in same time.
 
Top