Experiment three timer experiment

1. Purpose of the experiment

  1. Master the STM32 interrupt programming process.
  2. Familiar with the basic use of STM32 firmware library.
  3. Master the STM32 timer design process.
  4. Familiar with the STM32 timer interrupt design process.

​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

Hardware: one PC

       STM32 development board set

Software: MDK V5 set

       

3. Experimental principle

1. STM32 external interrupt :

   STM32 has 20 external interrupt sources, as follows:

16 GPIO input interrupts ;

PVD output;

RTC alarm event;

USB wakeup event;

Ethernet wake-up event (applicable to interconnected products only).

   Although the GPIO input interrupt has 16 input channels, it only occupies 7 interrupt vectors. EXTI0~EXTI4 each occupy one interrupt vector, EXTI5~9 share one, and EXTI10~15 share one. So when programming, EXTI5~9 will share an interrupt function, and EXTI10~15 will share an interrupt function.

The mapping diagram of these 16 external interrupts and GPIO is as follows.

As can be seen from the figure above, the 16 interrupt input pins can be redefined by the user. Of course, there are restrictions, for example: EXTI0 channel can only choose one of the 7 pins PA0, PB0, PC0, PD0, PE0, PF0, PG0; and EXTI1 can only choose one of the 7 pins named Px1 (x can be one of A~G); and so on, EXTI2 can only choose Px2 pin;  …. The specific selection of the pin can be set by the corresponding bit of the register AFIO_EXTICRx (x can be 1~4).

2. Related internal registers

 

The above figure is a block diagram of the external interrupt/event controller. The block diagram involves 6 registers, namely:

Falling edge trigger selection register (EXTI_FTSR);

Rising edge trigger selection register (EXTI_RTSR);

Software interrupt event register (EXTI_SWIER);

pending register (EXTI_PR);

Interrupt mask register (EXTI_IMR);

Event Mask Register (EXTI_EMR).

3. NVIC---Nested Vectored Interrupt Controller

STM32 has only 68 interrupt channels at most, so all the above registers are not used. The priority setting of each interrupt is done by writing these register groups. The STM32 interrupt priority register only uses the upper 4 bits of the 8-bit width, and the meaning of these 4 bits is determined by the settings of the 3 bits [10:8] of the register AIRCR.

 

4. STM32 external interrupt process

 

1 ) Initialize the IO port as an input.

2 ) Turn on the multiplexing clock of the IO port, and set the mapping relationship between the IO port and the interrupt line.

3 ) Initialize online interrupts, set trigger conditions, etc.

4 ) Configure the interrupt grouping ( NVIC ) and enable the interrupt.

5 ) Write the interrupt service function.

5.1. Overview of STM32 general-purpose timers:

   The general-purpose timer of STM32 is a 16-bit autoload counter driven by a programmable prescaler.

   STM32 has 4 general-purpose timers (TIM2, TIM3, TIM4, TIM5), which are suitable for many occasions. In addition to basic timing, it is mainly used to measure the frequency, pulse width and output PWM pulse of the input pulse. It also has Encoder interface. Each timer is completely independent and does not share any resources with each other. They can operate synchronously together.

5.2. Time base unit:

   This part is similar to the timer of ordinary single-chip microcomputer, and consists of 3 small units, namely:

counter(CNT);

Prescaler (PSC)

Autoload Register (ARR).

The relationship between the three is as follows:

 

 

The counter can count up, count down or bidirectionally count up and down, and the counter clock is provided by the frequency division output of the prescaler.

The prescaler is based on a 16-bit counter controlled by a 16-bit register (TIMx_PSC). It can divide the clock frequency of the counter by any value between 1 and 65536 (the frequency division value is the value of the TIMx_PSC register + 1 ).

The formula for calculating the working frequency of the timer is CK_CNT=timer clock/(TIMx_PSC +1)

From this we can get that one clock cycle of the STM32 microcontroller is: T=1/ CK_CNT

For example, the clock of the ordinary

TIMx_ARR = 10 000

Because 72 000 000 / 7200 = 10KHz      // 7199=7200-1

Clock cycle T=1/10KHz=100us

100us × 10 000 = 1S

Conclusion: The frequency division ratio is 7199 and the value of the timer counter is 10 000

5.3. Working mode:

    There are 3 working modes of the counter function of the general-purpose timer:

count up mode;

count down mode;

Center alignment mode (count up/down).

    In count-up mode, the counter counts from 0 to the autoload value (the content of the TIMx_ARR counter), then restarts from 0 and generates a counter overflow event.

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

