2024-02-18 10:36 PM - edited 2024-02-19 05:26 AM
float init_sensor(void){
uint32_t numTicks;
float distance;
RCC->AHB1ENR |= (1U<<1);
GPIOB->MODER &=~ (1U<<19); // pin 9 as output
GPIOB->MODER |= (1U<<18); // pin 9 as output
GPIOB->MODER &=~ (1U<<16); // pin 8 as input
GPIOB->MODER &=~ (1U<<17); // pin 8 as input
GPIOA->MODER |= (1U<<10);
GPIOA->MODER &=~ (1U<<11);
RCC->CFGR |= (1U<<4);
RCC->CFGR |= (1U<<5);
RCC->CFGR &=~ (1U<<6);
RCC->CFGR |= (1U<<7);
while(1){
//generate 10uS pulse and 250uS delay
GPIOB->ODR |= (1U<<9);
delay_us(10);
GPIOB->ODR &=~ (1U<<9);
delay_us(200);
numTicks=0;
while((GPIOB->IDR) & (1U<<8)){
numTicks++;
}
distance = (numTicks)/58;
}
return distance;
}
Hello,
I wrote this code,
and the code get stuck in the while GPIOB condition and I don't know why..
it's also happens when I tried similar condition to check UIF timer flag.
I checked it with my oscilloscope and pin8 generate pulses so numTick should be increment, I don't know why the code not processed.
Edit: the problem is the IDR not updating value although the pin get signals from the sensor.
any ideas?
2024-02-18 10:56 PM
First of all, when posting code, please use the </> button, that makes it more readable.
Next, although it's really nice that you are using registers and not HAL functions, it will help you and us if you not use (1U<<9) for register settings, but the bit defines from the STM32xyz.h file. Believe me, in a few weeks you will probably not remember what bit x of register y means.
Now about your code:
1) Setup: try to set mode of input pin to input MODER.xy = 00, now it's set to analog
2) Then you set pin 9 to 1, then to 0, then you wait in the while loop that pin 8 gets 1.
Even assuming that pin 8 and 9 are connected, this leads to getting stuck in the while loop, because pin 8 = 0, so pin 9 = 0
2024-02-18 11:34 PM
thank you for the answer.
I don't realize you proper.
are you saying that the problem is pin configuration? that's why the while condition not working?
I'm pretty sure the pins configuration is ok because I copied this code from a working project.
2024-02-19 01:09 AM
I'm not sure what "analog mode" = 11 is, I guess input without Schmitt-trigger.
Maybe try 00.
Anyway, I made a mistake and didn't read your while loop properly.
Right now you set pin 9 = 0, then the next while(pin 8 == 1) numTicks++; never increments numTicks because pin 8 = 0.
What do you actually want to do?
And again, are pin 8 & pin 9 connected?
2024-02-19 01:31 AM
I'm trying to generate pulse for ultra-sonic distance sensor(HC-SR04) and it works. its calculate distance by counting the pulse width time.
in my case pin 9 should send a pulse from the stm32 and pin 8 ( input) should get his "1" from the sensor - and according to my oscilloscope it's working but for some reason the code not increasing numTicks variable although there is a "1" in pin8.
2024-02-19 02:01 AM
Whats coming in , pin8 ? (show scope screen)
+
Maybe your code is working , but no timeout there. So if it "miss" the pulse, it will wait forever.
I would just do some limit here:
numTicks=0;
while((GPIOB->IDR) & (1U<<8)
{
numTicks++;
if (numTicks > 10000000) break; // max. possible distance
}
2024-02-19 02:57 AM - edited 2024-02-19 03:36 AM
yes pin 8 is input.
I added 2 lines to check :
while(!((GPIOB->IDR) & GPIO_IDR_ID8)){
check++;
}
and the variable check is increase which means the problem is the IDR don't get "1" value at all..
2024-02-19 04:10 AM
Wait until the input becomes 1 BEFORE you start counting the time. Your code has many other problems but this is the starting point.
2024-02-19 04:13 AM
that what I did in the while..
the while says "wait until input "1" and then count numTicks"