The general counter of stm32 is used to count the pulses of the encoder

Publisher:Jinyu521Latest update time:2018-07-19 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

This week, I worked on the encoder module for the computer mouse. It took me a whole week.
The process was really funny. We planned to use the CH3 and CH4 channels of TIM3 to collect the PWM of the left motor to measure speed and distance, and use the CH2 and CH1 of TIM4 to measure the right motor.
When writing the code, we first worked on the left motor. It was not until Friday that we found that the CH3 and CH4 channels of the general timer could not be used for PWM input. What a mess! ! The following is one of our configurations, which I would like to share with you!
  We use GPIO_B_7, the second channel of TIMER4


 void MY_CONFIG(void)
{
   NVIC_InitTypeDef NVIC_InitStructure;  
   GPIO_InitTypeDef GPIO_InitStructure;
   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
   /* TIM4 clock source enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
  /* Enable GPIOA, clock */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
 

GPIO_InitStructure.GPIO_Pin =  GPIO_Pin_7; 
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; 
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; 
GPIO_Init(GPIOB, &GPIO_InitStructure);

//TIM4 output initialization 
TIM_DeInit(TIM4);  
TIM_TimeBaseStructure.TIM_Period = 100; //Count value = 100-1 
TIM_TimeBaseStructure.TIM_Prescaler = 0; //Pre-scaling 
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //Clock division TIM_TimeBaseStructure.TIM_CounterMode 
= TIM_CounterMode_Down; //Mode 
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //Basic initialization

TIM_SetCounter(TIM4, 100); //Set TIM4 counter register value  
 //Set TIMx auto-reload register value 
 TIM_ARRPreloadConfig(TIM4, ENABLE);
TIM_SelectInputTrigger(TIM4, TIM_TS_TI2FP2); //If it is channel 1, it is TIM_TS_TI1FP1
//TIM_SelectInputTrigger(TIM2, TIM_TS_TI1F_ED); 
TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_External1); 
//TIM_SelectSlaveMode(TIM4, TIM_SlaveMode_Reset);

TIM_SetCompare2(TIM4, 100); //If one channel is used, then: TIM_SetComper1(TIM4,100);
TIM_Cmd(TIM4, ENABLE); //Start TIM4 
TIM_ClearFlag(TIM4, TIM_IT_CC2); //If one channel is used, then here and the following TIM_IT_CC2 are changed to TIM_IT_CC1
TIM_ITConfig(TIM4, TIM_IT_CC2, ENABLE)
//Interrupt configuration

NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQChannel;; // 
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 3; //Preemption priority 0 
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //Response priority 0 
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //Enable interrupt 
NVIC_Init(&NVIC_InitStructure); //Write settings 
}

void TIM4_IRQHandler(void) 
{

TIM_ClearFlag(TIM4, TIM_IT_CC2);


It's sad, I have to redraw the board. But I learned a lot about the general timer of stm32, keep it up


Keywords:stm32 Reference address:The general counter of stm32 is used to count the pulses of the encoder

Previous article:STM32 Quadrature Encoder Interface
Next article:STM32 timer encoder mode

Recommended ReadingLatest update time:2024-11-23 15:13

Detailed analysis of 8 modes of STM32 GPIO port (analogous to 51 MCU)
Regarding the 8 working modes of the STM32GPIO port, let us first raise some questions? What if the STM32 GPIO port needs both input and output? 1. Floating input mode     The red part in the above figure indicates the floating input process. When there is external input, 0 is read as 0, and when there is external in
[Microcontroller]
STM32 learning GPIO
1. First, the GPIO clock should be enabled. All GPIOs are mounted on the bus AHB1. The corresponding library function is RCC_AHB1PeriphClockCmd(uint32_t RCC_AHB1Periph, FunctionalState NewState); RCC_AHB1Periph is the required peripheral mounted on AHB1, and FunctionalState NewState can be ENABLE or DISABLE. Because i
[Microcontroller]
STM32 USB DFU device firmware upgrade
Speaking of STM32 USB UDF, it is actually what we often call IAP (In Application Programming). There are many ways to use IAP. I have used serial port IAP and network IAP before. Here we use USB IAP, which is to update the code through USB. So it is necessary to understand IAP online. IAP is the abbreviation of In Ap
[Microcontroller]
STM32 USB DFU device firmware upgrade
STM32 MCU key debounce and FPGA key debounce
Written in front: STM32 MCU key debounce and FPGA key debounce Key debounce: From the above figure, we can see that there is a difference between the ideal waveform and the actual waveform. The actual waveform has jitter at the moment of pressing and releasing. The length of the jitter time is related to the mec
[Microcontroller]
STM32 MCU key debounce and FPGA key debounce
stm32 PUSH button controls LED flashing
int main(void) {   /* USER CODE BEGIN 1 */     /* USER CODE END 1 */     /* MCU Configuration----------------------------------------------------------*/     /* Reset of all peripherals, Initializes the Flash interface and the Systick. */   HAL_Init();     /* USER CODE BEGIN Init */     /* USER CODE END Init */     /*
[Microcontroller]
STM32 system learning - DMA (direct memory access)
The main function of DMA is to transfer data, but it does not need to occupy the CPU, that is, when transferring data, the CPU can do other things, such as multithreading. Data is transferred from peripherals to memory or from memory to memory. The DMA controller includes DMA1 and DMA2, of which DMA1 has 7 channels an
[Microcontroller]
STM32 system learning - DMA (direct memory access)
STM32 hardware SPI drives 0.96-inch OLED
1.OLED related See —- 51 Software simulation SPI drive OLED 2. Hardware SPI See - SPI Topic (II) - STM32 driver FLASH (W25Q64) 3. Drivers The driver is transplanted with reference to the 51 MCU, except that the simulated SPI is replaced with the STM32 hardware SPI, and there is no need to write the timing co
[Microcontroller]
A preliminary study on STM32 general timer
STM32F103ZET6 has two advanced timers, TIM1 and TIM8, four general timers, TIM2-TIM5, and two basic timers, TIM6-TIM7. This article uses TIM3 to introduce the general timer of STM32. The timer of STM32 is powerful. This article takes a preliminary look at the general timer.   Paste the code below: void TIM3_In
[Microcontroller]
Latest Microcontroller Articles
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号