PDA

View Full Version : TIMER1 or RCTIME?



mcbeasleyjr
- 29th December 2008, 01:15
A friend and I are working on a robotics project. Our mobile robot application utilizes infrared technology for object detection, and eventually, communication. However, we have recently decided to implement an ultrasonic range finder on this project. The problem we have is that we are not familiar with how to program the TIMER registers nor how to work the interrupts. We are using one pin of our PIC16F877A to drive power to a 555 timer, which in turn, generates roughly a 42khz signal to be transmitted by the ultrasonic range finder. Once this signal is received, it sends a high pulse back to another pin of the pic. The problem we are having is getting the amount of time it takes from sending the signal to receiving the signal. As many of you know, we need the elapsed time in order to calculate the distance from the object. I've been trying to read up on both the TIMER registers and the RCTIME command as possibilities for helping us fix this problem but as of yet I've really gotten no where. If any of you could provide us with a sample code to go by we would greatly appreciate it. Thank you all for your time.

aratti
- 29th December 2008, 01:30
You should read the thread below, which I sure it should solve your problem.

http://www.picbasic.co.uk/forum/showthread.php?t=190

Al.

mcbeasleyjr
- 29th December 2008, 01:45
I'm not too familiar with assembly or c code. I am using the basic like language written in microcode studio for this.

Acetronics2
- 29th December 2008, 14:04
amount of time it takes from sending the signal to receiving the signal. As many of you know, we need the elapsed time in order to calculate the distance from the object. I've been trying to read up on both the TIMER registers and the RCTIME command as possibilities for helping us fix this problem but as of yet I've really gotten no where. If any of you could provide us with a sample code to go by we would greatly appreciate it. Thank you all for your time.


Hi, ...

Let's see:

sound speed @ 20°C ( SQR ( 1.4*287*(273+20)) = ~ 340 m/s

say your min distance will be ~ 10 cm ... time will be 2x.1/340 ~ 600 µS

RCTime @ 20 Mhz gives 2µS granularity ... for a 16 bits value

so, the range will be 10 cm to 10cm* 65535/300 = 21.8 meters.

Looks fine ...

Alain

mcbeasleyjr
- 30th December 2008, 00:29
Hi, ...

Let's see:

sound speed @ 20°C ( SQR ( 1.4*287*(273+20)) = ~ 340 m/s

say your min distance will be ~ 10 cm ... time will be 2x.1/340 ~ 600 µS

RCTime @ 20 Mhz gives 2µS granularity ... for a 16 bits value

so, the range will be 10 cm to 10cm* 65535/300 = 21.8 meters.

Looks fine ...

Alain

Thank you for your help. However, I am a little confused by your calculations. Could you explain them a little more for me? I understand your calculation for speed of sound as that is what I am already using. It's from there on that I am confused.

Acetronics2
- 30th December 2008, 19:45
Hi,

as your Tx and Rx will be on the same side, both facing the object to be detected ...

sound will have a travel equal to ... Twice the distance you look for.

As simple as that !

2 x 10 cm / 340 m/s = ... 0.588 ms = ~ 600 µs

10 cm mini ??? haha ... I let you discover why ... and why to cancel detections under these values ...

Alain

robert.quaas
- 30th December 2008, 19:48
Got some pictures of your ROBOT so we can see please ?

Happy New Year 2009

Robert.Quaas

mcbeasleyjr
- 31st December 2008, 01:50
First of all I want to thank you all for your help.... I believe I have figured the RCTIME command out well enough that I have a fairly accurate distance measurement.

Here is the section of my code. The only problem that I have is the fact that you can't use floats in Microcode studio, or if you can I don't know how. So some parts of my calculations may seem awkward but it will work right once finished.

CHECK_RANGE:
FOR B = 1 TO 50
SPEED_OF_SOUND = 1126 'Speed of sound at 68 degrees F is 1125.79ft/s
DEFINE PULSIN_MAX 4000 'Limits the count to an equivalent of about 8ms
HIGH PORTB.0 'Used to drive a 555 timer which generates 40khz signal
RCTIME PORTB.1, 1, RETURN_TIME 'How long before pin changes logic?
RETURN_TIME = RETURN_TIME * 2 'Calculates total microseconds
SPEED_OF_SOUND = SPEED_OF_SOUND * 12
DISTANCE = (SPEED_OF_SOUND * (RETURN_TIME / 1000 / 1000)) / 2
LCDOUT $FE, 1
LCDOUT $FE, 2
LCDOUT $FE, $0C
LCDOUT "DISTANCE IS ", DISTANCE, "IN" '2 x 16 LCD Display by the way
PAUSE 500
NEXT B
RETURN

Now, by my calculations, using a 20MHz clock makes the resolution of the RCTIME command 2us. So by saying (return_time * 2), this multiplies the count returned by 2 us. At max range it would be 4000 * 2 or 8000us. Then, I want to measure the distance in inches so I multiply the speed of sound variable by 12 to convert to inches. Then for the distance calculation, and this is where my problem is. I cannot store a float in a variable, instead the result of the calculation will be stored with everything after the decimal cut off. But I can deal with that for now. I would like to know if it is possible to get the right side of the decimal stored into a variable though. So, at max range of 4000 counts, (13512 inches * (8000us/1000/1000 'to convert to seconds')) / 2 'cut the amount of time in half' = 54.048 inches to target. However, only 54 will be stored in the distance variable at the current time. So my range I think would show 0 inches to 54 inches. I don't intend to let the robot get close enough to return a number as low as 0 though. As for pictures, I will post some as soon as I can. Again, thank you all for your help and feel free to double check my work above.