2016-12-10 02:23 AM
hi all
while (1)
{ int time; int i=0; GPIO_SetBits(GPIOA,GPIO_Pin_1); //delay_us(10); Delay(240); //Delay01ms(100); GPIO_ResetBits(GPIOA,GPIO_Pin_1); while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0); while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0))i++; time = (int)i/9500000; distance_mm =(int) (time*344)*1000/2; //delay_us(1000); //Delay01ms(1000); Delay(1440000); }it stop at '
while(GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0)==0);' , and
distance_mm=0
i use keil 5.20
2016-12-10 04:12 AM
I suppose this is continuation to
https://community.st.com/0D50X00009Xket0SAB
Well if your program hangs at a loop waiting for an input to get set, then either that input never gets set (this can be confirmed by looking at that pin by an oscilloscope) - or the pin is not configured as an input.
You should implement some sort of timeout. Also, measuring time by incrementing in a loop is not a good practice, you should learn how to use timer input capture for this purpose.
JW
2016-12-10 05:56 AM
Advice: If you are looking at measuring accurately time between signal edges, get assistance in HW Timers.
The timer has 'CC' which formally is called 'input captures' (an input signal to monitor). When there is an edge, the free-running timer value will be taken as snapshot. Connect the signal to 2 of the timer channels, one for each edge, then you can measure the period and duty cycle. Make it even nicer: Generate a Timer interrupt which will do this and once the data you need is available, set a volatile bit to 1 (by interrupt) to expose your data to the main loop, and clear it when the main loop has used it and is ready for the next... This way, your main loop won't be having any SW delay.
A SW way to measure time will be having some 'jitter' or 'noise' in the measured value because of the interrupts,