In center-aligned mode, the counter counts from 0 to the autoload value (TIMx_ARR register)−1, generates a counter overflow event, then counts down to 1 and generates a counter underflow event; then restarts counting from 0. The center alignment mode can be divided into 3 different modes, which are selected by the CMS[1:0] setting of the control register 1 (TIMx_CR1).

5.4. Clock selection:

   Before the time base unit, the clock source of the timer needs to be selected to provide the CK_PSC signal. As can be seen from the block diagram of the general-purpose timer, there are several options for the clock of each general-purpose timer:

Internal Clock (CK_INT)

External clock mode 1: external input pin (TIx)

External clock mode 2: External trigger input (ETR)

Internal trigger input (ITRx): Use one timer as the prescaler of another timer, for example, one timer Timer1 can be configured as the prescaler of another timer Timer2.

   For details on how to choose, please refer to the description of the clock selection and slave mode control register (TIMx_SMCR) in Chapter 14 of the "STM32 Data Manual".

   Using the internal clock (CN_INT) as the clock input of the general-purpose timer is the simplest application of the general-purpose timer and involves the least register operations.

5.5. The following routines are used to illustrate the timer interrupt steps:

   Use the timer to generate an interrupt, and then flip the voltage signal on LED1 in the interrupt service function to indicate the generation of the timer interrupt. Take the general timer TIM3 as an example to illustrate what steps to go through,

1) TIM3 clock enable. 

   Set the clock of TIM3 through the first bit of APB1ENR, because the frequency division of APB1 is set to 2 in the Stm32_Clock_Init function, the clock of TIM3 is twice the clock of APB1, which is equal to the system clock. 

2) Set the values ​​of TIM3_ARR and TIM3_PSC. 

   Through these two registers, the value of automatic reloading and frequency division coefficient are set. These two parameters plus the clock frequency determine the overflow time of the timer. 

3) Set TIM3_DIER to allow update interruption. 

   To use the update interrupt of TIM3, set the UIE bit of DIER and enable the trigger interrupt. 

4) Allow TIM3 to work. 

   After configuring the timer, you need to enable the timer. Set by the CEN bit of TIM3_CR1. 

5) TIM3 interrupt group setting. 

   After the timer is configured, because an interrupt is to be generated, the NVIC-related registers must be set to enable the TIM3 interrupt. 

6) Write the interrupt service function. 

    Use this function to handle related interrupts generated by timers. After the interrupt is generated, the value of the status register is used to judge what type of interrupt is generated this time. Then perform related operations, you can use the update (overflow) interrupt, so the lowest bit in the status register SR. After processing the interrupt, write 0 to the lowest bit of TIM3_SR to clear the interrupt flag. 

Through the above steps, use the update interrupt of the general-purpose timer to control the flipping of the external LED

4. Experimental content

 

 Circuit schematic

  1. Combining key interrupt and timer programming to achieve the following: ( the timer can use multiple )

 The state of the main program is as follows: Realize the function of LED1, LED2, and LED3 forward flowing lights (that is, light up sequentially, from top to bottom), and the LED interval time is 0.2S. (Must be done with timer interrupt)

‚ If the keyboard K2 is pressed, the control LED1 will flash 3 times at intervals of 1 second and then exit the interrupt. (Combined with external interrupt + timer interrupt to complete)

ƒ If the keyboard K3 is pressed, the control LED2 will flash 3 times at intervals of 2 seconds and then exit the interrupt. (Combined with external interrupt + timer interrupt to complete)

„  If there are two buttons pressed at the same time ,  it will be executed according to the priority level of K3 and K2 from high to low .

  1. Programming uses PWM to realize the LED1 breathing light function: LED1 gradually brightens, then gradually dims, and so on.

Submit the content requirements of the experiment report:

  1. Purpose.

(1) Master the STM32 timer design process.

(2) Familiar with the STM32 timer interrupt design process.

2 . experiment content . Question 1-2 Programming .

(1) Button and LED initialization

 

 (2) Initialization of the basic timer 6

 (3) Interrupt function of basic timer 6 (timing 1S)

 (4) Initialization of the basic timer 7

 (5) Interrupt function of basic timer 7 (set 2S)

 (6) External interrupt initialization

 (7) External interrupt function

 The main function, which realizes the function of LED1, LED2, and LED3 forward water lights (that is, sequentially light up, from top to bottom), and the LED interval time is 0.2S.

 

3 . Experiment summary.

In this experiment, I learned about the design process of the STM32 interrupt program, which made me familiar with the specific application of external interrupts, realized the application of timers, and can flexibly use the programming skill of status code. Let the knowledge I have learned be practiced and improve my hands-on ability. It also made me understand my own shortcomings, and I will continue to strengthen my own learning in the future.

Guess you like

Origin blog.csdn.net/weixin_45784275/article/details/125425431