当前位置:网站首页>Stm32cubemx quadrature encoder
Stm32cubemx quadrature encoder
2022-07-25 02:59:00 【ASWaterbenben】
Recently, I encountered the need to obtain the function of orthogonal coding , So come back and study STM32CubeMX Encoder function , It has been updated before STM32 Timer function , However, the principle of the quadrature encoder of the balance car was not clear , Just use the pulse input function directly to get things done , Now I'm paying for my former Youth , But see the existing Encoder The instructions are relatively simple , Therefore, it is considered to contribute to developers , After I research it clearly, I will write a blog .
Coming back to the book , The quadrature encoder is usually installed on the motor , Coaxial with the main shaft of the motor rotor , Some are connected to the encoder through the main shaft to reduce or accelerate the gear set , There are generally two kinds of encoders: grating encoder and magnetic pole encoder , The output pulses of forward rotation and reverse rotation are like the following figure 
STM32 It has the function of processing orthogonal pulses , That is, in the timer Encoder Pattern
Specific operation :
STM32CubeMX To configure
1. open STM32CubeMX, Choose your own chip , Here I use STM32F429IGT6 Take... For example ;
2. Set up the system 、 Basic operation of clock
SYS
RCC
3. Timer settings
Here I choose TIM3,TIM3 Bit Universal timer , There are many general timers for general chips
Combined Channels use Encoder Mode( Encoder mode )
After selecting this mode TIM3 Of CH1 and CH2 It becomes an encoder interface , Remember the corresponding two interfaces , Then connect the encoder signal to these two ports , My is PA6 and PA7
The parameter settings below are set according to the requirements , Because I need to record the rising and falling edges of the encoder , So here we choose Encoder Mode TI1 and TI2, This mode will AB The rising and falling edges of both sets of pulses are counted , It's counting 4 Time , Students who do not need to be so precise can put the pre frequency division coefficient (Prescaler) It is amended as follows 4-1, Then pulse detection 4 Pulses will count 1 Time , Filter depends on individual , I use DuPont line to connect directly , distance 10cm No, so I set 0, You can see how to choose STM32 Chinese Reference Manual , Lazy people look at the picture below 

4. Serial port settings
To facilitate observation , You need to send the corresponding rotation direction through the serial port , Rotate the count value to the serial port debugging assistant .
Because the chip is only used to send data , There will be no interruption , Directly select asynchronous serial port 
5. Finally, configure the clock tree
This is handled by everyone , Each chip is different , Finally let the middle HCLK(MHz) by 72 that will do 
6. Project configuration
The development environment I use is Keil5, So in Toolchain/IDE I chose MDK-ARM

