Decimal to Binary Conversion to then print to Parallax Serial Terminal
Hello,
I am trying to run a simple program in Propeller Tool using Spin. The program utilizes the Parallax Serial Terminal to allow a user to enter a decimal number that is multiplied to create a new variable, and then that new variable is converted into binary and printed to the Parallax Serial Terminal. I am having trouble with the the decimal to binary conversion. For the spin code I have, when I try to run it I get an "Expected Expression Form" error with the i after repeat highlighted. Can anyone see a mistake I am making or a way to put that i in "expression form"? Also, if anyone has a simpler way of converting a decimal to binary, please let me know.
Thanks for the help,
Zach
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
Byte Data
word atten
word decimal
word bin
OBJ
SPI : "SPI_Spin" ''The Standalone SPI Spin engine
pst : "Parallax Serial Terminal"
f : "Float32"
fs : "FloatString"
PUB DSA_CONTROL|SERIN,CLK,LE,ClockDelay,ClockState
pst.Start(115200)
f.Start
SPI.Start(15000, 1)
pst.Str(String("Attentuation Control ", pst#NL))
repeat
pst.Str(String(pst#NL, "Enter desired level of attenuation: "))
atten := pst.DecIn
decimal := f.FMul(atten, 4)
bin := string("00000000")
repeat i from 0 to strsize(bin)-1
byte[bin][i] := (((|<(strsize(bin)-1-i))&decimal)>>(strsize(bin)-1-i))+48
Data := byte[bin][i]
pst.Str(String("Data = "))
pst.Str(fs.FloatToString(Data))
Comments
The Propeller uses binary internally. Decimal, hexadecimal and binary are just alternate ways to display the data to us humans.
Try this code.
Samething I just have Hex in mine
Thank you so much Duane Degn and DigitalBob. The code worked very well in my program.
Cheers!
Make sure you don't wait too long before asking for help.
I remember trying to figure out the same sort of stuff when I first started using microcontrollers 13 years ago. I somehow thought I needed a separate variable for binary and decimal.
The one time you will need a separate variable is when using floating point numbers. Floating point numbers are encoded differently than the integer values we normally use. Mathematical manipulation of Floating point numbers need to floating point specific code. But as a former boss of @"Heater." used to say, if you need to use floating point, you don't understand the problem. I've come to learn Heater's boss was a wise man.
I'm having another issue with decimal to binary conversion. But this one has to do with using the conversion from the Simple_Numbers library. When I implement the following code,
CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000
VAR
byte Data
long atten
long decimal
long dec
long SERIN
long CLK
long LE
OBJ
SPI : "SPI_Spin" ''The Standalone SPI Spin engine
pst : "Parallax Serial Terminal"
f : "Float32"
fs : "FloatString"
sn : "Simple_Numbers"
PUB Test_Conversion
SPI.Start(15000, 1)
pst.Start(115200)
decimal := 4
Data := sn.Bin(decimal, 8)
pst.Dec(Data)
I'm expecting the variable "decimal", defined as 4, to be converted into 00000100 and "Data" to be a variable defined as that binary number. Then to double check that I'm getting the correct binary number, I convert it back to decimal and send it to the Parallax Serial Terminal to check it. When I do this, the number that results is 192. Any ideas on where that number is coming from? Am I misusing the sn.Bin(decimal, 8) function? Also, something curious happens when I change the variable designation for "Data" in the VAR section. If I switch it from a byte to a long, without changing anything else, 6076 results instead of the 192. I'm not sure if this is a clue.
Thanks,
Zach
P.s. Sorry about the formatting of my code when the comment is posted
The variable "decimal" is stored in the Propeller in binary. The "Bin" method is just used to convert this to characters to display on the screen. The returned value of "Bin" is the location in memory where these characters are stored.
If you change "pst.Dec(Data)" to "pst.Str(Data)" you'd see the characters stored in the memory located held by "Data".
"Data" in your example hold the memory location of the ASCII characters used to display the binary value. If you don't know what ASCII characters are, look it up on Wikipedia. Understanding the difference between values stored in memory and characters to display these values is an important concept.
Edit: The code "pst.Dec(Data)" displayed memory address where the binary characters are stored in RAM. The buffer "nstr" is used in Simple_Numbers to hold these characters.
Simple_Numbers is likely not needed for what you're trying to do. Propeller Serial Terminal.spin can already display values in binary notation.
Is this what your trying to do
Put three back-ticks (on the tilde key) on the lines before and after your code -- that will cause the forum to format it like code.
Hi JonnyMac and DigitalBob,
So I'm working with a Propeller 1 Development Board. What I'm trying to do with it is have a user enter a desired level of attenuation using the Parallax Serial Terminal object, preferably I would like to have them enter a floating point number but I can't seem to find that in the PST object library, have that number be converted to binary and then have that binary be passed to the microcontroller to be outputted using the SPI object.
My strategy has been to try and make the converted binary number, converted using PST, into a variable, I call it Data, and then use that variable with the SPI object. But I'm having trouble getting that to work. Any thoughts?
I'll keep looking into how I can get the user to be able to enter a floating point number into PST and have PST recognize it, but do either of you have any suggestions for that as well? Currently when 31.75 is entered, it is read as 3175. I've temporarily gotten around this by using an if/else statement to get the desired number.
Thanks,
Zach
There are likely better floating point input methods out there but this is one made for a project.
What sort of SPI devices using floating point input?
All the SPI devices I've used require integer values as input.
Hi Duane,
I've gotten around the floating point number issue by asking the user some extra questions about the level of attenuation. If it's a fraction, then I treat the number a little differently to get the correct binary number.
You're correct, the SPI is using an integer or binary value. The floating point is geared towards the attenuator logic that will be used.
My main issue now is just trying to pass the binary number derived in the Parallax Serial Terminal object into the SPI object. Below is my code for trying to do this.
So everything seems to work well leading up to the defining of the variable decimal, both cases of fraction and integer work. The trouble comes in when I try to define the variable Data. I'm using the simple_numbers object to convert the integer stored in the variable decimal into a binary number, and then defining that as Data. (the pst.Dec(Data) is there so I can verify that the correct binary number is being stored in Data by converting it back to a decimal number and printing it to the terminal). Then that Data variable is used in the SPI object to send the binary number to the microcontroller; where LED lights blink out the binary number. Unfortunately, no matter what integer the variable decimal is it gets converted into the binary number for 200. I know that the SPI object is working as it is supposed to because I get 200 blinked out with the LED's on the microcontroller. So the issue seems to be with Data := sn.Bin(decimal, 8). Any thoughts on why that would not yield the correct decimal to binary conversion? And why it would yield the binary equivalent of 200 no matter what number the decimal variable is?
Thanks for all of the help!
Because the buffer "nstr" is located in RAM at memory position 200. I tried to explain this in an earlier post.
An integer in the Propeller is already encoded in binary. You don't need to convert. You only "convert to binary" if you want to display the number using the character "0" and the character "1". Make sure you read about ASCII characters.
Thank you for your patience Duane. I now understand what you were trying to explain. You are correct, I don't need the simple_numbers object to convert that number into binary. I was able to redefine Data and easily pass it to the SPI.
Thank you!
Ok, try this I gave an example of a float string etc. Your Attentuation fraction enter a number higher than 25 like 50 or a 100, or else you'll get "0" option 2 is ok, it just multiplies the interger. You can use the float to convert the pst.DecIn to a float etc. I don't have a DS1620 or else I would post a working code (too lazy to look at the data sheet)
Here is an old JonnyMac DS1620 Demo if that helps