//PING Sonar //equations were taken from here and not independently verified: //http://www.societyofrobots.com/member_tutorials/node/174 //http://www.societyofrobots.com/robotforum/index.php?topic=5123.msg40008 //http://www.societyofrobots.com/robotforum/index.php?topic=4656.30 //uses timer0 int sonar_Ping() { #define PINGPIN 0 // assign a pin to the Ping Sensor #undef DDR #undef PIN #define DDR DDRC #define PORT PORTC #define PIN PINC PORT_ON(DDR, PINGPIN); // Switch PingPin to OUPUT // ------Trigger Pulse-------------- PORT_OFF(PORT, PINGPIN); // Bring PingPin low before starting trigger pulse delay_us(2); // Wait for 2 microseconds PORT_ON(PORT, PINGPIN); // Bring PingPin High for 5us according to spec sheet. delay_us(5); // Wait for 5 microseconds PORT_OFF(PORT, PINGPIN);; // Bring PingPin Low and standby //--------End Trigger Pulse--------------------- FLIP_PORT(DDR, PINGPIN); // Switch PingPin to INPUT loop_until_bit_is_set(PIN, PINGPIN); // Loop until the the PingPin goes high (macro found in sfr_def.h) //clears timer, reset overflow counter reset_timer0(); //reset timer 0 loop_until_bit_is_clear(PIN, 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 return (get_timer0_overflow()*255+TCNT0) * 2.068965517;//elapsed time x conversion }
void loop_delay_init() { T0IF = 0; T0IE = 1; reset_timer0(); }
int main(void) { /****************INITIALIZATIONS*******************/ //other stuff Im experimenting with for SoR uartInit(); // initialize the UART (serial port) uartSetBaudRate(0, 9600); // set UARTE speed, for Bluetooth uartSetBaudRate(1, 115200); // set UARTD speed, for USB connection, up to 500k, try 115200 if it uartSetBaudRate(2, 57600); // set UARTH speed uartSetBaudRate(3, 57600); // set UARTJ speed, for Blackfin //G=Ground, T=Tx (connect to external Rx), R=Rx (connect to external Tx) // initialize rprintf system and configure uart1 (USB) for rprintf rprintfInit(uart1SendByte); configure_ports(); // configure which ports are analog, digital, etc. LED_on(); rprintf("\r\nSystem Warming Up"); // initialize the timer system (comment out ones you don't want) init_timer0(TIMER_CLK_1024); init_timer1(TIMER_CLK_64); init_timer2(TIMER2_CLK_64); init_timer3(TIMER_CLK_64); init_timer4(TIMER_CLK_64); init_timer5(TIMER_CLK_1024); //timer5Init(); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage int i = 0, j = 0; //let system stabelize for X time for(i=0;i<16;i++) { j=a2dConvert8bit(i);//read each ADC once to get it working accurately delay_cycles(5000); //keep LED on long enough to see Axon reseting rprintf("."); } LED_off(); rprintf("Initialization Complete \r\n"); reset_timer0(); reset_timer1(); reset_timer2(); reset_timer3(); reset_timer4(); reset_timer5(); while(1) { control(); delay_cycles(100); //an optional small delay to prevent crazy oscillations } return 0; }
void prvSetupHardware(){ int i, j; //add 1.7s delay for potential power issues delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); delay_cycles(65535); uartInit(); // initialize the UART (serial port) uartSetBaudRate(0, 38400); // set UARTE speed, for Bluetooth uartSetBaudRate(1, 115200); // set UARTD speed, for USB connection, up to 500k, try 115200 if it doesn't work uartSetBaudRate(2, 38400); // set UARTH speed uartSetBaudRate(3, 38400); // set UARTJ speed, for Blackfin //G=Ground, T=Tx (connect to external Rx), R=Rx (connect to external Tx) rprintfInit(uart1SendByte);// initialize rprintf system and configure uart1 (USB) for rprintf configure_ports(); // configure which ports are analog, digital, etc. LED_on(); //rprintf("\r\nSystem Warmed Up"); // initialize the timer system init_timer0(TIMER_CLK_1024); // init_timer1(TIMER_CLK_64); // Timer 1 is initialized by FreeRTOS init_timer2(TIMER2_CLK_64); init_timer3(TIMER_CLK_64); init_timer4(TIMER_CLK_64); init_timer5(TIMER_CLK_64); a2dInit(); // initialize analog to digital converter (ADC) a2dSetPrescaler(ADC_PRESCALE_DIV32); // configure ADC scaling a2dSetReference(ADC_REFERENCE_AVCC); // configure ADC reference voltage //let system stabelize for X time for(i=0;i<16;i++) { j=a2dConvert8bit(i);//read each ADC once to get it working accurately delay_cycles(5000); //keep LED on long enough to see Axon reseting rprintf("."); } LED_off(); rprintf("Initialization Complete \r\n"); //reset all timers to zero reset_timer0(); reset_timer1(); reset_timer2(); reset_timer3(); reset_timer4(); reset_timer5(); /********PWM Setup***********/ prvPWMSetup(); }