ADC Calculations

A project log for Logic probe using ATTINY26

A Logic Probe with the usual LEDs and sound output based on an ATTINY26. My entry to the 1k challenge.

Klaus DormannKlaus Dormann 12/24/2016 at 11:420 Comments

You may have noticed that the ADC is used in 8-bit mode as the ADLAR bit (ADC left adjust result) is set in ADMUX. At the same time we only use ADCH to determine the voltage thresholds. The reference voltage is set to Vcc. So we get 0 for ground and 255 for Vcc. Now we need to use the rule of three (also called the rule of proportion) to find the ADCH values for a certain percentage or voltage.

For CMOS the voltages are defined as <= 30% Vcc for low, >= 70% for high.

Since we can only compare an integer with ADCH we need to cut off the result to an integer. The compare results are based on the carry flag of the compare. 0 = same or higher, 1 = lower. As we want to compare the lower threshold to be lower or equal we need to elevate the lower threshold by 1. I used ADClow = 77 and ADChigh = 178.

For TTL the voltages are defined as Vcc = 5V, Vlow = 0.8V and Vhigh = 2V

Again we need to only use the integer part and elevate the lower threshold by 1: ADClow = 41 and ADChigh = 102

Why to cut off and not to round? Remember, the 2 least significant bits of the ADC result are also cut off, so an ADC result of 76.75 is still lower than 77. If we would have rounded 76.5 + 1 we would compare to 78 and that would actually be less accurate.