1. Encoder principle
What is orthogonal? If the two signal phase is 90 degrees apart, the two signals are called orthogonal. Since two signals are 90 degrees apart, it is possible to determine the direction according to which of the two signals whichever is first.
The TI12 mode is used here, for example, when the T1 rising edge, T2 at low level, T1 falling edge, T2 at high power, counting up, the advantage is that when there is Burr generated, it will automatically +1-1 filter out the burr.
2. Interrupt of Encoder
Because the encoder is timer-based, the interrupt of the encoder is actually the interrupt of the timer. That is, the timer is to add a number (or minus a number) at a certain time, when the number arrives at the preset value of the interrupt, and the encoder is each effective pulse to add a number (or minus a number), when the number reached the preset value of the interruption. If the preset value is 1000, the encoder is different from the timer interrupt, when the encoder reversal value reaches 999 to produce an interrupt, and when the encoder is turning to 0, the same interrupt occurs. The two interrupts on the hardware are indistinguishable, which creates a case of miscalculation.
3. STM32 Encoder does not consider the situation
Imagine if the encoder has a preset value of 1000, and if one time we make the encoder forward interrupt, what should be done immediately after the reversal? According to the above, this time there will be two of the same interruption. If there is no processing on the algorithm, it is very likely to think that it is a two-time walk forward. But not really. So this time must be combined to determine the direction of the walk (the direction of Use dir register bit) or read the count register bit once the interrupt is generated (see whether it is 999 or 0, in order to determine the current direction). Only the last one is positive and this time the same is true, the distance is the sum.
4. STM32F1 Series Timer 16-bit problem
I use STM32 timer 3 work in the encoder mode, can get the encoder position, but because now only 16 bits count, the number of bits is not enough, I want to expand to 32 bits, you can first use the timer internal connection, the timer 3 overflow to the timer 2, with a timer 2 to do high, in the normal turn of the time, But the reverse is wrong. A change of mind, do a 10ms generation of interrupts, call the following code in the interrupt, the following code is Max_count is more than the maximum number of possible counts in 10ms, Encoder_tim_period is the period value of the timer, preferably than max_ Count is large, then define a 32-bit signed variable, such as Currentcount, and then execute Currentcount + = Enc_getcount () every 10ms, and only need to read the value of Currentcount to get the displacement information.
S16 enc_getcount (void)
{
static U16 LastCount = 0;
U16 Curcount =encoder_tim->cnt;
S32 dAngle = Curcount-lastcount;
if (dangle>= max_count) {
DAngle-=encoder_tim_period;
}elseif (DAngle <-max_count) {
DAngle +=encoder_tim_period;
}
LastCount = Curcount;
Return (S16) DAngle;
}
5. Some other information that we get
1. Encoder has a speed limit, exceeding this limit is not normal work, this is the limit of hardware, in principle, the more the number of lines, the lower the speed, this in the selection should be noted that the output of the encoder is generally open-drain, so the MCU IO must pull up the input state.
2. When the timer is initialized, the value of the CNT register at any time is the position information of the encoder, and he will add a reversal he will reduce this part is not required software intervention, the initialization of the Tim_period value should be the Code consolidation Circle scale value, in the reduction overflow will be automatically corrected to this number. Add more than this value back to 0 .
3. If an overflow interrupt is required to expand into a multiturn count, the loop count on the program is added and the direction bit is reduced.
4. The input pin of each timer can be filtered by software setting
5. In the application, if there is no absolute position signal or the initialization has not received the absolute position signal before the count can only be a relative count. After receiving the absolute position signal, re-modify the value of CNT once. The code tray generally has 0 position signal, Combine to the timer to capture the input on the line. After power-up, you can find this position by moving back and forth.
6. Even if there is a filter count value occasionally there will be a wrong situation, a lap more than one or a few count a number is very normal especially when the speed is relatively high, it is very important to have an absolute position signal correction is necessary. Absolute position signal does not need to be at the zero position point, When this signal is received, the CNT is modified to a fixed value.
7. The input interrupt of the timer can achieve the effect of each step count, but you may not be able to handle it at high speed.
STM32 quadrature Encoder Drive Motor