View previous topic :: View next topic |
Author |
Message |
Core2
Joined: 27 Sep 2008 Posts: 22
|
Will this work? |
Posted: Sat Jan 16, 2010 12:13 pm |
|
|
Hello everyone,
My 'C' is rusty. I need to set a signal and wait for a response. Here is my function:
Code: | void getSonar (void)
{
output_high(TRIGGER); //Initiate the Sonar sequence
setup_timer_1(T1_INTERNAL|T1_DIV_BY_2); //Start counting, 0.4uS tick time
while (!echo_rcvd){} //Wait for echo
printf("Range = %lu inches\n\r",range/185); //Print range
//Need to check if 185 is the right number; not so sure.
output_low(TRIGGER);
}
|
Its the While statement I'm not sure about. Do I need the braces? I have the ECHO line tied to RB5 as in IOC. The ISR will SET 'echo_rcvd' to TRUE'
I'm running this on a PIC16F882, compiler 4.093.
Thanks,
Duane |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Sat Jan 16, 2010 3:03 pm |
|
|
You should do the setup timer routine at uC init not in this function. In the function you just call set_timer1(0);
while (condition) {} <--- Is OK, it just waits in that spot until your condition is met. |
|
|
Core2
Joined: 27 Sep 2008 Posts: 22
|
|
Posted: Sat Jan 16, 2010 5:21 pm |
|
|
Thanks for the info. Will move the Timer setup.
What command actually makes the timer start?
Duane |
|
|
bungee-
Joined: 27 Jun 2007 Posts: 206
|
|
Posted: Sat Jan 16, 2010 5:52 pm |
|
|
Core2 wrote: | Thanks for the info. Will move the Timer setup.
What command actually makes the timer start?
Duane | Timer will be running all the time, you'll just set the timer1 to 0 with that command. So you'll be able to get time measurement. |
|
|
Core2
Joined: 27 Sep 2008 Posts: 22
|
|
Posted: Sat Jan 16, 2010 6:01 pm |
|
|
Gotcha. Thanks! |
|
|
|