Comments on AN217

Discussion about the ZBasic language including the System Library. If you're not sure where to post your message, do it here. However, do not make test posts here; that's the purpose of the Sandbox.
Post Reply
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Comments on AN217

Post by mikep »

I am posting this as it may useful to other people. I added AN217 to my project with of course a separate thread to do the monitoring (every second). I would rather simply look at the count to determine if the voltage is over or under some threshold.

Code: Select all

Private Const threshold as UnsignedInteger = 514 ' 10V
My suggestion would be to change the readADC() function to return the unsigned integer count. For example purposes you can do the actual voltage calculation in Main as follows:

Code: Select all

Private Const divider_ratio as Single = 7.788 ' discovered from measurement
Debug.Print "Voltage = "; Fmt(  CSng(readADC(PS_Bit)) * (2.56 / 1024.0) * divider_ratio, 3)
Of course now the floating point arthimetric can be simplified to a simple multiply. Multiply is much faster than a divide.

Code: Select all

Private Const ratio as Single = 0.01947 ' pre-calculated constant
Debug.Print "Voltage = "; Fmt(  CSng(readADC(PS_Bit)) * ratio, 2)
3 decimal digits precision is probably too much. I would round to 1 or 2 digits (not shown here).
Mike Perks
stevech
Posts: 715
Joined: 22 February 2006, 20:56 PM

Post by stevech »

considered using fixed point in an 16 or 32 bit int?
mikep
Posts: 796
Joined: 24 September 2005, 15:54 PM

Post by mikep »

You may have misunderstood my intent. The original routine contains floating point math. I removed that math from the routine and used it outside only for displaying the output value for humans. For code purposes I suggested returning an integer and then do integer comparisons like this:

Code: Select all

Private Const lowBattery as UnsignedInteger = 520 ' approx 10V (520/52 = 10)

voltage = ReadADC(monitorPin)
If voltage < lowBattery Then
	Debug.Print "Low battery voltage"
End If
Mike Perks
Post Reply