Briefly describe the method of using STM32 timer to count and control the orthogonal encoder

Publisher:lqs1975Latest update time:2017-11-02 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

As shown in the figure, each TIMER of STM32 has an orthogonal encoder input interface. TI1 and TI2 are input filtered, and edge detection generates TI1FP1. TI2FP2 is connected to the encoder module. By configuring the working mode of the encoder, the encoder can be counted forward/reverse.

Briefly describe the method of STM32 timer to control the counting of orthogonal encoder 
As shown in the figure below, the encoder uses two-phase signals A and B, but I only need to count the TI1 signal (the first line). I just discovered this error. It turns out that counting both signals results in more than 100 pulses (100-line photoelectric encoder) in one rotation of the encoder disk. By comparing the two level signals through the STM32 encoder module, it is easy to calculate the operation of the encoder.

Briefly describe the method of STM32 timer to control the counting of orthogonal encoder 
The following is my debugged OK code:


  1. void Encoder_Configration(void)

  2. {

  3. GPIO_InitTypeDef GPIO_InitStructure;

  4. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  5. TIM_ICInitTypeDef TIM_ICInitStructure;


  6. //PC6 A phase PC7 B phase

  7. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;

  8. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;

  9. GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

  10. GPIO_Init(GPIOC,&GPIO_InitStructure);







  11. TIM_TimeBaseStructure.TIM_Prescaler = 0x0; // No prescaling 

  12. TIM_TimeBaseStructure.TIM_Period = 10000; 

  13. TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

  14. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; 

  15. TIM_TimeBaseInit(TIM8, &TIM_TimeBaseStructure);


  16. TIM_EncoderInterfaceConfig(TIM8, TIM_EncoderMode_TI12, 

  17. TIM_ICPolarity_Rising, TIM_ICPolarity_Rising);

  18. TIM_ICStructInit(&TIM_ICInitStructure);

  19. TIM_ICInitStructure.TIM_ICFilter = 6;//ICx_FILTER;

  20. TIM_ICInit(TIM8, &TIM_ICInitStructure);


  21. // Clear all pending interrupts

  22. TIM_ClearFlag(TIM8, TIM_FLAG_Update);

  23. TIM_ITConfig(TIM8, TIM_IT_Update, ENABLE);

  24. //Reset counter

  25. TIM2->CNT = 0;


  26. TIM_Cmd(TIM8, ENABLE); 


  27. }


  28. n_Counter = TIM_GetCounter(TIM8);

  29. Diled_Disp_Num((float)n_Counter);



Another point worth noting is that the STM32 timer is 16 bits, which means it can only count to 65535. There are two ways to do this. One is to use two timers in a chained manner to expand 16 bits to 32 bits. Another simple method is to enable the overflow interrupt of the timer. Each interrupt represents that the encoder has run a specific angle.

For example, if the encoder has 400 lines, set the ARR register to 400. Each overflow interrupt represents that the motor has turned a circle, and so on.

In addition, the detection of input pulses is actually similar, except that an external trigger module is used inside the STM32 to implement it. As shown in the purple box in Figure 1, the encoder module should be an upgrade of this module. The following is the configuration code:


  1. void TIM3_ETR_GetDropCounts_Configuration(void)


  2. GPIO_InitTypeDef GPIO_InitStructure;

  3. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

  4. //test PA0 TIM8_ETR

  5. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;

  6. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;;

  7. GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;

  8. GPIO_Init(GPIOD, &GPIO_InitStructure);


  9. TIM_TimeBaseStructure.TIM_Prescaler = 0x00;

  10. TIM_TimeBaseStructure.TIM_Period = 0xFFFF;

  11. TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;

  12. TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

  13. TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); // Time base configuration


  14. TIM_ETRClockMode2Config(TIM3, TIM_ExtTRGPSC_OFF, TIM_ExtTRGPolarity_NonInverted, 0);


  15. TIM_SetCounter(TIM3, 0);


  16. TIM_Cmd(TIM3, ENABLE);

  17. }


Keywords:STM32 Reference address:Briefly describe the method of using STM32 timer to count and control the orthogonal encoder

Previous article:STM32——JLINK downloader flash firmware
Next article:Problems encountered when porting cjson to stm32

Recommended ReadingLatest update time:2024-11-16 14:48

STM32 external interrupt configuration
1. Placement suspension void NVIC_Configuration(void) {   NVIC_InitTypeDef NVIC_InitStructure;   /* Set the Vector Table base location at 0x08004000    NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x4000); // 1. Allocate interrupt vector table   NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //Set interrupt priority   /*
[Microcontroller]
STM32 processor timer comparison output test program
      Here are the three most critical files main.c file #include   "stm32f10x.h" #include   "stm32f10x_tim.h" #include   "misc.h"   unsigned int CCR3_Val=5000;  unsigned int CCR4_Val=10000;  extern void time_ini(void);     void RCC_Configuration(void) {   SystemInit();    RCC_APB2
[Microcontroller]
STM32 processor timer comparison output test program
STM32 timer--general timer output with dead zone complementary PWM
Function: Use a common timer to output 2 complementary PWMs with adjustable frequency, adjustable duty cycle and adjustable dead zone. Principle: As shown in the figure below, the counting mode is the center alignment mode It can be seen that the CH3 high level interval is centered when the counter counts to 4 and e
[Microcontroller]
STM32 stack knowledge
Write the following code on the STM32 platform: int main() { while(1); } BUILD://Program Size: Code=340 RO-data=252 RW-data=0 ZI-data=1632  After compiling, you will find that such a program has used more than 1600 RAM. Where did this 1600 RAM go? Analyzing the map, you will find that it is occupied by the heap and st
[Microcontroller]
STM32 stack knowledge
Understanding of STM32 interrupt function SysTick_Handler
Purpose Tick ​​timer summary (STM32F103). After using the tick timer for a while, I suddenly forgot how to configure it. I re-read the manual and blog post and recorded the memo here.  The reload register of SysTick determines the timer frequency. If the clock source of SysTick is 72M, SystemFrequency = 72000000Hz
[Microcontroller]
Understanding of STM32 interrupt function SysTick_Handler
Multi-channel voltage measurement design based on STM32
  1. Introduction   In recent years, data acquisition and its application have received more and more attention, and data acquisition systems have also developed rapidly. It can be widely used in various fields.   Data acquisition technology is one of the important branches of information science. Data acquisition
[Microcontroller]
Multi-channel voltage measurement design based on STM32
STM32 - Issues with redirecting printf to the serial port
Simply put: if you want to use printf in mdk, you need to redefine the fputc function and avoid using semihosting (semihosting mode).  The default output device of the standard library function is the display. To output to the serial port or LCD, you must redefine the functions related to the output device called in t
[Microcontroller]
Detailed explanation of eight input and output modes of STM32
When I was reading the data sheet recently, I found that there are 8 types of GPIO configurations in Cortex-M3: (1) GPIO_Mode_AIN analog input  (2) GPIO_Mode_IN_FLOATING floating input (3) GPIO_Mode_IPD pull-down input (4) GPIO_Mode_IPU pull-up input (5) GPIO_Mode_Out_OD open drain output (6) GPIO_Mode_Out_PP push-pul
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号