When generating code, put Generated files Select the first item in , Otherwise h Document and c Documents are mixed , It's hard to look at 
7. Click on the top right corner GENERATE CODE Generate code
Code changes
1. Serial port code modification
For convenience , use printf Function output information , So it needs to be in the serial port /* USER CODE BEGIN 0 */ Add serial port redirection function , As shown below :
/* USER CODE BEGIN 0 */
#include <stdio.h>
struct __FILE
{
int handle;
};
FILE __stdout;
void _sys_exit(int x)
{
x = x;
}
int fputc(int ch, FILE *f)
{
while((USART1->SR&0X40)==0);// Cycle to send , Until it's sent
USART1->DR=(uint8_t)ch;
return ch;
}
/* USER CODE END 0 */
2.main.c modify
First, add stdio.h The inclusion of , Convenient for the back printf Output
stay main.c Of /* USER CODE BEGIN Includes */ Add the following code :
/* USER CODE BEGIN Includes */
#include <stdio.h>
/* USER CODE END Includes */
Then you need several variables , Respectively
| Variable name | Variable usage |
|---|---|
| Direction | Encoder rotation direction |
| counter | Main function count , be used for printf The timing segment of the function is sent |
| enc1 | Encoder count (0~65535) |
| enc1_old | Last encoder count (0~65535) |
| enc2 | enc1 Carry of |
| enc | Final encoder count |
Defining variables , stay /* USER CODE BEGIN 1 */ Add the following code
/* USER CODE BEGIN 1 */
uint8_t Direction;
uint16_t counter;
uint16_t enc1 = 0,enc1_old = 0;
int16_t enc2 = 0;
int32_t enc;
/* USER CODE END 1 */
Start encoder interpretation , stay /* USER CODE BEGIN 2 */ Add encoder startup function
/* USER CODE BEGIN 2 */
HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL);
/* USER CODE END 2 */
In circulation , That is to say /* USER CODE BEGIN 3 */ After that, add the reading encoder value function, carry processing function and print output function :
/* USER CODE BEGIN 3 */
Direction = __HAL_TIM_IS_TIM_COUNTING_DOWN(&htim3);
enc1 = (uint32_t)(__HAL_TIM_GET_COUNTER(&htim3)); // Get the value of the timer
if((Direction == 0) &(enc1 < enc1_old)) // The value of positive rotation decreases , Description carry
{
enc2++;
}
if((Direction == 1) &(enc1 > enc1_old)) // The reverse rotation value becomes smaller , Description borrow
{
enc2--;
}
enc1_old = enc1; // to update enc1_old, For the next calculation
enc = enc2<<16 | enc1; // Calculate the total value of the current count , belt +- Number
counter++; // Main function count
if(counter>1000) // The main function runs about 1ms, This is for every 1000ms Send once
{
counter = 0; // The count value is cleared
printf("Dir %d, Enc2 %d, Enc1 %d, ENC %d\r\n",Direction,enc2,enc1,enc);// Print relevant count data
}
HAL_Delay(1);
}
/* USER CODE END 3 */
At this point, the code has been completely modified , Compile and download to the chip , Wait for wiring test
Wiring test
The encoder I use is 5v A magnetic pole encoder for power supply , But the final interface should be VCC、GND、A、B Four interfaces , At this time, connect as follows
| Encoder | STM32 |
|---|---|
| A | TIM3_CH1 |
| B | TIM3_CH2 |
| VCC | 5V level |
| GND | And STM32 Common land |
After connecting, you can power on , Open the serial debugging assistant , Connect the serial port 1
You will receive the following data , At this time, you can see the change of the value by turning the encoder 
thus , Encoder function has been completed .
Good Game!!!!!!
Next, we will launch a series of sharing about serial port use , Apes in need, please pay attention !!!!!
You are welcome to reprint and quote the above content , Just mark the source !!!!!
边栏推荐
- Flume's study notes
- [stm32f130rct6] idea and code of ultrasonic ranging module
- How to take the mold for the picture of 1.54 inch TFT st7789 LCD screen
- MySQL common function summary, very practical, often encountered in interviews
- Flink's study notes
- "Introduction to interface testing" punch in day08: can you save all parameters to excel for test data?
- Domain driven model (DDD)
- C: wechat chat software instance (wpf+websocket+webapi+entityframework)
- JS written test questions -- random numbers, array de duplication
- Strategy mode, just read one article
猜你喜欢

Tp5.1 include include files (reference public files)

Tp5.1 initialize initialization method (not \u initialize)

Learning record XIII
![[stm32f103rct6] motor PWM drive module idea and code](/img/a5/3010acff73c8913e967ff3a024877e.png)
[stm32f103rct6] motor PWM drive module idea and code

Learning record 10

Jenkins plug-in development -- plug-in expansion

Dc-1-practice

C: wechat chat software instance (wpf+websocket+webapi+entityframework)

JS written test -- regular expression

Nuscenes data set summary
随机推荐
Pypi counts the number of Downloads
Edit mathematical formulas in markdown
SQL recursive follow-up
Operator explanation - C language
Ctfshow misc introduction
Learning record 10
Dynamic planning of force buckle punch in summary
Hashcode details
Concurrent programming day01
Domestic edge computing organization and product research
Time formatting
Dc-1-practice
JS written test question -- prototype, new, this comprehensive question
ICO objects in classification
6.0 cancellation of member registration verification code
JS written test questions -- random numbers, array de duplication
Arduino + si5351 square wave generator
Application method and practical case of sqlmap of penetration test SQL injection
Review all frames before sum of SSM frames
List.stream common operations