cancel
Showing results for 
Search instead for 
Did you mean: 

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.

Nchun.1
Senior

#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 */

}

7 REPLIES 7

> GPIOA->AFR[0]|=2<<8; /* Alternate function low register is AF1 is TIM3_CH1 */

Why 2 << 8?

> GPIOA->OSPEEDR|=1<18;/*Medium speed */

You meant << instead of <

JW

> GPIOA->AFR[0]|=2<<8; /* Alternate function low register is AF is TIM3_CH1 */

Enabling AF02(alternative function ) in PA6 pin TIM3_CH1

 GPIOA->OSPEEDR|=1<18;/*Medium speed */

Above is corrected now

But still problem exists

>> GPIOA->AFR[0]|=2<<8; /* Alternate function low register is AF is TIM3_CH1 */

 >>

>> Enabling AF02(alternative function ) in PA6 pin TIM3_CH1

No, it is not.

JW

Thank you for correcting me.

 GPIOA->AFR[0]|=2<<24; /* Alternate function low register is AF1 is TIM3_CH1 */

Now values are capturing in TIM3->CCR1 Register.

How to calculate Frequency and Pulse width of capture signal in the above code ?

If you are sure you can capture every edge, then pulse width is given by difference between two consecutive edges, and frequency is given as reciprocal of period, which in turn is difference between two edges one apart.

JW

Hi, Getting Junk data in terminal While doing UART1 communication,checked Baud Rate setting every thing is fine.

PC is getting Restarted.

Please Help.

#include<NEW.h>

void Ultra_setting();

void delay();

void Tim3_Capture();

void Uart1_config();

uint8_t timeStamp1 = 0,timeStamp2=0,width=0,Distance=0;

unsigned int flag=0;

void TIM3_IRQHandler() /* Using interrupt for capturing Rising & falling edge */

{

  flag++;

  if (flag==1)

  {

    timeStamp1=TIM3->CCR1;

    TIM3->CCER|=1<<1;/*Change to Falling Edge */

  }

  else

  {

   timeStamp2=TIM3->CCR1;

     if(timeStamp2>timeStamp1)

     {

     width= timeStamp2-timeStamp1;

      }

     else if(timeStamp2<timeStamp1)

     {

       width= (0xffff - timeStamp1) + timeStamp2;

     }

    Distance = width * .034/2;

    flag=0;

    TIM3->CCER =1;//*Change to Rising Edge */

    WRITE(NVIC_ISER0 ,(0<<29));/*Disabling TIM3 Interrupt */

    TIM3->CR1=0;/*Disable Counter */

  }

  //USART1->SR=0;

  //USART1->DR='c';

}

int main()

{

  //GPIO A configuration

SystemInit();

Ultra_setting();

Gpio_Init();

Uart1_config();

  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 */

   Tim3_Capture();

   USART1->SR=0;

      USART1->DR='c';/* Getting junk Data in Usart Terminal */

   while(!(USART1->SR & (1<<6)) );

   }

}

void Uart1_config()

{

 RCC->APB2ENR |=USART1_CLK;/* Enabling UART1*/

 RCC->AHB1ENR |=1<<1;/* Enabling PORT B clock */

 GPIOB->MODER |=2<<12;/* Enabling Mode of PB6*/

 GPIOB->MODER |=2<<14;/*Enabling Mode of PB7 */

 GPIOB->OSPEEDR |=3<<12;/* Enabling High speed of PB6*/

 GPIOB->OSPEEDR |=3<<14;/* Enabling High speed of PB7*/

 GPIOB->AFR[0] |=7<<24;/*Enabling AFR7 of PB6*/

 GPIOB->AFR[0] |=7<<28;/*Enabling AFR7 of PB7 */

 USART1->CR1 |=USART1_EN;

 USART1->BRR =((3<<0) | (0x68<<4)); /*9600 Baud Rate*/

 // USART1->CR1 |=USART1_PE;

 // USART1->CR1 |=USART1_ODD;

 USART1->CR1 |=USART1_RE;

 USART1->CR1 |=USART1_TE;

}

void Tim3_Capture()

{

//input capture mode

  RCC->APB1ENR |= 2;   //ENABLE TIMER 3

  TIM3->PSC = 16;    //divide by 16

  TIM3->CCMR1 |= (0b01<<0)|(0b0100<<4);

  TIM3->CCER = 1;   //enable CH1 input capture

  WRITE(NVIC_ISER0 ,(1<<29));

  TIM3->EGR = 1<<1;

  TIM3->DIER =1<<1;

  TIM3->CR1 = 1; //enable TIM3

}

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;

}