Sign in

username:

password:



Not a member?

Search basicx



Search tips

Subscribe to basicx



basicx by Keywords

Accelerometer | ADC | ADXL | Adxl20 | AVR | BasicStamp | BX-35 | BX28 | BX35 | COM3 | Compiler | Downloader | EEPROM | Electromagnet | GetADC | GP2D1 | GPS | I2C | IDE | Keypad | LCD | LCD+ | MIDI | Motors | Multitasking | Netmedia | Networking | PCB | PID | PlaySound | PWM | Relays | RTC | Servo | ShiftOut | SitePlayer | SPI | Stack | Timer | USB

Ads

Discussion Groups

Discussion Groups | BasicX | Re: only two decimals

Discussion forum for the BasicX family of microcontroller chips.

only two decimals - Ed - Jan 11 12:48:00 2005


hi

I'm trying to do a program that has to show a result with only two
decimals (from a mathematical operation). At the moment I'm using
CSng to output decimals but this method shows too many.

Any hint on how to get the uC to show up (print on monitor) only 2
decimals? Thanks!






(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )


RE: only two decimals - Tom Becker - Jan 11 13:29:00 2005

> ... Any hint on how to get the uC to show up (print on monitor) only 2
decimals?

I can suggest two methods:

1- Function Fmt(2.345, 2) yields string "2.34", and
2- You can construct the equivilent with Mid, cStr and cInt. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - Don Kinzer - Jan 11 13:49:00 2005


--- In , "Tom Becker" <gtbecker@r...> wrote:
> [...]
> 1- Function Fmt(2.345, 2) yields string "2.34", and

Note, however, that Fmt() doesn't round; it simply truncates. Fmt
(2.346, 2) also yields the string "2.34".

I have written some formatting routines that do round properly.
However, they generate characters into a byte array which you would
then have to either display individually or convert to a string.
Also, the routines can optionally justify the resulting digit
sequence within a fixed number of characters (centered, flush left or
flush right). This is handy for emitting to an LCD.

You can find the routines at the link below and may use them for non-
commercial applications.

http://www.kinzers.com/don/BX24/Routines/Format.bas





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

RE: Re: only two decimals - Tom Becker - Jan 11 14:07:00 2005

> Note, however, that Fmt() doesn't round; it simply truncates.

No, I chose 2.345 deliberately to show that the result is "2.35", Don.
The function rounds up, I didn't; my error. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - Don Kinzer - Jan 11 15:29:00 2005


--- In , "Tom Becker" <gtbecker@r...> wrote:
> I chose 2.345 deliberately to show that the result is "2.35".
> The function rounds up.

You're right, it is rounding up. I'm more accustomed to
seeing "statistical rounding" where 2.345 would be rounded to 2.34
but 2.315 would be rounded to 2.32.




