Hi. I have an assignment at faculty to measure speed using incremental optical encoder (someone is using term rotary).
Have anyone done the similar project maybe? I get 2 analog signals and I use DA converter at begging. I have several questions, at the begging do I have to debounce the signals, or how to measure the frequency of signals (I guess I can't use interrupts). I am using MSP430 microcontroller
On the contrary you should use external interrupt for the encoder. I don't use your MCU so I don't know about this MCU's interrupt handling, but I'm telling you this:
You must not loose encoder interrupts. Make sure from your program or settings that this interrupt will always be enabled and hence executed.
So if encoder A will be connected to external interrupt, then if you implement a state machine with A and B states, you will be able to measure not only speed (you can use a dedicated timer for this to measure the time between interrupts), but direction also (from state machine).
Take a look at the following link and pay attention at the incremental rotary encoder part (the gray coding for clockwise and counter-clockwise are actually the state machine for the two directions):
what help do u want? is it software or hardware help? you can (of course) make frequency counter. I suggest you use A simple OP-AMP like LM324 having 4-QUAD INPUT. It makes you easier to debug the whole process.
I need help for software. I thing that I can't use interrupts because I have to take the signal from encoder to the A/D converter. So my question is do I need do debounce the signal I get from the A/D converter?
I can't use any additional hardware, only software debounce (if I have to, but I don't have experience with optical encoder so I don't know do I need to do this)
Maybe your board uses a frequency to voltage conversion.
Encoder is a fast changing input. You could debounce the analog signal, but I guess you must define zones of operation. For example 0.5-1.5V=low speed, 1.5 to 2.5=medium speed, 2.5 to 3.5=high speed. At the given example, if you counted 1.4V until now and suddenly you see a value of 1.7V, count it for some milliseconds first, and then consider it as a valid state changed (low speed became medium speed). Practically and depending on the application, maybe more zones are needed.
However, you could first try without debounce to see what happens and if you are not satisfied with the result, then insert the debounce code.
You should clarify which "analog" signal properties require to connect an AD converter to the encoder interface. As long as the signal swings beyond the VIL and VIH threshold values, it may be connected to digital IOs as well.
But if you would need to use ADC inputs, it's possible of course. You have to consider the ADC sampling and conversion time, so the maximum speed will be lower than with digital interfaces. Generally, the signals of a quadrature encoder don't need to be necessarily debounced. The decoding scheme involves a correct position count despite of bouncing signals. As you'll notice from th eliterature, a quadrature encoder signal is equivalent to a two bit gray counter, by design, only on eof two channels can bounce at a time, in turn, the counter position can be unsure by one position.
It should be considered, that some simplified decoder circuits may miss this elementary feature, e.g. decoders that are only processing one signal edge of the encoder. The circuit in post #3 is however working correctly, also with bouncing inputs. But it has the disadavantage of not cointing on each quadrature transition.
The purpose of work is not clear. You want to measure speed or angle ?
If is only speed, a tachometer counter could be enought, but direction rotation information will be lost.
However if is angle, should consider Index pin, to resset account and correct some accumulated error, every time turns at position 'zero'.
PS : Some microcontrolers, like DSPIC30F60xx, have built-in quadrature decoder.