cancel
Showing results for 
Search instead for 
Did you mean: 

TimeStamp1(Rising Edge) value is capturing as Zero always in ISR while Measuring Width of Pulse

Nchun.1
Senior

#include<NEW.h>

#include<stdio.h>

#include<stdarg.h>

void Ultra_setting();

void delay();

void Tim3_Capture();

void Uart1_config();

void delay1();

void uart_printf(const char *fmt, ...);

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

unsigned int flag=0;

void TIM3_IRQHandler()

{

  flag++;

  if (flag==1)

  {

   timeStamp1=TIM3->CCR1;

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

   uart_printf("%d\n", TIM3->CCR1);

  }

  else

  {

  timeStamp2=TIM3->CCR1;

  TIM3->CNT=0;

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

  uart_printf("The distance is %d %d %d %d\n", Distance, timeStamp1,timeStamp2,width);

  }

}

int main()

{

  //GPIO A configuration

SystemInit();

Ultra_setting();

Gpio_Init();

Uart1_config();

  GPIOA->ODR =1<<8;/* Turning ON PA8pin */

  delay();/* 10 microseconds ON*/

  GPIOA->ODR =0<<8;/* Turning ON PA8 pin */

  Tim3_Capture();

  while(1);

}

void Gpio_Init()

{

  RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/

  GPIOA->MODER |=2<<12;/*Alternative function in PA6 pORT*/

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

}

char buf[512];

void uart_printf(const char *fmt, ...)

{

int ret;

va_list args;

va_start(args, fmt);

ret = vsnprintf(buf, 512, fmt, args);

va_end(args);

if (ret > 0)

{

for (int i =0; i < ret; i++)

{

USART1->SR = 0;

USART1->DR = buf[i];

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

}

}

}

void Uart1_config()

{

RCC->APB2ENR |=USART1_CLK;

RCC->AHB1ENR |=1;

GPIOA->MODER |=2<<18;/* Enabling Mode of PA9 */

GPIOA->MODER |=2<<20;/*Enabling Mode of PA9 */

GPIOA->OSPEEDR |=3<<18;/* Enabling High speed of PA9*/

GPIOA->OSPEEDR |=3<<20;/* Enabling High speed of PA10*/

GPIOA->AFR[1] |=7<<4;/*Enabling AFR7 of PA9 */

  GPIOA->AFR[1] |=7<<8;/*Enabling AFR8 of PA10 */

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->CCMR1 |= (0b01<<0)|(0b0100<<4);

//  TIM3->CCMR1|=TIM2_CCIS;

  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

  // TIM3->CNT=0;

}

void Ultra_setting()

{

  RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/

GPIOA->MODER |=1<<16;/*General Purpose output function in PA9 Port*/

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

}

void delay()

{

 RCC->APB1ENR |=1<<0; /* TIM2 clock enabled*/

 TIM2->CNT=0;

 TIM2->ARR=70;

 TIM2->PSC=1;

 TIM2->CR1 |=1;

 while((!TIM2->SR & 1<<0));

 TIM2->SR=0;

}

2 REPLIES 2

Toggle a GPIO output pin when entering the TIM interrupt and observe on oscilloscope. You'll probably be surprised.

> TIM3->EGR = 1<<1;

This forces an interrupt.

And you don't clear the interrupt source in the ISR.

JW

Nchun.1
Senior

0693W000008zsMZQAY.png 

Sorry ,After Toggling pin i am not finding any difference after/begore disabling TIM3->EGR for below.

Toggle a GPIO output pin when entering the TIM interrupt and observe on oscilloscope. You'll probably be surprised.

The timeStamp1 value is getting updated after the Disabling Event Generation Register

> TIM3->EGR = 1<<1;

#include<NEW.h>

#include<stdio.h>

#include<stdarg.h>

void Ultra_setting();

void delay();

void Tim3_Capture();

void Uart1_config();

void delay1();

void Gpio_Init();

void uart_printf(const char *fmt, ...);

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

unsigned int flag=0;

void TIM3_IRQHandler()

{

  flag++;

  GPIOC->ODR =~(1<<8);/*Toggling PC8 pin */

  if (flag==1)

  {

   timeStamp1=TIM3->CCR1;

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

  }

  else

  {

   timeStamp2=TIM3->CCR1;

   TIM3->CNT = 0;

    if(timeStamp2>timeStamp1)

    {

     width= timeStamp2-timeStamp1;

    // uart_printf("The distance is %d %d %d %d\n", Distance, timeStamp1,timeStamp2,width);

    }

    else if(timeStamp2<timeStamp1)

    {

      width= (0xffff - timeStamp1) + timeStamp2;

    }

    Distance =width * 0.34/2;

    flag=0;

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

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

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

   uart_printf("The distance is %d %d %d %d\n", Distance, timeStamp1,timeStamp2,width);

  }

}

int main()

{

  //GPIO A configuration

   TIM3->CNT-0;

SystemInit();

Ultra_setting();

Gpio_Init();

Uart1_config();

  // while (1)

  GPIOA->ODR =1<<8;/* Turning ON PA8pin */

  delay();/* 10 microseconds ON*/

  GPIOA->ODR =0<<8;/* Turning ON PA8 pin */

  Tim3_Capture();

while(1);

}

char buf[512];

void uart_printf(const char *fmt, ...)

{

int ret;

va_list args;

va_start(args, fmt);

ret = vsnprintf(buf, 512, fmt, args);

va_end(args);

if (ret > 0)

{

for (int i =0; i < ret; i++)

{

USART1->SR = 0;

USART1->DR = buf[i];

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

}

}

}

void delay1()

{

RCC->APB1ENR |=1<<0; /* TIM2 clock enabled*/

TIM2->ARR=10000;

  TIM2->PSC=1999;

  TIM2->CR1 |=1;

  while((!TIM2->SR & 1<<0));

TIM2->SR=0;

}

void Gpio_Init()

{

  RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/

  RCC->AHB1ENR |=4; /* Enabling clock for GPIOC port*/

  GPIOA->MODER |=2<<12;/*Alternative function in PA6 pORT*/

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

  GPIOC->MODER |=1<<16;/* Enabling general purpose output in PC8 Pin */

}

void Uart1_config()

{

RCC->APB2ENR |=USART1_CLK;

RCC->AHB1ENR |=1;

GPIOA->MODER |=2<<18;/* Enabling Mode of PA9 */

GPIOA->MODER |=2<<20;/*Enabling Mode of PA9 */

GPIOA->OSPEEDR |=3<<18;/* Enabling High speed of PA9*/

GPIOA->OSPEEDR |=3<<20;/* Enabling High speed of PA10*/

GPIOA->AFR[1] |=7<<4;/*Enabling AFR7 of PA9 */

  GPIOA->AFR[1] |=7<<8;/*Enabling AFR8 of PA10 */

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->CCMR1 |= (0b01<<0)|(0b0100<<4);

//  TIM3->CCMR1|=TIM2_CCIS;

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

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

  TIM3->DIER =1<<1;

  // TIM3->EGR=1<<1;

  TIM3->CR1=1;

  TIM3->CNT=0;

}

void Ultra_setting()

{

  RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/

GPIOA->MODER |=1<<16;/*General Purpose output function in PA9 Port*/

GPIOA->OSPEEDR|=1<<16;/*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;

}