I am trying to get the ping sensor to work with the axon using my own code because the official ping code is still in beta. I want to read the pulse from the sensor with a arduino-style pulsein() function and then convert that into range data. while writing my code i found that the actual timer functions in the source code differ from what is in the documentation on the axon web page. I do not think that my digital input or output is working because the activity light on the sensor never blinks. So there are a number of things that could be wrong with my code, but i was hoping someone could help me figure this out, because the only sensors readily available to me are ping ultrasonic sensors. here is my code:
#include "SoR_Utils.h"
#include "sensors.c"
#include "rprintf.h"
#include "uart4.h"
int pulsein(char port, int pin, int hl)
{
if(hl == 0)
{
if(port == 'A')
{
while(bit_is_set(PINA, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_clear(PINA, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'E')
{
while(bit_is_set(PINE, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_clear(PINE, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'H')
{
while(bit_is_set(PINH, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_clear(PINH, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'C')
{
while(bit_is_set(PINC, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_clear(PINC, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
}
else if(hl == 1)
{
if(port == 'A')
{
while(bit_is_clear(PINA, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_set(PINA, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'E')
{
while(bit_is_clear(PINE, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_set(PINE, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'H')
{
while(bit_is_clear(PINH, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_set(PINH, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
else if(port == 'C')
{
while(bit_is_clear(PINC, pin))
{
}
init_timer1(TIMER_CLK_1);
while(bit_is_set(PINC, pin))
{
}
int pulse = get_timer1_overflow()*255+TCNT1;
reset_timer1();
return pulse;
}
}
}
//only attach ping to pin 5
int readpingcm(char port)
{
long duration;
long cm;
if(port == 'A')
{
//output
DDRA = _BV(PA5);
PORT_OFF(PORTA, 5);
delay_us(2);
PORT_ON(PORTA, 5);
delay_us(5);
PORT_OFF(PORTA, 5);
//input
cbi(DDRA, PA5);
duration = pulsein('A', 5, 1);
cm = duration/29/2;
return cm;
}
else if(port == 'E')
{
//output
DDRE = _BV(PE5);
PORT_OFF(PORTE, 5);
delay_us(2);
PORT_ON(PORTE, 5);
delay_us(5);
PORT_OFF(PORTE, 5);
//input
cbi(DDRE, PE5);
duration = pulsein('A', 5, 1);
cm = duration/29/2;
return cm;
}
else if(port == 'H')
{
//output
DDRH = _BV(PH5);
PORT_OFF(PORTH, 5);
delay_us(2);
PORT_ON(PORTH, 5);
delay_us(5);
PORT_OFF(PORTH, 5);
//input
cbi(DDRH, PH5);
duration = pulsein('A', 5, 1);
cm = duration/29/2;
return cm;
}
else if(port == 'C')
{
//output
DDRC = _BV(PC5);
PORT_OFF(PORTC, 5);
delay_us(2);
PORT_ON(PORTC, 5);
delay_us(5);
PORT_OFF(PORTC, 5);
//input
cbi(DDRC, PC5);
duration = pulsein('A', 5, 1);
cm = duration/29/2;
return cm;
}
}
int main(void)
{
uartInit();
uartSetBaudRate(1, 115200);
//initialize analog to digital converter
a2dInit();
//configure ADC scaling
a2dSetPrescaler(ADC_PRESCALE_DIV32);
//configure ADC reference voltage
a2dSetReference(ADC_REFERENCE_AVCC);
while(1)
{
int range = readpingcm('A');
rprintfInit(uart1SendByte);
rprintf("%d",range);
}
return 0;
}
thanks for any help!