AnsweredAssumed Answered

Can't read CCR1 value stm32f3

Question asked by Rafał Kosewski on Aug 9, 2017
Latest reply on Aug 10, 2017 by Jive Tihs

Hello guys,

So I'm doing simple timer capture input practice exercise. I have HC-SR 04 sensor and want to drive it with stm32f3 discovery board. If someone could check what is wrong with my code:

 

 

#include "stm32f303xc.h"

long cnt1 = 0;
long cnt2 = 0;
short x = 0;
void Delay(uint32_t circles);

int main(void){
RCC->AHBENR = RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOEEN; //clock for A and E ports
RCC->APB1ENR = RCC_APB1ENR_TIM3EN; //clock for timer
GPIOE->MODER |= GPIO_MODER_MODER11_0 // Green
| GPIO_MODER_MODER10_0 // Orange
| GPIO_MODER_MODER9_0 // Red LED output mode
| GPIO_MODER_MODER2_0; // PE02 - TRIGG PIN outputmode

TIM3->CCMR1 = TIM_CCMR1_CC1S_0; //CC AS INPUT, MAP TI1 (PE03)
TIM3->CCER = TIM_CCER_CC1NP_Msk | TIM_CCER_CC1P_Msk; // EVENT ON RISING AND FALLING EDGE
TIM3->CCMR1 = TIM_CCMR1_IC1PSC; // NO PSC - CAPTURE EACH EVENT
TIM3->CCER = TIM_CCER_CC1E; // ENABLE INPUT CAPTURE COMPARE MODE
TIM3->DIER = TIM_DIER_CC1IE; // ENABLE CC INTERRUPT
TIM3->CR1 = TIM_CR1_CEN; // ENABLE TIM3
NVIC_EnableIRQ(TIM3_IRQn); // ENABLE NVIC INTERRUPT FOR TIM3

Delay(800000); // Wait some time

GPIOE->ODR |= GPIO_ODR_2; // Set PE02
Delay(8000); // Wait 10us
GPIOE->ODR &= ~(GPIO_ODR_2); // Reset PE02


while(1){}
}
void TIM3_IRQHandler(void)
{
if(x==0)
{
cnt1 = TIM3->CCR1; // GET VALUE
x++; // REMEMBER THAT NEXT IS FALLING EDGE
GPIOE->ODR |= GPIO_ODR_10; // SET LED TO CONFIRM
}
else if(x==1)
{
cnt2 = TIM3->CCR1; // GET VALUE
x--; // NEXT IS RISING EDGE
GPIOE->ODR |= GPIO_ODR_11; // SET LED TO CONFIRM
}
}
// SysTick clock = 8MHz so you get 1sec delay afret 8000000 circles
//
void Delay(uint32_t circles)
{
SysTick->LOAD = circles - 1;
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE_Msk |
SysTick_CTRL_ENABLE_Msk;
while(!(SysTick->CTRL & SysTick_CTRL_COUNTFLAG_Msk));
SysTick->CTRL &= ~SysTick_CTRL_ENABLE_Msk;
}

 

Problem is that CCR1 value is always zero, so the cnt1 and cnt2 won't get value, however, both LEDs lights. 

Outcomes