(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - Ed - Jan 12 5:45:00 2005


Thanks!

Ok, what I'm trying to do is to calculate a value and store it in A,
then when I print this value I want it with only 2 decimals.

I tryed your example but I don't seem to get it, sorry I'm a bit new
to this.
--- In , "Tom Becker" <gtbecker@r...> wrote:
> > ... Any hint on how to get the uC to show up (print on monitor)
only 2
> decimals?
>
> I can suggest two methods:
>
> 1- Function Fmt(2.345, 2) yields string "2.34", and
> 2- You can construct the equivilent with Mid, cStr and cInt. > Tom >
> Tom Becker
> --... ...--
> GTBecker@R... www.RighTime.com
> The RighTime Clock Company, Inc., Cape Coral, Florida USA
> +1239 540 5700






(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

RE: Re: only two decimals - Tom Becker - Jan 12 8:21:00 2005

> ... calculate a value and store it in A, then when I print this value
I want it with only 2 decimals.

OK, let's print the value of the squareroot of five, to two decimal
places.

Sub Main()
dim sA as single
sA = 5.0 ^ 0.5
Degug.Print Fmt(sA, 2)
End Sub

You should see 2.24, although the value is ~2.23607. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - Don Kinzer - Jan 12 11:51:00 2005


--- In , "Ed" <esteyezz@y...> wrote:
> I tryed your example but I don't seem to get it...

Insufficient data. Show us exactly what you tried and and describe
what you observed. Someone may be able to spot what change is needed.





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - Ed - Jan 13 4:17:00 2005



Thanks a lot!

That is what I wanted, it is working now!!

The program is reading the output of an accelerometer (ADXL202) and
I wanted it to display only 3 decimal points for every measurement.

I have the accelerometer set to 1000 samples per second but I'm only
getting about 20 from the uC, any idea how fast can the uC take
samples?






(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

BasicX - ADXL202 speed - Ed - Jan 13 11:31:00 2005


Ooopppss, I forgot about the serial port while monitoring the unit.

Without it i get about 200, any good? Can it go faster?

Thanks a lot for the help






(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

RE: BasicX - ADXL202 speed - Tom Becker - Jan 13 12:42:00 2005

> ... Can it go faster?

This is a classic issue of throughput, the combined limitations by the
source rate, the input rate, the processing rate and the output rate.

It seems to me that you can control all four. So, to see how fast you
can input data alone, do little processing and little output. For
example, just measure input rate, maybe for 10 seconds, and display it
once, or loop, counting for a second and display it once per loop.

You can answer your question. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: BasicX - ADXL202 speed - Ed - Jan 14 5:49:00 2005


It is what I was thinking. I'm now simplifying my code to see if I
can gain a little bit of speed.

I'm interested on measuring frequencies below 20Hz but i wouldn't
mind to have a bandwidth [BW] of 0-100Hz. At the momento I'm just
looking for specific values coming from the accelerometer but i was
thinking this:

Could it be possible to to sample a signal for 10 seconds and then
do an FFT analysis and output the values Freq. vs Amplitud? Do you
think the uC is powerful enought for this?

The other option I was thinking is to do a similar thing, sample
10seconds and send it to the computer, then here the PC makes the
FFT calculations and refreshes it every 10 seconds (or 5 or any
time). My problem is that I was thinking to do this using a free FFT
analyser for audio but of course, usually audio cards don't go below
20Hz so I have to find my own ways. For the kind of BW I'm
interested, it shoudn't be a lot of work, shouldn't be?

Thanks a lot for your support!!

Ed
--- In , "Tom Becker" <gtbecker@r...> wrote:
> > ... Can it go faster?
>
> This is a classic issue of throughput, the combined limitations by
the
> source rate, the input rate, the processing rate and the output
rate.
>
> It seems to me that you can control all four. So, to see how fast
you
> can input data alone, do little processing and little output. For
> example, just measure input rate, maybe for 10 seconds, and
display it
> once, or loop, counting for a second and display it once per loop.
>
> You can answer your question. > Tom >
> Tom Becker
> --... ...--
> GTBecker@R... www.RighTime.com
> The RighTime Clock Company, Inc., Cape Coral, Florida USA
> +1239 540 5700






(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

RE: Re: BasicX - ADXL202 speed - Tom Becker - Jan 14 14:20:00 2005

> ... sample a signal for 10 seconds and then do an FFT...

I doubt you would have sufficient RAM with the BX-24, which has about
400 bytes to work with. A BX-01 with additional RAM, though, could do
an FFT. Otherwise, your last idea of doing the FFT on a desktop might
be best, I think.

It would not be difficult to collect your accelerometer data with a
BX-24 and send it to a desktop machine in real time. With that data
your desktop app could build a .wav file, which you could then examine
in detail with any of many audio editors like CoolEdit. Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700




(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

Re: only two decimals - w7mjr - Jan 19 9:47:00 2005


Note the documented restriction in the range of the number...

From BASIC EXPRESS COMPILER REVISION HISTORY V2.01

"[...] Fmt is a low-RAM version
of VB's Format function, and converts floating point numbers to
strings with a user-specified number of digits aft of the decimal
point. The new function handles numbers in range -999 to +999, and 0
to 3 digits aft of the decimal point."





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )

RE: Re: only two decimals - Tom Becker - Jan 19 12:00:00 2005

In addition to Don's functions shown earlier in the thread, you can roll
your own display format function, of course. Here's one example that
displays a signed value, -10.0000 < sIndicatedError < +10.0000, to three
decimal places, rounded high:

Dim strX as string, strZ as string
If 0.0>sIndicatedError Then
strX=" -" 'show sign
Else
strX=" +"
End If
strZ=cStr(cLng(Fix(Abs(sIndicatedError * 1000.0 + 0.5) +
10000.0)))
strX=strX & Mid(strZ, 2, 1) & "." & mid(strZ, 3, 3)
Call PutStr_1(strX) ' -x.xxx Tom
Tom Becker
--... ...--
www.RighTime.com
The RighTime Clock Company, Inc., Cape Coral, Florida USA
+1239 540 5700





(You need to be a member of basicx -- send a blank email to basicx-subscribe@yahoogroups.com )