cancel
Showing results for 
Search instead for 
Did you mean: 

Why doesn't my input compare react to input signals?

Emper
Associate II

Good evening everyone,

At the moment I am trying to configure an inpute compare pin. Eventually we are going to calculate the frequency of the input signal etc etc, but for now we need to get the pin working.

GPIOA->MODER = (GPIOA->MODER & ~GPIO_MODER_MODER15) |(0b10 << GPIO_MODER_MODER15_Pos);

GPIOA->PUPDR = (GPIOA->PUPDR &~GPIO_PUPDR_PUPDR15) | (0b10 << GPIO_PUPDR_PUPDR15_Pos);

RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;

TIM2->PSC = 0.098;

TIM2->ARR = 65535;

TIM2->CCER &= ~TIM_CCER_CC1E;

TIM2->CCMR1 |= TIM_CCMR1_CC1S_0;

TIM2->CCER |= TIM_CCER_CC1P_Msk;

TIM2->CCMR1 |= TIM_CCMR1_IC1PSC;

TIM2->CCER |= TIM_CCER_CC1E;

TIM2->DIER |= TIM_DIER_UIE;

TIM2->DIER |= TIM_DIER_CC1IE;

TIM2->CR1 |= TIM_CR1_CEN;

  

NVIC_EnableIRQ(TIM2_IRQn);

void TIM2_IRQHandler(void)

{

 char *msgStart = "Rising edge\r\n";

 HAL_UART_Transmit(&huart2, (uint8_t*)msgStart, strlen(msgStart), HAL_MAX_DELAY);

 TIM2->SR &= ~TIM_SR_CC1IF;

}

Where the prescaled timer runs at 1kHz so that we can measure in ms.

So at the moment I have configured an input pin to alternate mode, set the input compare settings and made the interrupt handler.

This doesn't work when put a signal on pin PA15.

What am I doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
  • do you enable GPIOA clock in RCC?
  • you don't set the AF for TIM2 in GPIO_AFR[] (btw. which STM32 are you using?)
  • PSC set to double (floating point)? probably harmless here, but certainly strange
  • you set the input capture prescaler to 8 events, i.e. it will capture only on every 8th falling (given you set CC1P) edge, are you aware of that?
  • you enable Update Interrupt in DIER, yet you don't handle it in the ISR

When in doubt, read out and check the registers' content; observe registers in debugger, also check if you can see changes on the input pin in the respective GPIO_IDR pin, if you see the timer running, and if you can see the timer status flags changing accordingly.

JW

View solution in original post

3 REPLIES 3
  • do you enable GPIOA clock in RCC?
  • you don't set the AF for TIM2 in GPIO_AFR[] (btw. which STM32 are you using?)
  • PSC set to double (floating point)? probably harmless here, but certainly strange
  • you set the input capture prescaler to 8 events, i.e. it will capture only on every 8th falling (given you set CC1P) edge, are you aware of that?
  • you enable Update Interrupt in DIER, yet you don't handle it in the ISR

When in doubt, read out and check the registers' content; observe registers in debugger, also check if you can see changes on the input pin in the respective GPIO_IDR pin, if you see the timer running, and if you can see the timer status flags changing accordingly.

JW

Thanks for your reply! We are using the Nucleo F303RE.

  • do you enable GPIOA clock in RCC?

We didn't so we added it, still not working

  • you don't set the AF for TIM2 in GPIO_AFR[] (btw. which STM32 are you using?)

We indeed didn't and we added it, still not working

  • PSC set to double (floating point)? probably harmless here, but certainly strange

It's because we want the prescaled clock to 1000Hz

  • you set the input capture prescaler to 8 events, i.e. it will capture only on every 8th falling (given you set CC1P) edge, are you aware of that?

No, we want to count the rising edges, all of them

  • you enable Update Interrupt in DIER, yet you don't handle it in the ISR

We do actually, it's the function I posted;

void TIM2_IRQHandler(void)

{

 char *msgStart = "Rising edge\r\n";

 HAL_UART_Transmit(&huart2, (uint8_t*)msgStart, strlen(msgStart), HAL_MAX_DELAY);

 TIM2->SR &= ~TIM_SR_CC1IF;

}

We are out of options, you know what's wrong?

As I've said above:

When in doubt, read out and check the registers' content; observe registers in debugger, also check if you can see changes on the input pin in the respective GPIO_IDR pin, if you see the timer running, and if you can see the timer status flags changing accordingly.

JW