About the application configuration of stm32 timer

Publisher:勾剑寒Latest update time:2015-09-21 Source: eefocusKeywords:stm32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
The timer is a powerful tool of stm32. It can be said that there is no project that does not use the timer, so only by mastering and understanding the use of timers can we make good products.
This chapter talks about the use of stm32 timer TIM_GetCounter (TIMx). This has been used in previous projects to obtain the duration of high and low levels, and has been used in ultrasonic ranging and infrared remote control decoding. Except for 1 and 8, the other timers of stm32 are general timers. How to use this function to capture the duration of the level? First, let's look at the initialization content, taking TIM4 as an example
void Timer4_CFG()
{

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;


RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOE | RCC_APB2Periph_AFIO , ENABLE);
 
GPIO_InitStructure.GPIO_Pin = IR_LED_PIN;
GPIO_InitStructure.GPIO_Speed ​​= GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(IR_LED_PORT, &GPIO_InitStructure);


TIM_TimeBaseStructure.TIM_Prescaler = 3599; //TIM_CKD_DIV1
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //
TIM_TimeBaseStructure.TIM_Period = 200; // ARR
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DI V1;
//TIM_TimeBaseStructure.TIM_RepetitionCounter = 4;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);

//TIM_PrescalerConfig(TIM4,3599,TIM_PSCReloadMode_Immediate);//72MHz/(3599+1)=20 000 HZ 50uS
TIM_ARRPreloadConf ig(TIM4,DISABLE);

TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE);
TIM_ITConfig(TIM4,TIM_IT_Trigger,ENABLE);
TIM_Cmd(TIM4, DISABLE);
     

}
APB1 is a 36M clock, which generates a 20KHZ 50us period waveform after frequency division. TIM->CNT is a counter. There is a certain rule when calculating the value of TIM_TimeBaseStructure.TIM_Period here. That is, you need to know the maximum high and low levels you collected. For example, the longest level in the waveform I collected is 4ms, and the base timer is 50us. Then I have to calculate 80 to reach 4ms, so the value of TIM_TimeBaseStructure.TIM_Period must be greater than 80, otherwise it will go wrong. Here I want to remind everyone that when the value of TIM-CNT reaches the value of TIM_TimeBaseStructure.TIM_Period, it will be cleared and counted again as shown in the figure below. This is the register configuration method for obtaining high and low levels using the TIMX-CNT counter of the timer. Of course, cnt=TIM_GetCounter(TIMx); cnt must be multiplied by 50us to get the real time. If you don't use a counter, and want to use a global variable in the interrupt to represent the number of interrupts to calculate the duration of high and low levels, please note that this method may be inaccurate due to the large number of program functions. You can try it when there are fewer functions. Then the configuration of the timer must be changed again. The benchmark is still 50us. TIM_TimeBaseStructure.TIM_Prescaler =3599; //TIM_CKD_DIV1 At this time, the maximum value cannot be considered, but a minimum common divisor should be considered. For example, how many high and low levels do I want to collect? There are 8ms, 4ms, 1ms, and 500us. Then the value of TIM_TimeBaseStructure.TIM_Period has requirements. As long as it is a multiple of 50us and is less than 500us or its divisor after multiplying with 50us, it will be fine. For example, TIM_TimeBaseStructure.TIM_Prescaler = 3599; // TIM_TimeBaseStructure.TIM_Period=1; // These two sentences mean that an interrupt occurs every 50us.
WENTIJIEJU.png








Keywords:stm32 Reference address:About the application configuration of stm32 timer

Previous article:How to write data to the flash of stm32 and use it as eerom
Next article:stm32 timer 2 outputs 10KHZ waveform

Recommended ReadingLatest update time:2024-11-16 13:55

stm32 startup mode + pull-up (pull-down) resistor two-in-one
I have been working on a stm32 project recently and have been struggling with its startup method.  I saw a good article and reposted it. The format of the original article is not very good, and there are many strange things in the content (especially the second article, I just think the original article is original, b
[Microcontroller]
STM32 learning: timer software timing
#include "led.h" #include "delay.h" #include "key.h" #include "sys.h" #include "lcd.h" #include "usart.h" #include "timer.h" #include "beep.h"        extern u32  sec;  int  main(void)  {          delay_init(); //delay function initialization           NVIC_Configuration(); //Set NVIC interrupt group 2: 2-bit preempt
[Microcontroller]
STM32 debugging-serial port printing function and usage
1. In the usart.h file, add the following code: #if 1 #pragma import(__use_no_semihosting)              //Support functions required by the standard library                  struct __FILE  {  int handle;  };   FILE __stdout;        // define _sys_exit() to avoid using semihost mode     _sys_exit(int x)  {  x = x;  } 
[Microcontroller]
One of the STM32/STM8L/STM8S series, running water lamp
1. STM32F103 lights up the LED 1. Circuit diagram: 2. Code: //FUNCTION: LED initialization //PARA: None //RETURN: None void LED_INIT(void)  {     GPIO_InitTypeDef GPIO_InitStructure;       RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); //Enable AFIO and GPIOB ports     GPIO_PinRemapCon
[Microcontroller]
One of the STM32/STM8L/STM8S series, running water lamp
STM32 memory allocation details
1. KEIL compiled data code RO-data RW-data ZI-data Flash actually stores data 2. Memory Segments bss segment, data segment, text segment, heap and stack. 2.1, bss segment The bss segment usually refers to a memory area used to store uninitialized global variables in a program; BSS is the abbreviation of Bloc
[Microcontroller]
STM32 memory allocation details
Transceiver chip selection problem for low-speed CAN communication based on STM32
The CAN transceiver chip used in the first version of the test circuit of this circuit system is TJA1050 produced by Philips. The circuit refers to the circuit of the Battleship STM32 development board, as shown in the figure below. There is no problem with the circuit test, but the performance cannot meet the eng
[Microcontroller]
Transceiver chip selection problem for low-speed CAN communication based on STM32
Common digital tube chip TM1637 driver based on STM32
I won’t go into details about the IIC protocol here; Use analog IIC port definition B14 B15 #define AnalonSDA_Pin GPIO_PIN_12 #define AnalonSDA_GPIO_Port GPIOB #define AnalonSCL_Pin GPIO_PIN_13 #define AnalonSCL_GPIO_Port GPIOB IO configured as output   /*Configure GPIO pin Output Level */   HAL_GPIO_WritePin(
[Microcontroller]
STM32 AD is also very large without signal input
Reason: VREF+ is not connected to the network in the schematic diagram, resulting in the absence of this network on the PCB. Therefore, the data of the external channel is very large when no signal is added, and the number is not necessarily the same each time the power is turned on.
[Microcontroller]
STM32 AD is also very large without signal input
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号