16. Timer interrupt experiment

Publisher:jingyunLatest update time:2017-11-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Classification of STM32 timers:

1. Advanced timer TIME1,TIME8

2. General timer TIME2~TIME5

3. Basic timer TIME6,TIME7

16. Timer interrupt experiment

This chapter explains the general timer, refer to Chapters 13, 14, and 15 of the Development Guide and Chapter 14 of the Chinese Reference Manual.

16. Timer interrupt experiment
16. Timer interrupt experiment


1. Counter Counting Mode

1. Up counting mode

2. Down counting mode

In down mode, the counter starts from the automatically loaded value (the value of the TIMx_ARR counter) and counts down to 0, then restarts from the automatically loaded value and generates a counter underflow event.

16. Timer interrupt experiment

16. Timer interrupt experiment

3. Center alignment mode (up/down)

In this experiment, we use the down counting mode

2. STM32 general purpose timer

The STM32's general purpose TIMx (TIM2, TIM3, TIM4 and TIM5) timer functions include: 

1) 16-bit up, down, up/down auto-load counter (TIMx_CNT).

2) 16-bit programmable (can be modified in real time) prescaler (TIMx_PSC), the division coefficient of the counter clock frequency is any value between 1 and 65535.

3) 4 independent channels (TIMx_CH1~4), which can be used as: 

A. Input Capture 

B. Output Comparison 

C. PWM Generation (Edge or Center Aligned Mode) 

D. Single pulse mode output 

4) External signals (TIMx_ETR) can be used to control the synchronization circuit of the timer and the timer interconnection (one timer can be used to control another timer).

5) Interrupt/DMA is generated when the following events occur: 

A. Update: counter overflow/underflow, counter initialization (by software or internal/external trigger) 

B. Trigger event (counter starts, stops, initializes, or counts by internal/external trigger) 

C. Input Capture 

D. Output Comparison 

E. Support incremental (quadrature) encoder and Hall sensor circuits for positioning 

F. Trigger input as external clock or cycle-by-cycle current management

3. Use a timer to perform a timed step of a settable length

16. Timer interrupt experiment
The overflow time of the counter is (the initial value of the counter + 1) * the clock of the counter

How to set the counting frequency of the counter:

1. Timer clock source

1) Internal clock (CK_INT)

2) External clock mode 1: external input pin (TIx)

3) External clock mode 2: external trigger input (ETR)

4) Internal trigger input (ITRx): Use timer A as the prescaler for timer B (A provides the clock for B).

This chapter explains the internal clock (CK_INT)

16. Timer interrupt experiment
16. Timer interrupt experiment

The clock comes from the APB1 clock and then passes through a multiplier. The multiplication factor can be 1 or 2, which is determined by the APB1 predivider.

If its prescaler factor is 1, the multiplier is 2.

If its pre-scaling factor is not 1, the multiplication factor is 1.

Up counting mode:

16. Timer interrupt experiment

Example:

After initializing the system with the SystemInit() function, set the system clock to 72MHz

16. Timer interrupt experiment

The overflow time calculation formula is as follows:

Tout= ((arr+1)*(psc+1))/Tclk

in:

psc: prescaler value

arr: Automatic reload value.

Tclk: The input clock frequency of TIM3 (in Mhz).

Tout: TIM3 overflow time (in us).

The value of (psc+1)/Tclk is the time of one cycle of the timer clock.

4. Some related registers

1. DMA/Interrupt Enable Register (TIMx_DIER)

16. Timer interrupt experiment

The 0th bit is mainly used, which is the update interrupt enable bit. This chapter uses the timer update interrupt, so this bit should be set to 1 to allow interrupts caused by update events.

2. Prescaler register (TIMx_PSC)

This register is used to divide the clock frequency and then provide it to the counter as the counter clock.

16. Timer interrupt experiment
3. Auto-reload register (TIMx_ARR)

16. Timer interrupt experiment
4. Status Register (TIMx_SR)

This register is used to mark whether various events/interrupts related to the timer have occurred.

16. Timer interrupt experiment

Getting and clearing the status flag:

FlagStatus TIM_GetFlagStatus(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);

void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG);

ITStatus TIM_GetITStatus(TIM_TypeDef* TIMx, uint16_t TIM_IT);

void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT);

5. Control register 1 (TIMx_CR1)

Used to enable the timer, mainly used for bit 4 and bit 0.

16. Timer interrupt experiment
16. Timer interrupt experiment
6. Counter current value register CNT

16. Timer interrupt experiment

5. Steps to use the timer

1) TIM3 clock enable.

TIM3 is mounted under APB1, so we enable TIM3 through the enable function under APB1 bus. The called function is:

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //Clock enable

2) Initialize timer parameters, set auto-reload value, division factor, counting method, etc.

