LTspice sure works great, i just added a component from analog devices.
The AD8211 current shunt monitor.
A short example coarse about how to measure current with an MCU :
The AD8211 is great when used with a resistor with a value that is (with a little imagination) a power of 2. For example, 0.16 Ohms.
The AD8211 has an amplification of 20V/V. This means that it will amplify the voltage difference between the input pins with 20. 20 is with a little imagination also a power of 2.
Thus we can say the output voltage = 0.16*20*I.
If we would convert this to a digital value with an ADC, we have an ADC count. Now, why do i keep mentioning that power of 2.
Well, when the AD value is normalized and scaled because the MCU is a 32 bit (or a 16 bit) MCU, it is easy to normalize the value and scale it. Meaning multiply it by 100 or 1000. A 32 bit register can hold values signed from -2*10^9 to 2*10^9.
0.16 will become 16 when properly done and 20 will become 2 when properly done. This means that the value only needs to be shifted instead of using multiplication or division.
Shifting 1 bit position to the left is multiplying by 2.
Shifting 1 bit position to the right is dividing by 2.
This speeds up and makes the calculating for an embedded MCU a lot easier.
I want to measure 20mA to 1A.
Take note that i am discarding offset and calculation errors for a moment.
But since the MCU also sets the trip level by use of DAC, i have some leverage to calibrate away a reasonable amount of offset and measurement errors.
The 20mA limit i have because the output of the AD8211 has an average limit of 0.05 Volt low voltage. It will not get lower than that meaning i cannot measure any lower then 0.05 Volts. Everything below 0.05 is noise and can be discarded without proper tricks. Also 20mA is low enough for the intended purpose.
Thus i get 0.02A*0.16*20 = 64mV.
Or 1A*0.16*20 = 3200mV.
ADC = 10 bit = 3.3 / 1023 = 3.2mV. (ADC resolution = reference voltage divided by 1023 = [2^10] - 1)
To measure 20mA :
64mV / 3.2mV = 20 ADC counts.
To measure 1A
3200mV / 3.2mV = 1000 ADC counts.
If i use mA units. I do not even have to calculate in this example.
But in practice i have to add or subtract some offset and measurement error.
If i convert this value into ascii, i only have to shift a comma (and do leading zero suppression )to display mA.
With these values, it is very easy to measure currents.
Measuring lower currents
Of course, if we would use an extra resistor of for example 1.6 Ohms we could measure theoretically as low as 2mA. I = 64mV / (20 * 1.6)
If we would use 16 Ohms, we could measure theoretically as low as 200uA.
It is logical that the maximum measurement (full scale) goes down as well.
Changing measurement range could be done, by using 3 modern Nmosfets with a very low RDson (5milliOhm) And switch current sense resistors 0.16 / 1.6 /16 Ohm.
Use a software trick to do autoranging (ADC full scale or 0 is changing the currently selected resistor for another one.) It all depends on what the requirements are and the limits of creativity.
I made a small linear power supply simulation in LTspice that will be controlled by an MCU.
The current trip point to shutdown can be set as well.
Why do i go through all this trouble, you are thinking ?
I have this wish list for a power supply i have the components for but not the time. But i am slowly getting there...
It is one part of 4 individual sections (all electrically isolated) in one 19 inch casing.
large 4 line 20 character display.
0 to 5Volts. 20mA to 1A current trip / current source.
0 to 12 Volts. 20mA to 1A. current trip / current source.
0...12 Volts , -12Volt . 20mA to 1A. current trip / current source.
0...45 Volts. 20mA to 4A. current trip / current source.