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.
- ADCth = ADCHmax * Vth% / 100%
- ADClow = 255 * 30 / 100 = 76.5
- ADChigh = 255 * 70 / 100 = 178,5
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
- ADCth = ADCHmax * Vth / Vcc
- ADClow = 255 * 0.8 / 5 = 40.8
- ADChigh = 255 * 2 / 5 = 102
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.
Discussions
Become a Hackaday.io Member
Create an account to leave a comment. Already have an account? Log In.