STM32 general timer input capture mode

Publisher:温文儒雅Latest update time:2019-01-04 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

#include "stm32f10x.h"


/*RCC clock configuration*/

void RCC_config()

 ErrorStatus HSEStartUpStatus;


 /* RCC registers are set to default configuration */

 RCC_DeInit();

 /* Turn on external high speed clock */

 RCC_HSEConfig(RCC_HSE_ON);

 /* Wait for the external high-speed clock to stabilize*/

 HSEStartUpStatus = RCC_WaitForHSEStartUp();

 if(HSEStartUpStatus == SUCCESS) 

 { 

  /* Set HCLK = SYSCLK */

  RCC_HCLKConfig(RCC_SYSCLK_Div1);

  /* Set PCLK2 = HCLK */

  RCC_PCLK2Config(RCC_HCLK_Div1);

  /* Set PCLK1 = HCLK / 2 */

  RCC_PCLK1Config(RCC_HCLK_Div2);

// /* Set FLASH code delay */

//  FLASH_SetLatency(FLASH_Latency_2);

// /* Enable prefetch cache */

//  FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  /* Set the PLL clock source to HSE multiplier 9 72MHz */

  RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  /* Enable PLL */

  RCC_PLLCmd(ENABLE);

  /* Wait for PLL to stabilize */

  while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);

  /* Set PLL as system clock source */

  RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  /* Wait for the system clock source to switch to PLL */

  while(RCC_GetSYSCLKSource() != 0x08);

 }

}


/* GPIO configuration */

void GPIO_config()

{

 GPIO_InitTypeDef GPIO_InitStructure;

 

 /* Enable GPIOA clock */

 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);

 

 /* Set the GPIOA clock to default parameters */

 GPIO_DeInit(GPIOA); 

 /* Pull-down output */

 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;

 /* Pin 13 */

 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;

 /* Output frequency 50MHz */

 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

 /* Initialize GPIOA_0 */

 GPIO_Init(GPIOA, &GPIO_InitStructure);

}


/* Timer configuration */

void TIMER_config()

{

 TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

 TIM_ICInitTypeDef TIM_ICInitStructure;

 

 /* Enable TIM2 clock */

 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);

 

 /* Set the Timer 2 register to initial value */

 TIM_DeInit(TIM2);

 

 /* Set timer 2 to use internal clock */

 TIM_InternalClockConfig(TIM2);

 

 /* Prescaler value*/

 TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1;

 /* Clock division */

 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;

 /* count up */

 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

 /* Automatic reload value*/

 TIM_TimeBaseStructure.TIM_Period = 1000 - 1;

 /* Initialize timer 2 */

 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);

 

 /* Channel 1 */

 TIM_ICInitStructure.TIM_Channel = TIM_Channel_1;

 /* rising edge trigger */

 TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising;

 /* Map to TI1 */

 TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI;

 /* Frequency division*/

 TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1;

 /* Filter */

 TIM_ICInitStructure.TIM_ICFilter = 0x00;

 /* Initialize input capture */

 TIM_ICInit(TIM2, &TIM_ICInitStructure);

 

 /* Clear interrupt flag */

 TIM_ClearFlag(TIM2, TIM_IT_Update | TIM_IT_CC1);

 

 /* Enable TIM2 interrupt */

 TIM_ITConfig(TIM2, TIM_IT_Update | TIM_IT_CC1, ENABLE);

 

 /* Start timer 2 */

 TIM_Cmd(TIM2, ENABLE);

}


/* Abort placement */

void NVIC_config()

{

 NVIC_InitTypeDef NVIC_InitStructure;

 

 /* Select interrupt group 1 */

 NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

 

 /* Select the interrupt channel of TIM2 */

 NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;

 /* Set the preemptive interrupt priority to 0 */

 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

 /* Set the responsive interrupt priority to 0 */

 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

 /* Enable interrupt */

 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

 /* Interrupt initialization */

 NVIC_Init(&NVIC_InitStructure);

}


/* Delay in milliseconds */

void delay_ms(uint16_t time)

{    

 uint16_t i = 0;


 while(time--)

 {

  i = 12000;

  while(i--);

 }

}


/* Timer 2 interrupt vector */

