A
A
Alexander Kunin2012-11-24 22:20:34
Programming
Alexander Kunin, 2012-11-24 22:20:34

STM32F4 Discovery. Question about input capture mode

Hello,

please tell me what I'm doing wrong.
The task is to read the pulse width.
After googling, I decided to use the Input capture mode for the timer.
The result is this code:

#include "stm32f4xx.h"
#include <stm32f4xx_rcc.h>
#include <stm32f4xx_gpio.h>
#include <stm32f4xx_tim.h>
#include <misc.h>
#include "lcd.hpp"
#include "stdio.h"

volatile int IC3ReadValue1 = 0;
volatile int IC3ReadValue2 = 0;
volatile int Capture = 0;
volatile int CaptureNumber = 0;
volatile int dataCaptureReady = 0;

void tim3ConfigRising()
{

  TIM_ICInitTypeDef timICStruct;

  timICStruct.TIM_Channel = TIM_Channel_2;
  timICStruct.TIM_ICPolarity = TIM_ICPolarity_Rising;
  timICStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
  timICStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  timICStruct.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM3, &timICStruct);

}

void tim3ConfigFalling()
{
  TIM_ICInitTypeDef timICStruct;

  timICStruct.TIM_Channel = TIM_Channel_2;
  timICStruct.TIM_ICPolarity = TIM_ICPolarity_Falling;
  timICStruct.TIM_ICSelection = TIM_ICSelection_DirectTI;
  timICStruct.TIM_ICPrescaler = TIM_ICPSC_DIV1;
  timICStruct.TIM_ICFilter = 0x0;

  TIM_ICInit(TIM3, &timICStruct);

}

void initTimers()
{
   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

    /* Частота счёта - 10 мкс, период - 20 мс */
   TIM_TimeBaseInitTypeDef base_timer;
   TIM_TimeBaseStructInit(&base_timer);
   base_timer.TIM_Prescaler = SystemCoreClock / 1000 - 1;
   base_timer.TIM_Period = 2000;
   base_timer.TIM_ClockDivision = 0;
   base_timer.TIM_CounterMode = TIM_CounterMode_Up;
   TIM_TimeBaseInit(TIM3, &base_timer);

   tim3ConfigRising();

   // TIM enable counter
  	 TIM_Cmd(TIM3, ENABLE);

   // Enable the CC2 Interrupt Request
   TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE);

   NVIC_InitTypeDef NVIC_InitStructure;

   NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
   NVIC_Init(&NVIC_InitStructure);
}

void initGpio()
{
  RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

  GPIO_InitTypeDef gpioStruct;
  gpioStruct.GPIO_Mode = GPIO_Mode_IN;
  gpioStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
  gpioStruct.GPIO_Speed = GPIO_Speed_50MHz;
  gpioStruct.GPIO_Pin = GPIO_Pin_7;

  GPIO_Init(GPIOA, &gpioStruct);
}

int main(void)
{

  LiquidCrystal lcd(RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_0, GPIO_Pin_1, GPIO_Pin_2,
      RCC_AHB1Periph_GPIOC, GPIOC, GPIO_Pin_5, GPIO_Pin_4);

  initGpio();
  initTimers();

  char buffer[20];
  char *bufPtr;
  bufPtr = (char*)buffer;
  while(1)
  {

    if (dataCaptureReady)
    {
      /* Capture computation */
        if (IC3ReadValue2 > IC3ReadValue1)
        {
        Capture = (IC3ReadValue2 - IC3ReadValue1);
        }
        else
        {
        Capture = ((0xFFFF - IC3ReadValue1) + IC3ReadValue2);
        }
        /* Frequency computation */
        int TIM3Freq = (uint32_t) SystemCoreClock / Capture;

      dataCaptureReady = 0;
      sprintf(bufPtr, "%d %d", Capture, TIM3Freq);
      lcd.setCursor(0,0);
      lcd.print(bufPtr);
    }

  }

}

extern "C" {
  void TIM3_IRQHandler()
  {

    if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET)
    {
      /* Clear TIM3 Capture compare interrupt pending bit */
      TIM_ClearITPendingBit(TIM3, TIM_IT_CC2);
      if(CaptureNumber == 0)
      {
        /* Get the Input Capture value */
        IC3ReadValue1 = TIM_GetCapture2(TIM3);
        CaptureNumber = 1;

        tim3ConfigFalling();

      }
      else if(CaptureNumber == 1)
      {
        /* Get the Input Capture value */
        IC3ReadValue2 = TIM_GetCapture2(TIM3);

        dataCaptureReady = 1;
        CaptureNumber = 0;

        tim3ConfigRising();

      }
    }
  }
}


But, the stone flower does not come out.
The interrupt handler is not called, the TIM3->CCR2 register is empty, while everything is as expected in GPIOA->IDR.

There is an assumption that the PA07 pin is not “connected” to the timer and this does not cause an interrupt, but the solution has not yet been found

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
Cyril, 2012-11-27
@skyksandr

Yes, that's right. Pin PA07 is not related to the timer, because this is its alternative function, which must be enabled.

gpioStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question