📜 ⬆️ ⬇️

Determination of resistance by the controller without ADC

Actually, there was such a task - several times per second to take readings of a potentiometer. The target information receiver is an ATmega32 controller. It has a built-in ADC, but the result returned by it had a resolution of 2-3 bits, and the remaining bits carried garbage.

The first thing that came to mind was to buy an external ADC and screw it to the controller. But there was neither the time nor the desire to deal with another device. I decided to try another method, which I was not sure about in working capacity, but nevertheless the scheme turned out to be quite working and gave an accuracy of about 8 bits (maybe more, did not check).


Actually the idea is that the digital input of the controller measures the discharge rate of the capacitor. The capacitor is charged to the supply voltage through a low resistance, then the time report is started, and the capacitor discharges through the potentiometer. When the voltage on the capacitor drops to 1.4V (the switching voltage of the controller input is low), the value of the logic level at the input switches and the time report stops. The resulting discharge time is proportional to the current resistance of the potentiometer.
')
Device layout:

image
A0, A1 - legs MK, digital port A.

The transistor in the circuit is used to ensure that the voltage to which the capacitor is charged does not depend on the current position of the potentiometer.

Calculation scheme.

To ensure good measurement accuracy, it is necessary to select the circuit elements in such a way that the discharge time at the extreme positions of the potentiometer would differ by about an order of magnitude. In general, the more the better, but everything has a side-alley.
The formula for the discharge time of a capacitor through a resistor (wiki):
image

It is necessary to select R and C in such a way that to ensure the discharge time from 5V (WU) to 1.4V (level of switching the port to logical 0) for a time of 0.02 seconds at maximum resistance, and at a time of 0.002 seconds at minimum resistance. The value of C is constant in both cases, and R differs by 50 kΩ (potentiometer value). The time was chosen for reasons of accuracy, the speed of the microcontroller, and is chosen to most closely match the available values ​​of resistors and capacitors. (the controller operates at a frequency of 8 MHz)
Solving the system of equations, we determine the value of resistor R13 and capacitor C1.
image

It does not take into account the internal resistance of the input of the controller, but the speed of the controller allowed to programmatically adjust the circuit and select the coefficient to convert the discharge time to the range 0..255.

R12 is used to charge the capacitor and should be small, R14 should provide for the saturation of the transistor at any possible current.
The final selection of the values ​​of the elements of the scheme:

R12 = 220 Ohm;
R13 = 6.8 kΩ;
R14 = 1 kΩ;
R 15 = 0..50 kΩ;
C1 = 1 μF.

To charge the capacitor, the transistor is turned off, and the A0 foot is tuned to high output (DDRA = 3, PORTA = 1). and a delay is made for a sufficiently long time to charge the capacitor. Then A0 switches to the input state, and the transistor opens (DDRA = 2, PORTA = 2). the capacitor begins to discharge, the discharge time is tracked by an empty cycle.

The following is the actual function code that measures the discharge rate.

uint8_t readADC() { //    DDRA = 2; PORTA = 2; int i = 0, t = 0; while (PINA & 1){ t++; if (t == 43) i++, t = 0; //  43   } PORTA = 1; //      DDRA = 3; //   , //   ,    //      i -= 40; //   if (i < 0) i = 0; //        if (i > 255) i = 255; return i; } 


The circuit works well, the removed 8 bits are stable. It is probably possible to speed up the operation of the circuit by recalculating the parameters for a shorter time, but the limitations to this are the input parasitic input capacitance and the operating time of the controller.
I did not check it, but I think that it is possible to reduce the discharge time by a factor of 10 to 20, and this will not affect the accuracy of the result.

Source: https://habr.com/ru/post/135135/


All Articles