In the library function, the timer initialization parameters are implemented through the initialization function TIM_TimeBaseInit:

voidTIM_TimeBaseInit(TIM_TypeDef*TIMx,

TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct);

Definition of the structure:

typedef struct

{

uint16_t TIM_Prescaler;   

uint16_t TIM_CounterMode;    

uint16_t TIM_Period;     

uint16_t TIM_ClockDivision;  

uint8_t TIM_RepetitionCounter;  

} TIM_TimeBaseInitTypeDef;

The first parameter TIM_Prescaler is used to set the frequency division coefficient

The second parameter TIM_CounterMode is used to set the counting mode

The third parameter TIM_Period is to set the automatic reload count period value

The fourth parameter TIM_ClockDivision is used to set the clock division factor       

For general timers, only the first four parameters are useful, and the last parameter TIM_RepetitionCounter is useful only for advanced timers.

example:

TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;

TIM_TimeBaseStructure.TIM_Period = 4999;

TIM_TimeBaseStructure.TIM_Prescaler =7199; 

TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; 

TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);

3) Set TIM3_DIER to enable update interrupt.

void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState);

4) TIM3 interrupt priority setting.

5) Allow TIM3 to work, that is, enable TIM3.

To start the timer after configuration, set the CEN bit in TIM3_CR1.

void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState)

6) Write the interrupt service function.

6. Update: counter overflow/underflow, counter initialization (by software or internal/external trigger)

16. Timer interrupt experiment


Reference address:16. Timer interrupt experiment

Previous article:15. Window watchdog experiment
Next article:17. Definition of all interrupt functions

Recommended ReadingLatest update time:2024-11-16 12:44

STM32 uses DMA plus serial port idle interrupt to receive data
 In STM32, you need to use the serial port to receive data, and you use the serial port interrupt to receive data. However, if you use this method, you have to frequently enter the serial port interrupt and then process it, which is inefficient. So I thought of using DMA to receive serial port data, which STM32 also su
[Microcontroller]
STM32 uses DMA plus serial port idle interrupt to receive data
Design of STM32 laser power supply control system
  The control system design of the laser power supply is realized by using STM32. Aiming at the practical application of laser welding, this paper has made better expansion and improvement to the functions of the laser power supply. The human-machine interface (HMI) display is used to control the laser power supply. T
[Power Management]
Design of STM32 laser power supply control system
stm32 advanced timer relearning
I recently used timers in a project, so I decided to relearn them. I used them only for simple pwm generation and interrupt processing before, and never studied them in depth. Today, I took this opportunity to relearn advanced timers. Once you learn advanced timers, you will have no problem with basic timers. In genera
[Microcontroller]
stm32 advanced timer relearning
STM32 Series No. 27 - Infrared Remote Control
Infrared coding classification: The infrared remote controller uses a single bus communication method. Commonly used infrared codes are: NEC Protocol PWM (Pulse Width Modulation) PPM (Pulse Position Modulation) of Philips RC-5 Protocol. NEC protocol features: 8-bit address and 8-bit instruction length Address an
[Microcontroller]
The influence of signal source characteristics on conversion results in STM32 ADC applications
All chips in the STM32 family have built-in successive approximation register ADC modules. The internal framework is as follows:   Each ADC conversion is first sampled and held, and then compared and output in multiple steps. The number of steps is equal to the number of ADC bits, and each ADC clock generates one d
[Microcontroller]
The influence of signal source characteristics on conversion results in STM32 ADC applications
Some issues about STM32 GPIO pins
/*  Name: Some issues about STM32 GPIO pins  Description: When I was writing a keyboard scanning program today, I encountered some problems.  Some pins can read the level status, but some pins can not read the status. After an afternoon of modification and a long search, I finally got some clues. Now I will briefly de
[Microcontroller]
stm32 FSMC-TFTLCD display
TFTLCD  The commonly used communication modes for TFT LCD screens are 6800 mode and 8080 mode. For TFT color screens, 8080 parallel port (abbreviated as 80 parallel port) mode is usually used. The read and write timing of 8080 mode is actually similar to that of LCD1602 or LCD12864. The 8080 interface has 5 basic c
[Microcontroller]
stm32 FSMC-TFTLCD display
STM32 learning notes: using library functions to drive LED lights
1. Familiar with the GPIO structure The following structure is what I got from the official manual: view plain copy print? typedef struct { u16 GPIO_Pin; GPIOSpeed_TypeDef GPIO_Speed; GPIOMode_TypeDef GPIO_Mode; } GPIO_InitTypeDef; 2. Programming steps 1. First, define a GPIO_InitTypeDef structure and name the
[Microcontroller]
STM32 learning notes: using library functions to drive LED lights
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号