2021-04-16 01:28 AM
#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 */
}
2021-04-16 02:59 AM
> 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
2021-04-16 03:24 AM
> 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
2021-04-16 03:45 AM
>> 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
2021-04-16 03:59 AM
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.
2021-04-16 04:49 AM
How to calculate Frequency and Pulse width of capture signal in the above code ?
2021-04-16 06:39 AM
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
2021-04-17 04:18 AM
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;
}