|
Re: Automatic counter-Encoder
Oh jeeez. Look, how fast is this "clicker" going to be clicking? If it's slower than 38 times per second you could have the code to do this in your slow loop. If it's slower than about 300 times per second (your wheel is turning slower than 600 RPM) then you can almost certainly put the checking code in your fast loop and just not worry about the complication of interrupts. The code itself is simple.
count = (count + ((dig_in ^ old_dig_in)?1:0))%30;
old_dig_in = dig_in;
And you're done. ^ is the bitwise XOR operator, it's true if both arguments aren't the same. So if the current input isn't equal to the old input, you add 1. Otherwise you add 0. And then you modulo by 30.
Unless you absolutely have to not miss any of the clicks and it's turning at some ludicrous speed, just plop that in your code with dig_in replaced by the actual digital input and don't worry about interrupts.
__________________
The difficult we do today; the impossible we do tomorrow. Miracles by appointment only.
Lone Star Regional Troubleshooter
|