Do you have a question? Post it now! No Registration Necessary
- Subject
- Posted on
Re: How to convert a decimal to 4bit binary in BASIC language?
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
Re: How to convert a decimal to 4bit binary in BASIC language?
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
Re: How to convert a decimal to 4bit binary in BASIC language?
Again it would be useful if you said exactly what you want to do.
You can convert to binary using the "BIN(x)" statment or send out the
binary to the serial port using the "Printbin" statement.
Bascom doesn't differentiate internally between binary, decimal,
octal, hexadecimal or characters. They are all (basically) 8 bits in
size and can be put out onto or read in from a port.
So if you just want to convert say number "9" into binary to output to
a port you don't need to. Just do this for example:
A = 9
portb = A
and the port will have &B0001001 on it
if, however, you do this:
A = "9"
portb = A
then the port will have &B00111001 on it. This is the binary
equivalent of ASCII 39(hex) or ASCII 57(decimal) which is the
character 9 as opposed to the quantity 9.
HTH
Alan
--
Sell your surplus electronic components at
http://ozcomponents.com
Sell your surplus electronic components at
http://ozcomponents.com
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
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.
Re: How to convert a decimal to 4bit binary in BASIC language?
I'm presuming the 10 bit is in straight binary? If so you cannot just
take the first four bits and consider them as a digit and then take
the next four bits and consider them as a digit and then take the last
two bits and consider them as a digit - it doesn't work.
You will also need to assemble the three lots of four bits from the
HT-12D into a ten (12) bit word. Each of the four bits from the
decoder does not represent a digit but just four bits out of the ten
(twelve) from your ADC.
First you have to convert the 10 bits to 3 digits (or 4 digits if you
want the complete range (0 to 1023). To do this have a look at the
MOD and DIVIDE functions in Bascom. Using these two functions you can
convert your 10bit word into three/four decimal digits.
You will have to store you 10 bit output as a WORD in Bascom. Then
convert to the three/four digits (using DIVIDE and MOD) which will
also have to me stored as WORDs. Then you can output them as you
want.
HTH
Alan
--
Sell your surplus electronic components at
http://ozcomponents.com
Sell your surplus electronic components at
http://ozcomponents.com
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
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
Re: How to convert a decimal to 4bit binary in BASIC language?
Each four bit from the HT-12D has to go in a particular position in
the 10 (12) bit word. So the first four bits go at the beginning of
the word.
For example if the first four bits are 0011 then the resultant 12 bit
word would look like:
0011 0000 0000
If the next four bits are 1001 then the 12 bit word has to look like:
0011 1001 0000
And if the final four bits are 0101 then the 12 bit word has to look
like:
0011 1001 0101
This can be accomplished several ways. The easiest is:
1) get the first four bits and multiply by 16
2) add the second four bits and then multiply by 16
3) add the final four bits.
Another way is:
1) get the first four bits and shift left four times
2) add the second four bits and shift left four times
3) add the final four bits.
Yet another way is:
1) get the first four bits and multiply by 256
2) get the second four bits and multiply by 16
3) add the result of 1) and 2)
4) add the final four bits.
And even another way is:
1) get the first four bits and shift left eight times
2) get the second four bits and shift left four times
3) add the result of 1) and 2)
4) add the final four bits.
They all basically do the same thing. Sit down with pencil and paper
and work out how the above work. Then try to work out which is the is
the easiest to understand or the most efficient depending on which you
prefer. Sometimes ease of underatnding is very helpful in the future
but sometimes you just need to have the most efficient way of doing
things (and that can be sometimes very hard to understand later on).
By the way I'm sure you (and others) could find even more ways of
doing the conversion. There isn't really a right way and a wrong way
as long as the result is correct.
HTH
Alan
--
Sell your surplus electronic components at
http://ozcomponents.com
Sell your surplus electronic components at
http://ozcomponents.com
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
Just remember there is no "right" way to do it. If it works and takes
a 100 line of code it's still right even if someone else can do it in
three lines of code.
The thing to bear in mind with what you are doing is exactly what the
10/12 bits represent (ie 0 to 1023 in binary) and how you need to
handle them to get out the data you need, (ie three or four decimal
digits).
Have fun
Alan
--
Sell your surplus electronic components at
http://ozcomponents.com
Sell your surplus electronic components at
http://ozcomponents.com
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
FWIW, normal design practice with that kind of gizmo is to drive your
display directly from the MC, rather than using a decoder chip. That
said, you'd be trading one less chip for I/O pins or added program
complexity, so your choice isn't unreasonable for a one-off design. ;)
Couldn't be simpler:
-----
' *** Assumptions about your version of BASIC: ***
'
' Everything is integer by default, as is typical for microcontrollers
' mod is the modulo operator,
' / is the integer division operator
' ReadADC() returns the most recent ADC output
dim digit(3) ' Array to hold conversion results, as the display mux
' will need to refresh the LED drive continuously from
' a timer interrupt or somesuch.
bin = ReadADC()
for count = 0 to 3 ' 10 bits = max output of 1023 = 4 digits.
' loop starts with least significant digit
' in digit(0). Count backwards (step -1) if you
' find this digit order confusing.
digit(count) = bin mod 10 ' Extract lowest BCD digit.
bin = bin / 10 ' Shift down by one power of ten.
next
' *** Actual display driver stuff goes here, outputing contents of
' digit() to the display driver circuit.
-----
--
W
. | ,. w , "Some people are alive only because
W
. | ,. w , "Some people are alive only because
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
As Ken say (below) there are easier solutions than the HT12E/D pair.
But is you are stuck with them then what you need to do is split your
10/12 bit word into three 4 bit words and send them one after another.
At the receiving end you take the three 4 bit words and reassemble
them into a 12 bit word.
I already gave you the basics of splitting using the DIVIDE and MOD
functions. Reassembling is done by multiplying (or shifting) and
adding.
Alan
--
Sell your surplus electronic components at
http://ozcomponents.com
Sell your surplus electronic components at
http://ozcomponents.com
We've slightly trimmed the long signature. Click to see the full one.
Re: How to convert a decimal to 4bit binary in BASIC language?
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
Site Timeline
- » (Melb) 21" monitor free to good home
- — Next thread in » Electronics Down Under
- » DIY bike lights / AA battery current
- — Previous thread in » Electronics Down Under
- » Dontronics Closure
- — Newest thread in » Electronics Down Under
- » [OT] neue Eiskarten
- — The site's Newest Thread. Posted in » Electronics (German)
- » Acquisti online, da chi?
- — The site's Last Updated Thread. Posted in » Electronics Hobby (Italian)