decimal to binary

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 7.

Moderator: Benj

Post Reply
siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

decimal to binary

Post by siliconchip »

hi all,
I'm trying to convert a decimal value to binary and displaying the output to an lcd but I'm not having any luck I can get the dec value showing but no matter what I change in the calc box I'm not achieving my goal, any help appreciated :(

cheers bob
Attachments
binary.fcfx
(9.87 KiB) Downloaded 466 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
I created a flowchart that should do that here.
Output is to RS232, but should easily be able to adapted for LCD.

Can you let me know how you got on please.

Martin
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin
as always thanks, can you look at my flowchart and let me know what you think please, also in the variables field I cant see .remainder or .count or .reversebin, I don't understand the significance of these as they are only listed in the calculation box and its got me baffled :?

cheers bob
Attachments
DECTOBINARY.fcfx
(13.58 KiB) Downloaded 446 times

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
You did a great job in converting as flowchart is working.
siliconchip wrote:I don't understand the significance of these as they are only listed in the calculation box and its got me baffled
Any variable that precedes with a decimal point is a local variable.
Local variables are only created with in the call macro (DecToBin), then are destroyed when call macro is exited.
If you want add the local variables to watch window then single step until simulation is in DecToBin (or set a break point) to pause inside .
Once paused within DecToBin then in simulation debugger select add variables then select the Local icon at the top of variables manager:
Add local to watch.png
(153.52 KiB) Downloaded 9937 times
You will only see the values when within DecToBin macro.
When in main you won't see the values as local variables created within DecToBin macro will not exist.

Martin
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin
once again thanks for the help, by making the variables local does this mean it saves space in the pic as they are only created when called then destroyed when used ? is this system efficient ? is it commonly used ? ive never seen "local variables" in a program before this is was what confused me

cheers bob

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: decimal to binary

Post by Benj »

Hi Bob,

A global variable will always consume RAM space. Anything can read or write to the global variable so it is generally considered bad practise to always use globals for everything as it can get hard to debug if there is a problem.

A local variable will only consume RAM space when you are in the macro that creates it. As the variable can only be accessed from inside the macro then it is somewhat easier to debug.

A further pointer is to try and avoid global and local variables with the same name or similar names as it can be easy to confuse the two and refer to the global when you mean to reference the local and visa versa.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi benj,
thanks for the reply am I right in thinking then that global variables are used/needed in the main part of the program but locals can be used in the add ons/(macros) part of the program, also could you use entirely local variables in all the programs macros but only global in the main section, thanks in advance

bob

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: decimal to binary

Post by Benj »

Hi Bob,

The main can also have local macros, however as you never leave the context of the main the variables will always consume RAM, so local variables in main may as well be globals.

One reason to use local variables in the main macro is if you wanted to ensure that no other macros can touch or effect your main variables which again might help with debugging.

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

Hi benj
thanks for that continuing with this theme I want to type a HEX number say 0xFF and have it show on the lcd but I can only show the first digit, ie 0x0f then it all goes wrong, showing decimal numbers ie 29 is far easier but I would like to write a HEX value to the lcd can anyone enlighten me please

cheers bob
Attachments
HEX.fcfx
(9.11 KiB) Downloaded 352 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: decimal to binary

Post by Benj »

Hi Bob,

Using a calculation icon you can call the NumberToHex$() function to convert a variable into a hexadecimal string. Then just send the string to the display.
NumToHexStr.jpg
NumToHexStr.jpg (45.31 KiB) Viewed 16288 times
Edit. Sorry just looked at your program. Should have done that first :oops:

In your program you have number = number * 10. Should this be number = number * 16 as with hexadecimal each character is worth 16 rather than 10 with decimal.

You might also need to wait for the key to be released before looping and reading the next key.

