Ohms law basic calculations for calculating R2 in voltage divider when R1 is known and to calculate voltage drop ratio when both R1 and R2 are known.
Revision 0:fe642275688d, committed 2016-03-07
- Comitter:
- joeata2wh
- Date:
- Mon Mar 07 23:27:37 2016 +0000
- Child:
- 1:0b105d5d31a3
- Child:
- 3:b4592b0ae1e3
- Commit message:
- test
Changed in this revision
ohms.cpp | Show annotated file Show diff for this revision Revisions of this file |
ohms.h | Show annotated file Show diff for this revision Revisions of this file |
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ohms.cpp Mon Mar 07 23:27:37 2016 +0000 @@ -0,0 +1,40 @@ +#include "mbed.h" +//#include "ohms.h" + + +float volDivideCalcRatio(long r1, long r2) { + return ((float) r2 / (float) (r1 + r2)); +} +float voltDivideAdjVolt(float vin, long r1, long r2) { + float ratio = ((float) r2 / (float) (r1 + r2)); + return vin / ratio; +} + + + + +long calcResistV(long refResistR1, float maxV, float measuredV) { + float VDrop = maxV - measuredV; + float dropRatio = measuredV / VDrop; + return (long) (refResistR1 * dropRatio); +} + + +/* + +adjVoltDivide::adjVoltDivide(long r1, long r2, float refVolt) { + _r1 = r1; + _r2 = r2; + _refVolt = refVolt; + _ratio = volDivideRatio(r1, r2); +} + +float adjVoltDivide::read(AnalogIn apin) { + return apin.read() / _ratio; +} + +uint16_t adjVoltDivide::read_u16(AnalogIn apin) { + return (int) ((float) apin.read_u16() / _ratio); +} + +*/ \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ohms.h Mon Mar 07 23:27:37 2016 +0000 @@ -0,0 +1,64 @@ +/* Ohms law used to calculate things like resistance from voltage drop +and to compute read voltage adjusted for dividor. */ + +#ifndef ohms_H +#define ohms_H +#include "mbed.h" + +float volDivideCalcRatio(long r1, long r2); +float voltDivideAdjVolt(float vin, long r1, long r2); +long calcResistV(long refResistR1, float maxV, float measuredV); + +/* Uses a voltage dividor to compute resistance of +of R2 based on a known R1 and a source (refVolt) based +on drop of voltage measured where R1 and R2 connect. +normaly used to measure resistance in variable resistance +circuits such as thermister. + +class VoltDivideAdjVolt { + + public: + resistVoltDivide(PinName pin, long r1, float refVolt) { + _r1 = r1; + _refVolt = refVolt; + _pinName = pinName; + } + ~resistVoltDivide(); + float read(); + + + private: + PinName _pin; + long _r1; + long _r2; + float _refVolt; +}; +*/ + +/* adjVoltDivide Compute the actual voltage output after effect +of voltage dividor has been removed. Normally used to compute +things like true battery voltage when voltage has been level +shifted into a range safe for use with micro-controller Assumes +R1 is connected to source voltage = refVolt and R2 is connected +to ground measurement is where R2 and R1 meet. Result of read() +will be voltage as if it had been measured without influence of +divider. +class adjVoltDivide { + + public: + adjVoltDivide(long r1, long r2, float refVolt); + ~adjVoltDivide(); + float adjust(float vin); + float adjust(int adcin); + float read(AnalogIn apin); + uint16_t read_u16(AnalogIn apin); + float ratio(); + + private: + long _r1; + long _r2; + float _refVolt; + float _ratio; +}; +*/ +#endif \ No newline at end of file