Setting up the STM32's own hardware encoder interface

Publisher:数字梦行Latest update time:2018-09-16 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Here, channel 1 and channel 2 of TIM3 are used as level capture of AB items.


Since STM32 has its own hardware encoder interface, it can be used as long as it is set up. It is very powerful. The following program has passed the test.



void TIM3_Mode_Config(void)  

{  

    //u16 CCR1_Val = 2500;  

    //u16 CCR2_Val = 1000;  

    GPIO_InitTypeDef GPIO_InitStructure;  

  TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;  

    TIM_ICInitTypeDef TIM_ICInitStructure;  

    //TIM_OCInitTypeDef TIM_OCInitStructure;  

      

/*------------------------------------------------ ----------------*/  

      

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);  

    

      

  GPIO_StructInit(&GPIO_InitStructure);  

  /* Configure PA.06,07 as encoder input */  

  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;  

  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  

  GPIO_Init(GPIOA, &GPIO_InitStructure);  

      

/*------------------------------------------------ ----------------*/      

  

      

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Enable TIM3  

    TIM_DeInit(TIM3);  

    TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  

      

  TIM_TimeBaseStructure.TIM_Period =0xffff; //  

  TIM_TimeBaseStructure.TIM_Prescaler =0; //Set prescaler:  

  TIM_TimeBaseStructure.TIM_ClockDivision =TIM_CKD_DIV1; //Set the clock division factor: no division  

  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //Upward counting mode  

  //TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_CenterAligned1;   

    /*Initialize TIM2 timer*/  

  TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);  

      

    /*------------------------------------------------ ------------------*/  

    //Encoding configuration encoding mode  

    TIM_EncoderInterfaceConfig(TIM3, TIM_EncoderMode_TI12,   

                             TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); //TIM_ICPolarity_Rising rising edge capture  

  TIM_ICStructInit(&TIM_ICInitStructure);  

  TIM_ICInitStructure.TIM_ICFilter = 6; //Comparison filter  

  TIM_ICInit(TIM3, &TIM_ICInitStructure);  

    

    //TIM_ARRPreloadConfig(TIM3, ENABLE);  

 // Clear all pending interrupts  

  TIM_ClearFlag(TIM3, TIM_FLAG_Update);  

  TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); //Enable interrupt  

  //Reset counter  

  TIM3->CNT =0;  

      

      

    TIM_Cmd(TIM3, ENABLE); //Enable timer 3  

}  

void TIM_Init(void)  

{  

  TIM3_Mode_Config();  

}
 

 

  


 

In the main program, send periodically through the serial port.  

 

int main(void)  

{  

  SystemInit(); // 72m clock  

  SysTick_Init();  

    TIM_Init();  

    NVIC_Config();  

    GPIO_74HC595_Config();  

  while (1)  

  {  

        encoder_num=TIM_GetCounter(TIM3);  

        

          

    // dis_595(encoder_num,encoder_num);  

          

  }  

}


Keywords:STM32 Reference address:Setting up the STM32's own hardware encoder interface

Previous article:STM32 orthogonal signal processing of code disk
Next article:Encoder configuration of stm32

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

Configuration process of stm32 serial port
1 Configure the IO ports used by the serial port: ①Rx floating input ②Tx multiplexed push-pull output Note: The specific configuration is as follows //GPIOA.9/10 is used for serial communication   //Set GPIOA.9 (USART.Tx) to multiplexed push-pull output   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;  GPIO_InitStructure.GPI
[Microcontroller]
STM32 study notes: infrared remote control
1. Brief Introduction Infrared remote control is a wireless, non-contact control technology with strong anti-interference ability, reliable information transmission, low power consumption and low cost. The encoding methods for infrared remote control that are currently widely used are: NEC protocol for PWM (pulse wi
[Microcontroller]
STM32 study notes: infrared remote control
STM32 drives ST7920's 12864 LCD (serial mode)
/********************************************************************************************** * File name: 12864.c * Copyright: * Module name: Serial working mode driver program of 12864 LCD driven by st7920 * cpu: stm32f103rct6 Main frequency: 72M * Author: * Creation date: 2009-10-15 * Function summary: *---------
[Microcontroller]
STM32 learning EXTI
EXTI has two functions: one is to generate an interrupt, the purpose is to pass the input signal to NVIC, further run the interrupt service function, realize the function, at the software level; the other is to generate an event, the purpose is to generate a pulse signal for other peripherals to use, this is the circu
[Microcontroller]
Analysis of TCP/IP protocol stack code ARP (STM32 platform)
1. Introduction to ARP Address Resolution Protocol ARP provides dynamic mapping between IP addresses and corresponding hardware addresses. A translation is required from the logical Internet address to the corresponding physical hardware address. This is the function of ARP. The function of ARP is to provide dynamic m
[Microcontroller]
Analysis of TCP/IP protocol stack code ARP (STM32 platform)
STM32 learning: keil measurement function running time
Set the operating frequency during Trace (that is, the current system clock, such as stm32f103, f107 is 72MHz, f407 is 168M, and f429 is 180M): 2. Enter the debugging state, set a breakpoint before a statement and execute to this point, then execute to another statement. The following Sec is the execution time
[Microcontroller]
STM32--Read and write internal Flash
Flash module organization The memory is organized into program memory blocks, data EEPROM blocks, and information blocks. The program memory block is divided into 4 KB sectors, each sector is further divided into 16 pages of 256 bytes each.   Program memory page erase This operation is used to erase a page (64 words)
[Microcontroller]
STM32--Read and write internal Flash
(VIII) STM32 CAN module experiment
bxCAN is the abbreviation of Basic Extended CAN, which supports CAN protocol 2.0A and 2.0B. Its design goal is to efficiently process a large number of received messages with minimal CPU load. It also supports priority requirements for message sending (priority characteristics can be configured by software).      
[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号