Input capture of STM32

Publisher:平安心境Latest update time:2018-04-15 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

Purpose:

Print the time the key is pressed on the serial debugging assistant


Experimental steps:





Experimental Procedure:


  1. /*******************************timer.c********************************/  

  2. #include "sys.h"  

  3. #include "stm32f4xx.h"  

  4.   

  5.   

  6. extern u8 TIM5CHA1_CAPTURE_STA;  

  7. extern u16  TIM5CHA1_CAPTURE_VAL;  

  8.   

  9.   

  10. /* 

  11. The purpose of this example is to 

  12. When a key is pressed, the time difference between each input capture, 

  13. Then print out the time difference from the serial port debugging assistant; 

  14. */  

  15.   

  16.   

  17. /* 

  18. For input capture operations, please refer to the steps in the register version. 

  19. */  

  20.   

  21.   

  22. /* 

  23. Initialization function of timer input capture: 

  24. Mainly about the configuration of registers 

  25. */  

  26.   

  27.   

  28. /*Initialize timer 5 as input capture*/  

  29. void TIM5_Init(void){  

  30.       

  31.     /************************* 

  32.     Timer input capture settings:  

  33.     *************************/  

  34.       

  35.     /*Reuse the KEY_UP button*/  

  36.     /*1. Enable GPIO port clock*/  

  37.     RCC->AHB1ENR |= 1;  

  38.     /*Here you also need to configure it to pull down, 

  39.     Because in input capture, it will be triggered by the rising edge; 

  40.     */  

  41.     GPIOA->PUPDR |= 0X2;  

  42.       

  43.     /*2. Enable multiplexing peripheral clock*/  

  44.     RCC->APB1ENR |= 1<<3;  

  45.       

  46.     /*3.Port mode is configured as multiplexing function*/  

  47.     GPIOA->MODER |= 0X2;  

  48.       

  49.     /*4. Configure GPIOx_AFRL or GPIOx_AFRH register, 

  50.     Connect the IO to the desired multiplexed peripheral */  

  51.     GPIOA->AFR[0] |= 0X2;  

  52.       

  53.       

  54.       

  55.     /*Set up input capture of timer 5*/  

  56.       

  57.     //Set the frequency division and automatic reload of TIM5  

  58.     TIM5->PSC = 84-1;  

  59.     TIM5->ARR = 0XFFFF-1; //The chip manual says it is 16 bits.  

  60.                               

  61.     //Set filtering/mapping/frequency division  

  62.     TIM5->CCMR1 |= 0X1;  

  63.       

  64.       

  65.     //Set the rising edge trigger and enable capture  

  66.     TIM5->CCER |= 0X1;  

  67.       

  68.     // Enable update interrupt and capture interrupt  

  69.     TIM5->DIER |= 0X3;  

  70.       

  71.     // Enable the counter  

  72.     TIM5->CR1 |= 1;  

  73.       

  74.       

  75.     //Set interrupt priority  

  76.     SCB->AIRCR |= 0x5 << 8; //Set group  

  77.     NVIC->IP[50] |= 0; //Set the priority, for details, please analyze the MY_NVIC_Init() function;  

  78.       

  79.     //As long as interrupts are involved, remember to enable interrupts at the end;  

  80.     //If not enabled, the interrupt will not occur  

  81.     NVIC->ISER[1] |= 1<<18; //Enable interrupt;  

  82.       

  83. }  

  84.   

  85.   

  86.   

  87. /*Each time a key is pressed, input captures the key. 

  88. Then every time an interrupt occurs twice, 

  89. Calculate the time difference between the two captures; 

  90. */  

  91. void TIM5_IRQHandler(void){  

  92.       

  93.     /* 

  94.     Interrupt handling function: 

  95.     */  

  96.       

  97.       

  98.     if((TIM5CHA1_CAPTURE_STA & 0x80) != 0x80){ //Indicates that a complete input capture has not yet ended;  

  99.           

  100.           

  101.           

  102.         if((TIM5->SR & 0X1) == 0X1){ //Indicates that an overflow flag has occurred  

  103.               

  104.               

  105.             if((TIM5CHA1_CAPTURE_STA & 0x40) == 0x40){ //Only after capturing a high level,  

  106.                                                         //We accumulate the value of the counter  

  107.                 if((TIM5CHA1_CAPTURE_STA & 0x3f)==0x3f){ //Indicates that the cumulative counter is full;  

  108.                                                         //Here, the high level lasts for a maximum of 4 seconds  

  109.                     TIM5CHA1_CAPTURE_STA |= 0x80;  

  110.                     TIM5CHA1_CAPTURE_VAL = 0xffff;  

  111.                       

  112.                       

  113.                 }else{  

  114.                   

  115.                     TIM5CHA1_CAPTURE_STA++;  

  116.                 }  

  117.                   

  118.                   

  119.             }  

  120.               

  121.         }  

  122.         if((TIM5->SR & 0X2) == 0X2){ //Indicates that the rising edge or falling edge has been captured  

  123.               

  124.               

  125.             if((TIM5CHA1_CAPTURE_STA & 0x40) == 0x40){ //Indicates that the falling edge has been triggered  

  126.                   

  127.                 TIM5CHA1_CAPTURE_STA |= 0x80 ; //Indicates that the capture of one cycle of rising and falling edges has been completed;  

  128.                 TIM5CHA1_CAPTURE_VAL = TIM5->CCR1; //Save the value of the counter when the falling edge trigger occurs;  

  129.           

  130.                 //Set the rising edge trigger and enable capture  

  131.                 TIM5->CCER &= ~(1<<1);  

  132.                   

  133.             }else{ //Indicates that the rising edge has been captured  

  134.                   

  135.                 //Disable the counter of timer 5  

  136.                 TIM5->CR1 &= ~(1);  

  137.                   

  138.                 //Let the counter value be 0 so as to calculate the value from 0 to the value captured by the next falling edge;  

  139.                 TIM5->CNT = 0;  

  140.                   

  141.                 //Set input capture to falling edge trigger  

  142.                 TIM5->CCER &= ~(0XF);  

  143.                 TIM5->CCER |= 0X3;  

  144.                   

  145.                 //Initialize the value to be counted;  

  146.                 TIM5CHA1_CAPTURE_STA = 0;  

  147.                 TIM5CHA1_CAPTURE_STA |= 0X40;  

  148.                 TIM5CHA1_CAPTURE_VAL = 0;  

  149.                   

  150.                 // Enable the counter  

  151.                 TIM5->CR1 |= 1;  

  152.               

  153.             }  

  154.               

  155.         }  

  156.           

  157.     }  

  158.       

  159.     /* 

  160.     Finally, remember to clear the interrupt flag in the interrupt: 

  161.     */  

  162.     TIM5->SR &= ~(0x3);  

  163. }   

  164. /*******************************timer.h*********************************/  

  165. #ifndef _EXIT_H  

  166. #define _EXTI_H  

  167.   

  168.   

  169. void TIM5_Init(void);  

  170.   

  171.   

  172. #endif  

  173. /*******************************test.c***********************************/  

  174. #include "sys.h"  

  175. #include "delay.h"  

  176. #include "beep.h"  

  177. #include "exti.h"  

  178. #include "led.h"  

  179. #include "uart.h"  

  180. #include "usart.h"  

  181.   

  182.   

  183.   

  184. u8 TIM5CHA1_CAPTURE_STA;  

  185. u32  TIM5CHA1_CAPTURE_VAL;  

  186.   

  187.   

  188. int main(void){  

  189.       

  190.     u8 i = 0;  

  191.     long long temp = 0; //The value here is relatively large, so choose long long  

  192.       

  193.     Stm32_Clock_Init(336,8,2,7); //Set the clock, 168Mhz    

  194.     delay_init(168); //Initialize delay function  

  195.     LED_Heat();  

  196.     Beep_Init();  

  197.     TIM5_Init();  

  198.     UART_Init();  

  199.       

  200.       

  201.     while(1){  

  202.           

  203.         PFout(9) = 0;  

  204.         delay_ms(500);  

  205.         PFout(9) = 1;  

  206.         delay_ms(500);  

  207.           

  208.         if((TIM5CHA1_CAPTURE_STA & 0x80) == 0x80){ //If a complete capture cycle (rising edge and falling edge)  

  209.   

  210.   

  211.             i = TIM5CHA1_CAPTURE_STA & 0x3f;  

  212.             printf("TIM5:%d\r\n",i);  

  213.             //Calculate the accumulated time (the time difference between high level and low level)    

  214.             temp = (TIM5CHA1_CAPTURE_STA & 0x3f)*0xffff;  

  215.             temp += TIM5CHA1_CAPTURE_VAL;  

  216.   

  217.               

  218.             printf("temp:%lld us\r\n",temp); //Think about how the printf() function works;  

  219.                   

  220.                 //Reinitialize  

  221.                 TIM5CHA1_CAPTURE_STA = 0;  

  222.                 TIM5CHA1_CAPTURE_VAL = 0;  

  223.   

  224.         }  

  225.               

  226.     }  

  227. }  





