TIM3-> CCR1 is not capturing TIM3->CNT value in input capture function .I am trying to interface HC-sr04 Ultrasonic sensor to STM32F401. I am giving input to TRIG pin with 10 micro second ON and Need to Measure Pulse from ECHO pin of the Sensor.
#include<NEW.h>
void Ultra_setting();
void delay();
int timeStamp = 0;
int main()
{
//GPIO A configuration
Ultra_setting();
Gpio_Init();
//input capture mode
RCC->APB1ENR |= 2; //ENABLE TIMER 3
TIM3->PSC = 16; //divide by 16
TIM3->CCMR1 |= (0b01<<0)|(0b0100<<4); //set CH1 to capture every edge
TIM3->CCER = 1; //enable CH1 input capture
TIM3->CR1 = 1; //enable TIM3
while (1)
{
TIM3->CNT=0;
GPIOA->ODR =1<<9;/* Turning ON PA9 pin */
delay();/* 10 microseconds ON*/
GPIOA->ODR =0<<9;/* Turning OFF PA9 pin */
while (!(TIM3->SR & 2));
timeStamp = TIM3->CCR1;
}
}
void Ultra_setting()
{
RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/
GPIOA->MODER |=1<<18;/*General Purpose output function in PA9 Port*/
GPIOA->OSPEEDR|=1<18;/*Medium speed */
}
void delay()
{
RCC->APB1ENR |=1<<0; /* TIM2 clock enabled*/
TIM2->CNT=0;
TIM2->ARR=98;
TIM2->PSC=1;
TIM2->CR1 |=1;
while((!TIM2->SR & 1<<0));
TIM2->SR=0;
}
void Gpio_Init()
{
RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/
GPIOA->MODER |=2<<12;/*Alternative function in PA6 pORT*/
GPIOA->AFR[0]|=2<<8; /* Alternate function low register is AF1 is TIM3_CH1 */
}