Saturday, October 07, 2006

October 7, 2006 Updates

Wow what progress! Slowly but surely I was able to get the SRF04 sonar module working. Not without a lot of lost time on false starts, dead batteries and general ignorance on my part. I have created some notes for you to consider if you are going down the same path.

* The SRF04 outputs a pulse whose width in us = the distance. To scale it to cm divide by 58 and to get inches divide by 148 per the instructions.
* You can use an external interrupt to measure this pulse by storing the timer count on the rising edge and then subtracting the second time on the falling edge.
* External INT0-3 do not allow “Any Edge” triggers (i.e. both rising and falling edges trigger the interrupt) 4-7 do.
* A 16 bit timer has fewer overflows and helps limit erroneous readings
* The timer counts you use are in CPU ticks not us. Depending on how you configure your prescaler a tick can represent .0625us up to 64us. Since your measurement is in us you will need to convert your ticks to us.
* The SRF04 has a 3cm minimum distance (100us) and measurements start at the element, not the PCB


So using Pascal’s AVRLib I chose to use Counter/Timer1 which he configures with a prescale of 64 (each tick = 4us with a 16MHz clock). I also chose to use INT4 which is on PORTE pin4 configured for both rising and falling edges. IN my ISR I check to see if it is the rising edge (PINE PE4 =1). If so, I capture the current tick count (SonarDistance = TCNT1). Otherwise, I set the SonarDistance = TCNT1 – SonarDistance because the TCNT1 is set to count down.

Now all I need to do is write a function that will ping the sonar X times and return the average.

Depending on how much more time I’ll have today, I’ll start on the encoders.

Onward,
Jay