Array To Binary, and Convert that from Binary To Decimal
jespervanbondt
Posts: 5
in Propeller 1
Hi,
I am working a a program that fetches data from an ADC using SPI,
I am able to store the values of the sensor in an array
that looks somewhat like this [0,1,0,1,1,0,1,0]
I have found a binaray to decimal converter
return (((BCD >> 4) * 10) + (BCD & $F)) (https://forums.parallax.com/discussion/134931/binary-to-decimal)
How am i able to convery my array to a proper binary number and then convert that to decimal?
I'm sorry if this question seems dumb i have only experimented with the propeller tool a few days
I am working a a program that fetches data from an ADC using SPI,
I am able to store the values of the sensor in an array
that looks somewhat like this [0,1,0,1,1,0,1,0]
I have found a binaray to decimal converter
return (((BCD >> 4) * 10) + (BCD & $F)) (https://forums.parallax.com/discussion/134931/binary-to-decimal)
How am i able to convery my array to a proper binary number and then convert that to decimal?
I'm sorry if this question seems dumb i have only experimented with the propeller tool a few days
Comments
Or is it wrong, it's my first time bitbanging
If you've read the bits 0, 1, 0, 1, 1, 0, 1, 0 you probably want to reconstruct the number %01011010. As Jon suggested, it's probably simplest to do this on the fly; something like: The final line shifts the contents of val left and then or's in the next bit. At the end you will have an 8 bit value val (i.e. a byte) that you can do whatever you want with.
I think you probably meant MISO (Master In Slave Out), which is how the ADC (slave device) would return data to the propeller (master device). MOSI (Master Out Slave In) is used for sending data from the propeller (such as commands) to the ADC.
Walter
https://forums.parallax.com/discussion/134931/binary-to-decimal
the comment of beau expects a string ... how could i convert the long val to a string so that this code works
i mean like how do i edit decimal += (byte[string(val)]-48)*(|<(7-i)) so it accepts val as a value
I think what your getting at is that your checking the value of the serial input with each clock and storing whether its 1 or 0. Now you want to know how to convert those 1s and 0s into a actual number. If thats what your asking,the answer is that you do it AS you receive them. There are various different ways that people use to receive serial data but they typically do very much the same thing. For example, you could check the value of the bit. Then take the result you get and xor it with a register that you use to accumulate your result. then shift it left. Then get the next bit, xor it, shift left, until you have it all.
You certainly COULD build your result after the fact. That might be appropriate if for example the data was coming in so fast you had no time to do anything but grab it. Just take a bit, shift left, then xor it,and repeat, until you have built the entire number. It wont be decimal. It will be a binary number (instead of a single bit)
Sometimes there is some confusion about the difference between a number and a string, since when we type them they look the same, but inside the computer they are very different. The string "123" is stored as four bytes 49, 50, 51, and 0. 49 is the ASCII code for "1" , 50 is the ASCII code for "2", and so on. The *number* 123 can be stored as one byte. When we write that byte in decimal we write it as 123. In hex we would write it as 7B. In binary it would be 01111011. Those are all ways of writing the same number.
Generally speaking computers store numbers in a kind of binary encoding internally. Some very old computers used binary coded decimal, but it's not really used any more. The Propeller certainly uses binary internally, but generally (for human consumption) most Propeller programs, including Spin, use decimal or hexadecimal notation.
I guess it would be useful to step back and see exactly what it is you are trying to do with the bits you receive. Is the device sending you ASCII data (a string)? Or is it sending you a value directly? Most devices do the latter, but sometimes the protocol might specify ASCII or some other encoding.
The ADC does not send a string, it just sends the number ( from 0 to 1024 ) in binary i.e 15 = 1111
This is a link to the docs of my ADC so you have a better understanding of what i'm trying to do https://cdn-shop.adafruit.com/datasheets/MCP3008.pdf
Page 19 the second area of text is the data receiving, i read the values of MISO and store them into val like you said, now i want to convert that binary value
to a decimal i.e from 1111 to 15, so i can start storing the sensorvalues. Also the MOSI is already coded and the ADC is configured.
Also i should note that i don't have the ADC yet, i ordered it and it will arrive tomorrow.
So could you modify your code to add the conversion from val to decimal?
There are other conversions that can be done (hex, bcd, octal, etc) for specific uses such as bcd for 7 segment displays.
There is many data type encodings out there. Just a plain number has two very common encodings, namely integer and floating point. Then there is fixed point (close neighbour of integer) and a multitude of string encodings. Integer (fixed point) is usually always native to the processor/hardware.
The integer (fixed point), as well as being the fastest to handle, is also the most efficient use of RAM, for a linear number at least.
And some used base 16 instead of base 2 for floating point...