Welcome to ROSMASTER X3 Plus repository
4.13 Timer captures the encoder data
13. The timer captures the encoder data
13. The timer captures the encoder data 13.1. Experimental purpose 13.2. Configuration pin information 13.3. Analysis of the experimental flow chart 13.4. core code explanation 13.5. Hardware connection 13.6. Experimental effect
13.1. Experimental purpose
Use the timer encoder mode function of STM32 to capture the encoder data.
13.2. Configuration pin information
- Import the ioc file from the Motor project, name it Encoder, and then the related driver of serial port 1.
According to the schematic diagram, the two encoders of the four motors are respectively connected to channel 1 and channel 2 of timer 2 3 4 5. The motor M1 corresponds to the timer TIM2, M2 corresponds to TIM4, M3 corresponds to TIM5, and M4 corresponds to TIM3.

- Taking the timer TIM2 as an example, the setting methods for TIM3, TIM4, and TIM5 are the same. Select Combined Channels mode as Encoder Mode encoder mode.

- In the settings, change the Encoder Mode to Encoder Mode TI1 and TI2, set it to quadruple frequency, and other parameters are shown in the figure below.

The final chip configuration pins are shown in the figure below:

13.3. Analysis of the experimental flow chart

13.4. core code explanation
- Create new bsp_encoder.h and bsp_encoder.c, and add the following content to bsp_encoder.h:

- Create the following content in the bsp_encoder.c file:
Encoder timer initialization.

- Read the data from the encoder.

- Update the count value of the encoder. It needs to be called every 10 milliseconds.

- Return the count of encoders that have been counted up to now, Encoder_Get_Count_Now returns one way, and Encoder_Get_ALL returns four ways.

- Add the content of encoder initialization in the Bsp_Init() function.

- The new encoder array is used to save the data of the encoder, and show_encoder is used to print the count of the encoder.

- On the basis of the original control motor, the function of printing encoder data every 100 milliseconds is added.

13.5. Hardware connection
The motor connecting line needs to be connected to the corresponding motor as shown in the figure below, otherwise it may cause the problem that the program does not match the phenomenon. Motor 1 corresponds to the motor in the upper left corner of the body, Motor 2 corresponds to the motor in the lower left corner, Motor 3 corresponds to the motor in the upper right corner, and Motor 4 corresponds to the motor in the lower right corner.

Since the power of the motor is relatively large, the expansion board should not be powered by USB 5V directly, but must be powered by DC 12V.
Then connect the micro-USB cable to the expansion board and computer.
13.6. Experimental effect
Since the motor will turn when started, please stand up the trolley before the experiment, and the motor wheels are suspended in the air to avoid rampage.
After the program is programmed, the LED light flashes every 200 milliseconds. Open the serial port assistant, you can see the data of the encoder. Press the first forward, the second free stop, the third backward, and the fourth brake to stop.
