I was asked for code snippets so I figured I would post it here in case anyone else wants it too. Although I only made one small change to Pomprocker's code, I am including all the changes I made to get it working using the Axon code downloaded from the website.
Here is the Axon.c change, I only changed it because I didn't want to have to wait to push the button and I wanted to control the loop elsewhere:
/*********ADD YOUR CODE BELOW THIS LINE **********/
rprintf("Initialization Complete \r\n");
//wait until user pushes button
//while(!button_pressed());
reset_timer_0();
reset_timer_2();
control();
//while(1)
//{
//control();//use this for your code
//delay_cycles(100);//an optional small delay to prevent crazy oscillations
//}
/*********ADD YOUR CODE ABOVE THIS LINE **********/
Here is my function prototype and modification to call my function:
void PingSonar(void);
//enter your code in control here
void control(void)
{
PingSonar();
//photovore();//run photovore algorithm
//test_code();//various testing code for admin
}
Here is the meat of the code that does triggers and reads the Ping))):
int GetDistance(void)
{
int PingPin = 0; // assign a pin to the Ping Sensor
int PingVal = 0; // initialize and assign Ping Sensor Reading Value
PORT_ON(DDRF, PingPin); // Switch PingPin to OUPUT
// ------Trigger Pulse--------------
PORT_OFF(PORTF, PingPin); // Bring PingPin low before starting trigger pulse
delay_us(2); // Wait for 2 microseconds
PORT_ON(PORTF, PingPin); // Bring PingPin High for 5us according to spec sheet.
delay_us(5); // Wait for 5 microseconds
PORT_OFF(PORTF, PingPin);; // Bring PingPin Low and standby
//--------End Trigger Pulse---------------------
FLIP_PORT(DDRF, PingPin); // Switch PingPin to INPUT
loop_until_bit_is_set(PINF, PingPin); // Loop until the the PingPin goes high (macro found in sfr_def.h)
//clears timer, reset overflow counter
reset_timer_0(); //reset timer 0
loop_until_bit_is_clear(PINF, PingPin); // Loop until the the PingPin goes low (macro found in sfr_def.h)
//read timer0's overflow counter
//255 is count before overflow, dependent on clock
int elapsed_time=timer0GetOverflowCount()*255+TCNT0;
// The PING))) returns a pulse width of 29.033 uS per centimeter
PingVal = elapsed_time;
return PingVal;
}
Mark Brown