How it works
Initially we set PORTA pin 0 as an output and we will always be sending a logic 1 to the pin whenever it is set to an output. So this means that we have +5v on PORTA pin 0 which will charge the capacitor to +5v (the 150 ohm resistor is there to limit the charging current - but still allowing the capacitor to charge quite fast.
Then we set PORTA pin 0 to an input (effectively removing the +5v that was there to charge the capacitor) as soon as we set PORTA pin 0 to an input, we check if there is a logic 0 there or not. (of course straight away there will be a logic 1 because the capactitor has had NO time to discharge.) So therefor since we are not at a logic 0, we increment the counter by one, then we again check to see if is a logic 0 yet. If not we increment the counter by one more.
We keep in this loop until the capacitor has discharged which will give us that logic 0 we were looking for. Now that we have this logic 0, we stop incrementing the counter.
The time it takes the capacitor to discharge is determined by the 10 ohm variable resistor and the 1k resistor. The greater the resistance - the less current we will have and therefor the slower the discharge. This means we will end up with a greater number in our counter variable. (i should mention that the 1k resistor is there to prevent the 10 ohm resistor from ever getting to 0 ohms)
So therefor, the number stored in the counter variable is relative to the position of the variable resistor. to give us a visual indication of what is going on, we output the counter variable straight to PORTB for display on the LED's. Now this is where you can do your calibration.
|