If you want to get into string manipulation and number conversions then the video on this topic may help things.
http://www.flowcode.co.uk/mmforums/viewt ... 75&p=78630

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi benj
thanks for the reply, I can send a digit 1-9 to the display but if I want to send 2 the second just over writes the first, if for example I want to put 1F the 1 will appear in the far right then when I add F it overwrites this, ive tried everything in my limited knowledge but cant suss it :oops:

bob

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
Can you try the attached file and see if its any better for you?

Martin
Attachments
HEX V2.fcfx
(9.3 KiB) Downloaded 368 times
Martin

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi,
Modified so waits for key release.

Martin
Attachments
HEX V3.fcfx
(9.68 KiB) Downloaded 370 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin
once again a big thank you, it now works as I was intending it to I compared it to my original and there was only 2 adjustments I needed to do which was *16 in the calculation and change the "key" to "number" in the hex string, but I have noticed that if I press a, then b appears and if I press b, a appears ? everything else is ok once again a big thank you to all

bob

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
Your welcome.
siliconchip wrote:but I have noticed that if I press a, then b appears and if I press b, a appears ?
That is a bug (which will require reporting) with Keypad component giving wrong values as A and B appear to be reversed in value being assigned.
I have made a simple modification to your flowchart to get around bug.

Martin
Attachments
HEX V4.fcfx
(10.53 KiB) Downloaded 343 times
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin,
that's brilliant thank you, does this code adjustment need to be their for simulation only for keys 10 and 11 ( a & b ) or will it affect actual hardware, cant test in hardware yet as I'm waiting for a keypad to arrive, once again thanks for your input

bob

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
No problem.
siliconchip wrote:does this code adjustment need to be their for simulation only for keys 10 and 11 ( a & b ) or will it affect actual hardware
Sorry I don't know the answer to that as also not got hardware to test with.
Its just a case of trying with both options.

Also before you compile and send to hardware you will need make sure pin connections are correct within Flowchart.
For example both LCD and Keypad are on the same port(B).
As your not making enable of LCD go low prior to operating Keypad, then pressing a key would nterfere with LCD display.
Best to have then on separate ports.
The can work on the same port if set up correctly, bust its not simply a case of just having both components on the same port.

Martin
Martin

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin
thanks for pointing that out :oops: I will go over it thoroughly, this whole thread has been confusing but at the same time very intuitive, I need to let my brain cool down a bit many thanks

bob

medelec35
Matrix Staff
Posts: 9521
Joined: Sat May 05, 2007 2:27 pm
Location: Northamptonshire, UK
Has thanked: 2585 times
Been thanked: 3815 times

Re: decimal to binary

Post by medelec35 »

Hi Bob,
siliconchip wrote:this whole thread has been confusing
If you let me me know which part is confusing I see if I can help you to make it clearer.

Martin
Martin

jgu1
Posts: 1333
Joined: Tue Oct 06, 2009 9:39 am
Has thanked: 1135 times
Been thanked: 299 times

Re: decimal to binary

Post by jgu1 »

As always helpfull as you are Martin. It seemed that all good people are collected here. :D

Nice that we always can get help here, from Flowcode friend and staff... :D
Thank's to all

Br Jorgen :D :D

siliconchip
Posts: 392
Joined: Wed Jan 05, 2011 11:24 am
Has thanked: 101 times
Been thanked: 25 times

Re: decimal to binary

Post by siliconchip »

hi martin
thanks for the support it is appreciated, where I mentioned confusing was the local and global variables, I didn't know local variables existed until now and by slowing the program down and single stepping ive been able to monitor what these are doing in the program, again without yours and benj help I would of been in the dark, ive also come across the special function boxes again not realising what these were for :oops: but again now I'm aware hopefully I can learn further I think for now I'm on top of this thread but if I don't ask for anymore help concerning this thread I will be back for sure looking for advice with future problems which I know will definitely occur and as Jorgen has said flowcodes community is great in helping those of us that are struggling and I hope it continues to do so once again thanks :)

bob

Post Reply