Voltage Divders Made Easy !

marks

Senior Member
Voltage Dividers Made Easy !

The maths is made easier if we select exact ratio's,we can use just two common available resistors.
The schematic shows some examples using an AXE132 with a regulator circuit
with transient protection usually good for about 200ma enough for an lcd or other display.
Just enter the correct Vin fullscale in our code ,if the regulator isint quite 5v we can tweak that a bit too.
Code:
#picaxe 18M2
#terminal 19200
SETFREQ M16
SYMBOL ADCvalue      = W3
SYMBOL Volts         = W4

Main:
ReadADC10  C.0,ADCvalue
 ReadADC10 C.0,Volts
 IF ADCvalue = Volts THEN ConvertV ' display equal ADCvalues
GOTO main

ConvertV:
Volts = Volts **64064       ' Convert 1023 to 1000 steps (**64064) for 5.00v reference
  'Volts = Volts **64333      ' tweaked a little 7805 output actually 5.021v /5.00 *64064

Volts = Volts *16           ' Multiply by Vin (Fullscale 16v)(R1 22K)(R2 10K)

Displaytest:
Sertxd (13,10,"ADCvalue ",#ADCvalue,"     ",#Volts," Volts ")' 016 resolution

  DisplayV:
  'Volts = Volts /100 
  'BinTOASCII Volts, b3,b2,b1 : IF  b3 = "0" THEN : b3 = " " : ENDIF' leading zero blanking  
  'Sertxd (13,10,"                ",b3,b2,".",b1," Volts")  ' 0.1 resolution
  
PAUSE 1000

GOTO main
 

Attachments

Last edited:

marks

Senior Member
The second example shows how we can use the Fixed Voltage Reference
using resistor values of the extended range now becoming more available
because we are using lower values some protection diodes have been added.
Code:
 #picaxe 18M2
#terminal 19200
SETFREQ M16
SYMBOL ADCvalue      = W3 : SYMBOL Decimal      = W3
SYMBOL Volts         = W4

Main:
FVRsetup FVR2048      ' internal Fixed Voltage Reference 2.048V
ADCconfig   %011        ' VRef- is 0V ,  VRef+ is FVR
    
ReadADC10  C.0,ADCvalue
 ReadADC10 C.0,Volts
 IF ADCvalue = Volts THEN ConvertV ' display equal ADCvalues
GOTO main

ConvertV:
Volts = Volts *50           ' Multiply by Vin (Fullscale 50v)(R1 36K)(R2 1.5K)

'Displaytest:
'Sertxd (13,10,"ADCvalue ",#ADCvalue,"     ",#Volts," Volts ")' 050 resolution

  DisplayV:
  Decimal = Volts //1000/100
  Volts = Volts /1000 
  Sertxd (13,10,"                ",#Volts,".",#Decimal," Volts")  ' 0.1 resolution
  
PAUSE 1000

GOTO main
 

Attachments

westaust55

Moderator
Suggest the 64064 comes from:

65536*1000/1023 with an accuracy to 3 decimal places.

However, suggest it is not achieving any greater accuracy than Volts = Volts * 1000 / 1023 though it does save 3 bytes of program space

I have used the ** operator myself in the past to quickly convert a binary value representing a fractional value to an integer value for quick SEROUT
For example:

Code:
; For the mantissa we now have a value where bit15 = 0.50, bit14 = 0.250, bit13 = 0.125, etc
; Move the value 4 decimal places to the left to create an integer value that we can understand
; so for example, 0.3010 appears as %0101100101110111 (=dec 119) but after "shift" will become 3010 
	LogMant = LogMant ** 10000
Similar can be used to take the lowest 4 bits from a DS18B20 reading (with READTEMP12) to determine the fractional part.
 
Last edited:

hippy

Technical Support
Staff member
65536*1000/1023 with an accuracy to 3 decimal places.
Thanks; I'd seen it used a couple of times but wasn't clear on how it worked or its theory. Quite a neat trick.

However, suggest it is not achieving any greater accuracy than Volts = Volts * 1000 / 1023 though it does save 3 bytes of program space
I guess its main benefit comes in avoiding overflow when ADC reading is greater than 65, which in most cases with READADC10 it will be. No overflow and three decimal place resolution.
 

marks

Senior Member
With the fractional part value multiplied into the highword you can
usually achieve accuracy to 4 decimal places.
As westaust55 has already shown we can work this out by
1000 /1023 *65536 +2 = 64063
very handy for improving normal maths and reducing our code.
 
Last edited:

Rampz

Well-known member
Voltage Dividers Made Easy !

The maths is made easier if we select exact ratio's,we can use just two common available resistors.
The schematic shows some examples using an AXE132 with a regulator circuit
with transient protection usually good for about 200ma enough for an lcd or other display.
Just enter the correct Vin fullscale in our code ,if the regulator isint quite 5v we can tweak that a bit too.
Code:
#picaxe 18M2
#terminal 19200
SETFREQ M16
SYMBOL ADCvalue      = W3
SYMBOL Volts         = W4

Main:
ReadADC10  C.0,ADCvalue
ReadADC10 C.0,Volts
IF ADCvalue = Volts THEN ConvertV ' display equal ADCvalues
GOTO main

ConvertV:
Volts = Volts **64064       ' Convert 1023 to 1000 steps (**64064) for 5.00v reference
  'Volts = Volts **64333      ' tweaked a little 7805 output actually 5.021v /5.00 *64064

Volts = Volts *16           ' Multiply by Vin (Fullscale 16v)(R1 22K)(R2 10K)

Displaytest:
Sertxd (13,10,"ADCvalue ",#ADCvalue,"     ",#Volts," Volts ")' 016 resolution

  DisplayV:
  'Volts = Volts /100
  'BinTOASCII Volts, b3,b2,b1 : IF  b3 = "0" THEN : b3 = " " : ENDIF' leading zero blanking 
  'Sertxd (13,10,"                ",b3,b2,".",b1," Volts")  ' 0.1 resolution
 
PAUSE 1000

GOTO main
Hello

I'm wanting to do something like this on a 08m2 chip, I notice you have this on a bigger chip and quite a few years ago, I won't be using a display but seeing the sertxd output in the terminal will be useful, I'd worked out I needed 10k and 22k resistors to give 5v at the ADC pin for full scale resolution of 16v
So I will try and remove the LCD parts and give it a go
 
Top