(9) Lanqiao Cup Embedded-Timer (input capture mode)

(1) Personal habits

A separate Capture.c and Capture.h will be created and placed under the HARAWARE folder

(2) Preparation

  • 1. The configuration of the input capture mode is similar to the output comparison mode (the function is similar, but the configuration parameters are different)
  • 2. Because there is no oscilloscope, in order to observe the effect, choose to measure the time the button is pressed (the hardware connection is as follows)
    Insert picture description hereInsert picture description here
  • 3. We choose to configure the K1 button, and its corresponding PA0 is the same as TIM2_CH1 (we configure them in this experiment)

(3) Write void Capture_init(void) initial function

Insert picture description here

(4) Write void TIM2_IRQHandler(void) interrupt service function

Insert picture description here

(5) Write void DealWith_0(void) processing function

Insert picture description here

(6) Problem

Question one:
According to its hardware connection, it is high when it is not pressed, and it is low when it is pressed. So we set the falling edge trigger for the first time (this conforms to the logic of our program)
Insert picture description here
Question two:
1. Because the timer is enabled, the timer is always counting, and the update interrupt is always triggered.
2. If this flag bit is not used, the overflow variable will always increase by
3, so when the rising edge is set to trigger the interrupt (that is, we When the button is pressed), the flag bit is 1 (only then can the overflow count be counted)

Question three:
When we press the button, start counting, change the falling edge trigger to the rising edge trigger, mark position 1, enter the next state (released state)
When we release the button, save the count value, change the rising edge trigger to Falling edge trigger, flag bit value 2, enter the next state (calculation state)

Question four:
1. The timer clock is divided into: 1MHz=1us
2. Hexadecimal to decimal: 0xFFFF=65535
3. Dividing by 1000 is to change the unit us into ms

Time ms = ((Saved counter value) + (Overflow number ∗ 65535)) / 1000 Time ms = ((Saved counter value) + (Overflow number *65535))/1000 When inter- m S=( ( Paul save the counter number unit value )+( Overflow the number65535))/1000

Guess you like

Origin blog.csdn.net/m0_46278925/article/details/113621113