BasicMicroUK - Forums

www.basicmicro.co.uk
It is currently Tue Jan 23, 2018 5:00 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: Help needed with simple math
PostPosted: Wed May 18, 2011 10:07 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
I know this is probably really simple for everyone, but I don't usually use math in my projects except for addition and subtraction. I need to get a 2 digit percentage of two numbers. These numbers will never exceed 10,000. These numbers will change, but to simplify things I will use two known values.

So I have
aa var sword
bb var sword
aa=10
bb=5

Now I need to divide aa by bb and show it as a percent (which will be displayed on a LCD). The end result should be 50%

So I also have
divval var sword

then I do the calculation
divval=bb/aa

And that is where I get stuck. This should give me a reslut of .5, but I always get 0 as the result. I've even fiddled with bb//aa, but I can't get it to work. What am I doing wrong?

<Edit> I've even tried using fdiv with long variables and no luck.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 12:57 am 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
I think I may still need help with this, but I found another problem I need to deal with. First this is one of those OEM boards, so I have to use IDE with it. It's been a while since I've done any programming and I forget if there is a way to monitor variables with the debug. But in any case I have been using my LCD to display results. I use a command like this

serout p15,n2400,[254,128,"value",dec divval]

But apparently the 'dec' command does not send the proper value if there is a decimal point. So I need to get that working first.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 1:11 am 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
Hi,

There are a few issues here. As aa and bb have been defined as an SWORD variables they are signed 16 bit integer variables. So
5/10 will equal zero.

(Page 32 of current syntax PDF) If you wish to use floating point you should define the variables using the float type, like:
aa var float.

Now if you simply want to display the value as a percentage, you could stay with fixed point math and do something like: divval = (bb * 100)/aa

But if you do go with floating point and you wish to print out a floating point number you should use REAL modifier instead of DEC (Page 81)
Code:
aa var float
bb var float
divval var float

aa = 5.
bb = 10.
divval = aa/bb
serout p15,n2400,[254,128,"value",real divval]


Kurt


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 1:41 am 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Thanks Kurt, I think you nailed it. But as I mentioned I have to use IDE with this module and it doesn't like variables assigned as float like that.

But this seems to be working

aa var long
bb var long
divval var long

aa= float 5
bb= float 10
divval = aa fdiv bb

serout p15,n2400,[254,128,"value",real divval]

Question - what does the 'real' modifier do? That seams work for the LCD display issue but is not in my IDE manual. The display now shows 0.5000

I have a few more issues, but let me see if I can figure them out.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 4:08 am 
Offline
Master

Joined: Tue Nov 21, 2006 5:34 pm
Posts: 662
What the Real modifier does is on the page I mentioned in the previous post in the Basic Micro studio Syntax Manual, which is downloaded as part of the IDE. You can get to this by selecting the appropriate item in the the Help menu of the IDE...

Kurt


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 4:26 am 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
KurtEck wrote:
What the Real modifier does is on the page I mentioned in the previous post in the Basic Micro studio Syntax Manual, which is downloaded as part of the IDE. You can get to this by selecting the appropriate item in the the Help menu of the IDE...

Kurt


I did find it in the BMS manual, but it does not work quite the same with IDE.

REAL{#maxint} expression{\#maxdec}
\#maxdec works
#maxint does not (unless I'm doing something wrong)

In any case I was able to work around it by first using 'divval fmul float 100' and then using 'dec int divval' in the serout command and have the exact output I want. Thanks for the help. I would still be scratching my head.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 5:45 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
The old Atom OEM module should show up as an Atom 24M module in Studio IIRC. Have you tried it yet?

In the old IDE(8.X and older) you are correct, you have to use longs to store floating point values and you have to keep track what kind of number the long actually holds, and you have to use the old floating point math functions.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Thu May 19, 2011 6:05 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Acidtech wrote:
The old Atom OEM module should show up as an Atom 24M module in Studio IIRC. Have you tried it yet?


Yes. That was my thread right below this one. OEM module won't program with BMS. Gives 'error writing 0'

atom-f484/error-writting-0-with-basic-micro-studio-t9608.html


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Fri May 20, 2011 6:33 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
Hmm. OK. And I have no way of getting it to work since we haven't had one for several years. :(

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Fri May 20, 2011 6:35 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
I have a suggestion though. Have you tried going in and setting just RTS or just DTR to be enabled? The OEM Atom module used a standard Atom interpreter chip, rev A. Also try adjusting the reset hold time. IDE and Studio default to different values for that and may be the cause of that problem.

I'm getting an old Atom24M module back from a customer that's having a Writing:0 error as well so maybe fixing that will fix the old OEM modules as well.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Fri May 20, 2011 7:12 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
Acidtech wrote:
I have a suggestion though. Have you tried going in and setting just RTS or just DTR to enabled? The OEM Atom module used a standard Atom interpreter chip, rev A. Also try adjusting the reset hold time. IDE and Studio default to different values for that and may be the cause of that problem.


I tried all combinations of RTS and DTR. I also tried different values for reset hold time. Once I get over 100 it errors out that it can't find a BA. I think I read somewhere where 75 is the highest value that should be used. Lower values do not work either.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Sun May 22, 2011 7:21 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
Ok, then for now you can either keep using the old IDE, send in the old module for me to test and figure out what the problem is or wait and see if it gets fixed when I work on the 24m module I'm getting back. If you want to send it in for testing make sure you include a printed return address inside the package. When we get these packages 9 out of 10 teims the return address has been destroyed so we can't figure out who sent us something.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Sun May 22, 2011 8:51 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
I'll just wait and see what happens with the 24m. Most of these modules that I have are already are in use, and the programs were written with IDE, so it's easy enough to just keep using that for them.

Thanks again to those who helped with the original math problem.


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Sun May 29, 2011 8:08 pm 
Offline
Site Admin
User avatar

Joined: Thu Mar 01, 2001 7:00 pm
Posts: 1316
Location: Temecula, CA
I got the other customers old 24m module and found it was leaving some garbage in the read buffer when getting in to boot mode so I added a clearreadbuffer just before starting the write which fixed the problem on that old module. THis is probably the ssame problem with the OEM modules so Studio 2.0.0.13 should work fine with them now.

_________________
Tech Support
Basic Micro - Robotic Technology Evolved


Top
 Profile  
 
 Post subject: Re: Help needed with simple math
PostPosted: Sun May 29, 2011 8:13 pm 
Offline
Citizen

Joined: Sat May 07, 2011 9:52 pm
Posts: 30
I'll give it a try as soon as it's availble to download.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 16 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 2 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group

phpBB SEO