S
S
Slavka2016-04-21 23:40:42
C++ / C#
Slavka, 2016-04-21 23:40:42

Implementation of the HC-SR04 rangefinder on stm32 using PWM?

For the rangefinder to work, you need to send a signal with a time length of 10 μs to one of its legs no more than every 50 ms, at first I implemented this work simply by delaying the timer

delay(50000);
GPIOB->ODR |= GPIO_ODR_ODR1;
delay(10);
GPIOB->ODR &= ~GPIO_ODR_ODR1;

But for this you need to constantly call the function for measurement, I decided to implement this work using PWM
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;
TIM3->PSC = SystemCoreClock / 1000000 -1; //отстчет каждую 1мкс
TIM3->ARR = 60000; //период 60мс
TIM3->CCR4 = 10; //время импульса 10мкс
TIM3->CCMR2 |= TIM_CCMR2_OC4M_0 | TIM_CCMR2_OC4M_1 | TIM_CCMR2_OC4M_2;
TIM3->CCER |= TIM_CCER_CC4E;
TIM3->CR1 |= TIM_CR1_CEN;

but there was a problem that he makes only one measurement and doesn’t want to anymore, only if you restart the device or debug, then he will again make one measurement and constantly only output it. Could I screw up somewhere in working with PWM?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
iv_k, 2016-04-22
@Slavka_online

and where do you set the GPIO mode to PWM? GPIO_Mode_AF_PP

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question