void TIM2_IRQHandler(void)

{

 static uint32_t capture = 0;

 

 /* Input capture interrupt */

 if(TIM_GetITStatus(TIM2, TIM_IT_CC1) != RESET)    

 {

  /* Clear interrupt flag */

  capture += TIM_GetCapture1(TIM2);

  

  /* Clear the counter value */

  TIM_SetCounter(TIM2, 0);

  capture = 0;

 }

 

 /* Update interrupt */

 if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)

 {

  capture += 1000;

 }  

 

 TIM_ClearFlag(TIM2, TIM_IT_CC1 | TIM_IT_Update);

 TIM_ClearITPendingBit(TIM2, TIM_IT_CC1 | TIM_IT_Update);

}


int main()

{

 /* Clock configuration */

 RCC_config();


 /* GPIO configuration */

 GPIO_config();

 

 /* Interrupt placement*/

 NVIC_config();


 /* Timer 2 configuration */

 TIMER_config();


 while(1)

 {

  delay_ms(1000);

 }

}


Keywords:STM32 Reference address:STM32 general timer input capture mode

Previous article:[STM32] Input capture of general timer (Example: Input capture)
Next article:stm32-Serial port uses IDLE interrupt to accept variable length data method

Recommended ReadingLatest update time:2024-11-16 11:39

Introduction to STM32 input capture
     Input capture mode can be used to measure pulse width or frequency. Except for TIM6 and TIM7, all other STM32 timers have input capture function. Simply put, STM32 input capture detects the edge signal on TIMx_CHx. When the edge signal changes (such as rising edge/falling edge), the current timer value (TIMx_CNT)
[Microcontroller]
STM32 network communication DM9000A circuit design
1 General description: The DM9000A PHY can communicate with the 10BASE-T standard on UTP3\4\5 or the 100BASE-T standard on UTP5. Its auto-negotiation function is able to automatically configure the DM9000A to maximize its performance. It also supports IEEE 802.3X full-duplex data stream communication.   2. Structure d
[Microcontroller]
STM32 drives LCD12864
#include "led.h" #undef APP_DE #include "hawk.h" LCD uses JLX19264G-260 from Jinglianxun Electronics #define FEEDDOG() GPIOC- ODR^= GPIO_Pin_0 //LCD IO initialization void LCD_Init(void) {    GPIO_InitTypeDef GPIO_InitStructure;    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIO
[Microcontroller]
STM32 SysTick Application Notes
In the library provided by STM32, different versions of the library have some differences in code. Therefore, when using the corresponding code library, you must read the corresponding documentation, otherwise it will cause a lot of trouble. This also brings a lot of problems to the upgrade of the code. The following i
[Microcontroller]
STM32 SysTick Application Notes
STM32 103 internal timer is inaccurate
system_stm32f10x.c     /*  PLL configuration: PLLCLK = HSE * 9 = 72 MHz HSE=12M*/     RCC- CFGR &= (uint32_t)((uint32_t)~(RCC_CFGR_PLLSRC | RCC_CFGR_PLLXTPRE |                                         RCC_CFGR_PLLMULL));     RCC- CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6);     /*  PLL configuration
[Microcontroller]
stm32 boot experience
The storage media corresponding to the three startup modes of STM32 are all built-in to the chip, they are:   1) User Flash = Flash built into the chip.  2) SRAM = the RAM area built into the chip, which is the memory. 3) System memory = a specific area inside the chip. When the chip leaves the factory, a Bootloader i
[Microcontroller]
stm32 boot experience
Significance of assert_param function
In the STM32 firmware library and the provided routines, assert_param() can be seen everywhere. If you open the stm32f10x_conf.h file in any routine, you can see that assert_param is actually a macro definition; in the firmware library, its function is to detect whether the parameters passed to the function are valid
[Microcontroller]
STM32 Development Board - I2C--24Cxx
The 24Cxx series we use for demonstration is the most commonly used EEPROM chip. As mentioned above, the address code of 24Cxx is fixed, and the 8 bits are as follows: 1   0   1   0   A2 A1 A0   0 A2 A1 A0 are the levels of its three pins respectively. There is something special about 24Cxx. 24Cxx includes four
[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号