experiment analysis:

1. Block diagram of the timer and enlarged version of the input capture block diagram



Note: By detecting the edge signal on TIMx_CHx, when the edge signal changes (such as rising edge/falling edge),

Store the current timer value (TIMx_CNT) into the corresponding capture/compare register (TIMx_CCRx) to complete a capture.


2. Input capture workflow analysis:

<1>


<2>


<3>


<4>


<5>



3. Tips for interrupt handling functions



Precautions:

1. As long as interrupts are involved, you must remember to enable interrupts at the end

2. The key processing is not very good, sometimes it will type several strings of numbers in a row;

To be more precise, the key sometimes jitters a bit, which is equivalent to pressing it several times, but not filtering out the smaller part of the band;


Keywords:STM32 Reference address:Input capture of STM32

Previous article:STM32 learning notes timer input capture experiment
Next article:STM32 timer interrupt

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

STM32 J-LINK SW debugging FAQ
STM32 supports SW debugging, which only requires two ports SWDIO/SWCLK and VCC/GND, and a maximum of four lines can be used for debugging. SWDIO/SWCLK does not require pull-up or pull-down resistors. The No Cortex-M SW Device Found problem is common during debugging. You can try the following methods: 1. The sof
[Microcontroller]
STM32 implements DAC output related settings
Introduction to STM32 DAC         The large-capacity STM32F103 has an internal DAC. The battleship STM32 selected is the STM32F103ZET6, which is a large-capacity product, so it has a DAC module. The DAC module (digital/analog conversion module) of STM32 is a 12-bit digital input, voltage output DAC. The DAC can be con
[Microcontroller]
STM32 implements DAC output related settings
STM32 FSMC control LCD function explanation
I encountered this problem while doing the project. I think the article is well written. I would like to share it with my colleagues who have doubts about the use of FSMC! LCD has the following control lines: CS: Chip Select, low level valid RS: Register Select WR: Write signal, low level valid RD: Read signal, low l
[Microcontroller]
Understanding of NVIC in STM32, with error examples
STM32 has 43 channels of settable interrupt sources; AIRC (Application Interrupt and Reset Register) register has 4 bits for specifying priority. These 4 bits are used to assign preemption priority and sub priority, and are defined in the STM32 firmware library as follows /* Preemption Priority Group------------------
[Microcontroller]
Introduction to STM32 independent watchdog
The STM32's independent watchdog is driven by a dedicated internal 40Khz low-speed clock, which remains effective even if the main clock fails. The principle of the watchdog: When the microcontroller system is disturbed by the outside world, the program may run away, resulting in an infinite loop. The watchdog circuit
[Microcontroller]
Detailed explanation of STM32 general purpose input and output port GPIO BRR, BSRR, ODR registers
Detailed page: http://alanzjl.sinaapp.com/2015/02/gpio_brr_bsrr_odr/ BRR, BSRR, and ODR are all used to control 16-bit pins. Among them, the upper 16 bits of BRR and ODR are unavailable (reserved), and the lower 16 bits are used to control the pins, while both the upper 16 bits and the lower 16 bits of BSRR are avai
[Microcontroller]
Detailed explanation of STM32 general purpose input and output port GPIO BRR, BSRR, ODR registers
STM32 CAN controller
1. CAN only has two signal lines, CAN_High and CAN_Low. CAN communicates in the form of differential signals.  2. There are two types of CAN communication networks: one is a high-speed short-distance closed-loop network that complies with the ISO11898 standard, with a maximum bus length of 40m and a maximum communicat
[Microcontroller]
STM32 independent watchdog (IWDG) and window watchdog (WWDG)
  There was a very popular game called "Watch Dogs". The game used a very new engine technology to create a vast world. The content is that the player Aiden Pearce (the protagonist) is a master of hacker technology. At that time, the world was in a state where all objects were controlled by electronic devices, and the
[Microcontroller]
STM32 independent watchdog (IWDG) and window watchdog (WWDG)
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号