2021-04-24 12:53 AM
The Pulse width value of the waveform in input capture is not matching with orginal Pulse from sensor output .
Please help.
#include<NEW.h>
#include<stdio.h>
#include<stdarg.h>
void Ultra_setting();
void delay(int val);
void Tim3_Capture();
void Uart1_config();
void uart_printf(const char *fmt, ...);
uint32_t timeStamp1 = 0,timeStamp2=0,width=0,Distance=0;
unsigned int flag=0;
void TIM3_IRQHandler()
{
uint32_t time = TIM3->CCR1;
if (flag==0)
{
timeStamp1=time;
}
else
{
timeStamp2= time;
if(timeStamp2>timeStamp1)
{
width= timeStamp2-timeStamp1;
}
else if(timeStamp2<timeStamp1)
{
width= (0xffff - timeStamp1) + timeStamp2;
}
Distance = width *(.034/2);
flag=0;
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);
}
flag++;
}
int main()
{
//GPIO A configuration
SystemInit();
Ultra_setting();
Uart1_config();
Tim3_Capture();/*Enable Tim3 capture*/
GPIOA->ODR |=1<<12;/* Turning ON PA12pin */
delay(100);/* 10 microseconds ON*/
GPIOA->ODR &=~(1<<12);/* Turning ON PA12 pin */
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 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_RE;
USART1->CR1 |=USART1_TE;
}
void Tim3_Capture()
{
//input capture mode
RCC->APB1ENR |= 2; //ENABLE TIMER 3
TIM3->PSC = 16; /*divide by 16 - 1 Mhz*/
TIM3->CCMR1 |=TIM2_CCIS;//01: CC1 channel is configured as input, IC1 is mapped on TI1.
TIM3->CCER |=1; //enable CH1 input capture
TIM3->CCER |=5<<1; //enable 11: noninverted/both edges
WRITE(NVIC_ISER0 ,(1<<29));//Enable timer Interrupt.
TIM3->DIER =1<<1; //Bit 1 CC1IE: Capture/Compare 1 interrupt enable
TIM3->CR1 = 1; //enable TIM3
TIM3->CNT=0;
}
void Ultra_setting()
{
RCC->AHB1ENR |=1; /* Enabling clock for GPIOA port*/
GPIOA->MODER |=2<<12;/*Alternative function in PA6 pORT*/
GPIOA->MODER |=1<<24;/*General Purpose output function in PA12 Port*/
GPIOA->AFR[0]|=2<<24; /* Alternate function low register is AF1 is TIM3_CH1 */
GPIOA->OSPEEDR|=1<<12;/*Medium speed */
}
void delay(int val)
{
RCC->APB1ENR |=1<<0; /* TIM2 clock enabled*/
TIM2->CNT=0;
TIM2->PSC=1;
TIM2->CR1 |=1;
while(TIM2->CNT < val);
TIM2->SR=0;
}
Solved! Go to Solution.
2021-04-24 01:09 AM
TIMx_PSC is preloaded, you need to force update by setting TIMx_EGR.UG to get it "active".
JW
2021-04-24 01:09 AM
TIMx_PSC is preloaded, you need to force update by setting TIMx_EGR.UG to get it "active".
JW
2021-04-24 02:01 AM
Thank you again JW.
Now Project is working perfectly