STM32 using timers for input capture

Source: Internet
Author: User

Introduction to input Capture
The input capture mode can be used to measure the pulse width or measurement frequency. STM32 timers, in addition to TIM6 and TIM7, have input capture capabilities for other timers.

STM32 input capture, simply by detecting the edge signal on the TIMX_CHX, when the edge signal jumps (such as rising edge/falling edge), the value of the moment timer (timx_cnt) stored in the corresponding channel capture/comparison register (TIMX_CCRX) inside, Complete one capture.

To use the TIM2 input to capture the configuration steps:
1, turn on the TIM2 clock, configure the PA0 as the drop-down input.
Need to use PA0 as the pulse input above the tim2_ch1.
2, set TIM2 arr and PSC, Prescaler factor and pre-loaded value
3, set the CCMR1 of TIM2
The CCMR1 register controls the mode of input capture 1 and 2, including mapping relationships, filtering, and dividing. 、
4. Set the TIM2 ccer, turn on the input capture, and set the capture to the rising edge.
The ccer is the switch of the timer and can set the edge of the input capture.
5, set TIM2 Dier, enable capture and update interrupt, and write interrupt service function.
Because we're going to capture the pulse width of the high-level signal. So, the first catch is the rising edge, and the second catch is the falling edge. Therefore, after capturing the rising edge, set the capture edge to the falling edge. At the same time, if the pulse width is longer, then the timer will overflow and the overflow must be processed. Both of these things are implemented in an outage, so we have to turn on capture interrupts and update interrupts here.
6, set TIM2 CR1, enable timer

The code is as follows:

//Tim2_cap Initialization//arr automatically reloads values//PSC prescaler FactorvoidTim2_cap_init (U16 arr,u16 PSC) {RCC->apb1enr |=1<<0;//Enable timer clockRcc->apb2enr |=1<<2;//Enable Gpioa clockGPIOA-&GT;CRL &=0xfffffff0;//Gpioa drop-down inputGPIOA-&GT;CRL |=0x00000008; Gpioa->odr |=0<<0;//Drop- down inputTIM2->arr=arr;//an update interrupt occurs when the auto reload value timer counts to this valuetim2->psc=psc;//Prescaler FactorTIM2-&GT;CCMR1 |=1<<0;//CC1 Channel is configured as input, IC1 maps to TI1TIM2-&GT;CCMR1 |=1<<4;//sampling frequency, effective after 2 eventsTIM2->ccer |=1<<0;//capture EnableTim2->ccer |=0<<1;//Rising edge CaptureTIM2->dier |=1<<1;//allow capture/compare interruptsTim2->dier |=1<<0;//Allow update interruptsTIM2-&GT;CR1 |=1<<0;//Enable counterMy_nvic_init (2,0, TIM2_IRQN,2);//Interrupt Grouping}

If you need to complete a high-level pulse-width capture, you need to re-set the capture direction after the completion of the rising edge capture, set to a falling edge capture, and when a successful completion of the rising edge-falling edge capture, you can use the counter value of the timer to calculate the high-level pulse width of the capture.

At the same time, when the rising edge is captured, there will be a counter overflow situation, we also need to handle the situation of counter overflow in the interrupt.

So we need to do two things in the timer interrupt.

1. Handling Overflow situation

2. Conversion between falling edge of rising edge

There are many ways, and here's how I handle it:

//Interrupt HandlingU8 Tim2_cap_sta =0; U16 Tim2_cap_value=0;voidTim2_irqhandler (void) {U16 INT_IP= tim2->SR; if(int_ip& (1<<0)){//Overflow        if(tim2_cap_sta&0x3F){//Pulse width too long            if(tim2_cap_sta&0x40){//the rising edge has been capturedTim2_cap_sta |=1<<7;//set up a successful captureTim2_cap_value = tim2->ccr1;//gets the captured valueTim2->ccer &=0<<1;//Rising edge CaptureTim2->ccer |=1<<0;//Enable to capture            }            Else{Tim2_cap_sta=0; Tim2_cap_value=0; }        }        ElseTim2_cap_sta++; }            if(int_ip& (1<<1)){//Capture        if(tim2_cap_sta&0x40){//the rising edge has been capturedTim2_cap_sta |=1<<7;//set up a successful captureTim2_cap_value = tim2->ccr1;//gets the captured valueTim2->ccer &=0<<1;//Rising edge CaptureTim2->ccer |=1<<0;//Enable to capture}Else{//We haven't captured the rising edge yet .Tim2_cap_sta |=1<<6;//set capture to rising edgeTim2_cap_value =0; TIM2->ccer |=1<<1;//Falling Edge Capturetim2->cnt =0;//Counter Clear 0}} TIM2->sr=0;//Clear Timer status (interrupt flag)}

There are two things to be aware of during interrupt processing:
1. Update interrupt and capture interrupt processing order

2, reset to the rising edge of the capture of the writing method

      0<<1// rising edge capture      1<<0//  Enable to capture

STM32 using timers for input capture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.