Buy an Axon, Axon II, or Axon Mote and build a great robot, while helping to support SoR.
0 Members and 1 Guest are viewing this topic.
' -----[ Revision History ]------------------------------------------------' -----[ I/O Definitions ]-------------------------------------------------Ping PIN 15' -----[ Constants ]-------------------------------------------------------#SELECT $STAMP #CASE BS2, BS2E Trigger CON 5 ' trigger pulse = 10 uS Scale CON $200 ' raw x 2.00 = uS #CASE BS2SX, BS2P, BS2PX Trigger CON 13 Scale CON $0CD ' raw x 0.80 = uS #CASE BS2PE Trigger CON 5 Scale CON $1E1 ' raw x 1.88 = uS#ENDSELECTRawToIn CON 889 ' 1 / 73.746 (with **)RawToCm CON 2257 ' 1 / 29.034 (with **)IsHigh CON 1 ' for PULSOUTIsLow CON 0' -----[ Variables ]-------------------------------------------------------rawDist VAR Word ' raw measurementinches VAR Wordcm VAR Word' -----[ EEPROM Data ]-----------------------------------------------------' -----[ Initialization ]--------------------------------------------------Reset: DEBUG CLS, ' setup report screen "Parallax Ping Sonar ", CR, "=====================", CR, CR, "Time (uS)..... ", CR, "Inches........ ", CR, "Centimeters... "' -----[ Program Code ]----------------------------------------------------Main: DO GOSUB Get_Sonar ' get sensor value inches = rawDist ** RawToIn ' convert to inches cm = rawDist ** RawToCm ' convert to centimeters DEBUG CRSRXY, 15, 3, ' update report screen DEC rawDist, CLREOL, CRSRXY, 15, 4, DEC inches, CLREOL, CRSRXY, 15, 5, DEC cm, CLREOL PAUSE 100 LOOP END' -----[ Subroutines ]-----------------------------------------------------' This subroutine triggers the Ping sonar sensor and measures' the echo pulse. The raw value from the sensor is converted to' microseconds based on the Stamp module in use. This value is' divided by two to remove the return trip -- the result value is' the distance from the sensor to the target in microseconds.Get_Sonar: Ping = IsLow ' make trigger 0-1-0 PULSOUT Ping, Trigger ' activate sensor PULSIN Ping, IsHigh, rawDist ' measure echo pulse rawDist = rawDist */ Scale ' convert to uS rawDist = rawDist / 2 ' remove return trip RETURN
' {$STAMP BS2}' {$PBASIC 2.5}' Conversion constants for room temperature measurements.CmConstant CON 2260InConstant CON 890cmDistance VAR WordinDistance VAR Wordtime VAR WordDO PULSOUT 15, 5 PULSIN 15, 1, time cmDistance = cmConstant ** time inDistance = inConstant ** time DEBUG HOME, DEC3 cmDistance, " cm" DEBUG CR, DEC3 inDistance, " in" PAUSE 100LOOP
' -----[ Program Description ]---------------------------------------------' This programs demonstrates roaming with the Parallax PING))) Mounting' Bracket, which allows you to pan-scan with your Parallax PING))) sensor.'' This program assumes you have already set your BOE-Bot up for roaming' with IR sensors. You must also have the PING))) Mounting bracket' installed as per the instructions, with the servo connected to P14 and' the PING))) sensor connected to P15.'' Due to the way the PING))) bracket mounts you may have to trim your IR' detectors down so that they don't stick up in the way of the moving' PING))) bracket as it rotates. It's best to angle them off at 45' degree angles so that the BOE-Bot doesn't run into anything while' moving along a wall at a narrow angle.'' The BOE-Bot will now roam around and when it senses an object, it will' scan within a 180 degree field of view for the clearest path, which it' will then navigate scanning for an object again. The IR Sensors prevent' hitting a wall when coming at it from a narrow angle where the PING)))' might have trouble seeing it due to the lack of reflection of sound.'' Calibration is important to this code working properly. If you do not' have the correct values for your servos then you may get strange results' from your BOE-Bot. The necessary calibration information is listed in' each section of the code where it is required.' -----[ Revision History ]------------------------------------------------' This code is basically a modified version of the Roaming With IR code' from the Robotics With The BOE-Bot Manual, written by Andy Lindsay.' Much of the original code was left untouched so you can see how it was' altered.' -----[ I/O Definitions ]-------------------------------------------------Piezo PIN 4 ' Piezo SpeakerRightServo PIN 12 ' Right ServoLeftServo PIN 13 ' Left ServoPingServo PIN 14 ' PING))) ServoPing PIN 15 ' PING))) Sensor' -----[ Variables ]-------------------------------------------------------irDetectLeft VAR Bit ' Variable For Left IR InputirDetectRight VAR Bit ' Variable For Right IR InputpulseCount VAR Byte ' Used For Measuring Turnsdistance VAR Word ' Current Distance Of ObjectoldDistance VAR Word ' Old Distance Valuecounter VAR Word ' PING))) Cycle Countertask VAR Nib ' Current Task' -----[ Initialization ]--------------------------------------------------FREQOUT Piezo, 2000, 3000 ' Signal Program Start/Reset' -----[ Program Code ]----------------------------------------------------Main:DO FREQOUT 8, 1, 38500 ' Emit 38.5 kHz IR To Left irDetectLeft = IN9 ' Store IR Detection Values FREQOUT 2, 1, 38500 ' Emit 38.5 kHz IR To Right irDetectRight = IN0 ' Store IR Detection Values IF (irDetectLeft = 0) AND (irDetectRight = 0) THEN GOSUB Ping_Around ' Object Detected via IR Forward ELSEIF (irDetectLeft = 0) THEN GOSUB Ping_Around ' Object Detected via IR Left ELSEIF (irDetectRight = 0) THEN GOSUB Ping_Around ' Object Detected via IR Right ENDIF counter = counter + 1 ' Increment Passive Counter IF counter > 10 THEN ' Wait For 10 Servo Pulses GOSUB Ping_Out ' Activate PING))) ENDIF IF (distance > 30) THEN ' Is Object Farther Than 30 cm? GOSUB Forward_Pulse ' If Yes Go Forward ELSE GOSUB Ping_Around ' Otherwise Scan For Clear Path ENDIFLOOP' -----[ Subroutines ]-----------------------------------------------------' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT MOVE FORWARD *' * WHILE THE PING))) IS FACING FORWARD. *' *************************************************************************Forward_Pulse: ' Send A Single Forward Pulse PULSOUT PingServo, 750 ' Ping Servo Forward Pulse Value PULSOUT LeftServo, 850 ' Left Servo Forward Pulse Value PULSOUT RightServo, 650 ' Right Servo Forward Pulse Value PAUSE 20 ' Refresh Delay RETURN' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT TURN LEFT 90 *' * DEGREES. USE THE SAME VALUE AS ABOVE FOR THE PING))) BRACKET SERVO. *' *************************************************************************Turn_Left: ' Left Turn, About 45 Degrees FOR pulseCount = 0 TO 7 ' Number Of Pulses To Turn PULSOUT PingServo, 750 ' Ping Servo Forward Pulse Value PULSOUT LeftServo, 650 ' Left Servo Left Pulse Value PULSOUT RightServo, 650 ' Right Servo Left Pulse Value PAUSE 20 ' Refresh Delay NEXT RETURN' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT TURN RIGHT 90 *' * DEGREES. USE THE SAME VALUE AS ABOVE FOR THE PING))) BRACKET SERVO. *' *************************************************************************Turn_Right: ' Right Turn, About 45 Degrees FOR pulseCount = 0 TO 7 ' Number Of Pulses To Turn PULSOUT PingServo, 750 ' Ping Servo Forward Pulse Value PULSOUT LeftServo, 850 ' Left Servo Right Pulse Value PULSOUT RightServo, 850 ' Right Servo Right Pulse Value PAUSE 20 ' Refresh Delay NEXT RETURN' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUES TO MAKE YOUR BOE-BOT MOVE BACKWARD *' * WHILE THE PING))) IS FACING FORWARD. *' *************************************************************************Back_Up: ' Back Up FOR pulseCount = 0 TO 40 ' Number Of Pulses To Backup PULSOUT PingServo, 750 ' Ping Servo Forward Pulse Value PULSOUT LeftServo, 650 ' Left Servo Backup Pulse Value PULSOUT RightServo, 850 ' Right Servo Backup Pulse Value PAUSE 20 ' Refresh Delay NEXT RETURNPing_Out: ' PING))) counter = 0 ' Reset Passive Delay Counter LOW Ping ' Force PING))) Line Low PULSOUT Ping, 5 ' Activate PING))) Pulse PULSIN Ping, 1, distance ' Receive Return Pulse distance = distance ** 2257 ' Calculate Distance RETURNPing_Around: ' Start 180 Degree Pan-Scan counter = 0 ' Reset Passive Delay Counter oldDistance = 30 ' Current Old Distance Values task = 0 ' Current Task Priority' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING))) *' * TURN 90 DEGREES LEFT. *' ************************************************************************* FOR pulseCount = 0 TO 20 ' Number Of Pulses To Spin LOW Ping ' Force PING))) Line Low PULSOUT PingServo, 1085 ' Ping Servo 90 Left Pulse Value PULSOUT Ping, 5 ' Activate PING))) PULSIN Ping, 1, distance ' Receive Distance Value PAUSE 20 ' Refresh Delay NEXT distance = distance ** 2257 ' Calculate Distance In cm IF distance > oldDistance THEN ' Is distance > Last Clear Path oldDistance = distance ' Update oldDistance Value task = 1 ENDIF' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING))) *' * TURN 45 DEGREES LEFT. *' ************************************************************************* FOR pulseCount = 0 TO 20 ' Number Of Pulses To Spin LOW Ping ' Force PING))) Line Low PULSOUT PingServo, 850 ' Ping Servo 45 Left Pulse Value PULSOUT Ping, 5 ' Activate PING))) PULSIN Ping, 1, distance ' Receive Distance Value PAUSE 20 ' Refresh Delay NEXT distance = distance ** 2257 ' Calculate Distance In cm IF distance > oldDistance THEN ' Is distance > Last Clear Path oldDistance = distance ' Update oldDistance Value task = 2 ENDIF' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING))) *' * TURN 45 DEGREES RIGHT. *' ************************************************************************* FOR pulseCount = 0 TO 20 ' Number Of Pulses To Spin LOW Ping ' Force PING))) Line Low PULSOUT PingServo, 400 ' Ping Servo 45 Right Pulse Value PULSOUT Ping, 5 ' Activate PING))) PULSIN Ping, 1, distance ' Receive Distance Value PAUSE 20 ' Refresh Delay NEXT distance = distance ** 2257 ' Calculate Distance In cm IF distance > oldDistance THEN ' Is distance > Last Clear Path oldDistance = distance ' Update oldDistance Value task = 3 ENDIF' *************************************************************************' * USE THE APPROPRIATE PULSOUT VALUE TO MAKE YOUR PING))) *' * TURN 90 DEGREES RIGHT. *' ************************************************************************* FOR pulseCount = 0 TO 20 ' Number Of Pulses To Spin LOW Ping ' Force PING))) Line Low PULSOUT PingServo, 225 ' Ping Servo 90 Right Pulse Value PULSOUT Ping, 5 ' Activate PING))) PULSIN Ping, 1, distance ' Receive Distance Value PAUSE 20 ' Refresh Delay NEXT distance = distance ** 2257 ' Calculate Distance In cm IF distance > oldDistance THEN ' Is distance > Last Clear Path oldDistance = distance ' Update oldDistance Value task = 4 ENDIF ON task GOSUB Task0, Task1, Task2, Task3, Task4 distance = 50 ' Prevent Scan From Looping RETURNTask0: ' Forward Was Clearest Path GOSUB Turn_Right ' This Could Mean Narrow Path GOSUB Turn_Right ' So We'll Turn Around GOSUB Turn_Right ' You Can Change The Behavior GOSUB Turn_Right ' Of Any Of The Tasks RETURNTask1: ' 90 Degrees Left Was Clearest GOSUB Turn_Left GOSUB Turn_Left RETURNTask2: ' 45 Degrees Left Was Clearest GOSUB Turn_Left RETURNTask3: ' 45 Degrees Right Was Clearest GOSUB Turn_Right RETURNTask4: ' 90 Degrees Right Was Clearest GOSUB Turn_Right GOSUB Turn_Right RETURN
PULSOUT Ping, Trigger ' activate sensorPULSIN Ping, IsHigh, rawDist ' measure echo pulse
I think that is basically saying, talk to it like a servo, then wait, and then listen to it like a sensor.
(set timer 0 to 0 hereset portd.o to output here)portd.0 = 0; //set portd.0 lowportd.0 = 1; //set portd.0 highportd.0 = 0; //set portd.0 low again. This triggers the pingset portd.0 to input hereenable timer0 herewait 200ms (this is how long it takes for the sensor to send its ping. Remember for an accurate reading to account for changeing the prot state and enabling the timer)while(portd.0 == 1){ //while the port is pulled high by the ping//do nothing}ping reading = timer0; the ping sensor has received an echo and pulled the line back low//now you just have to do the math
#include <avr/io.h>#include <util/delay.h>#include <stdio.h>#include "hardware.h"void init_hardware (void);void USART_Transmit( unsigned char data );uint16_t a2d_10( uint8_t Channel );uint8_t a2d_8( uint8_t Channel );uint16_t ultra_sonic (void);void init_hardware (void){ PORTA=0; DDRA=0; PORTB=YELLOW_LED_MASK; DDRB=YELLOW_LED_MASK; PORTC=0; DDRC=0; PORTD=0; DDRD=0; PORTE=0; DDRE=MOTOR_MASK|SERVO_WHEEL_MASK|SERVO_IR_MASK; PORTF=0; DDRF=0; PORTG=BLUE_LED_MASK|RED_LED_MASK; DDRG=BLUE_LED_MASK|RED_LED_MASK; TCCR3A=0b10101010; TCCR3B=0b00011010; ICR3=40000; OCR3A=3000; OCR3B=3000; OCR3C=3000; TCCR1A=0; TCCR1B=2; ADCSR = (( 1 << ADEN ) | ( 1 << ADSC ) | ADC_PRESCALAR_128 ); ADMUX = (( 0 << REFS1 ) | ( 1 << REFS0 )); if (((( 1 << ADEN ) | ( 1 << ADSC ) | ADC_PRESCALAR_128 ) & ADCSR ) != 0 ) { // Wait for the initial conversion to complete. This initializes // the ADC. while (ADCSR & ( 1 << ADSC) ) ; } UBRR0H = UBRR0_INIT >> 8; UBRR0L = UBRR0_INIT & 0xFF; UCSR0A = UCSR0A_INIT; UCSR0B = UCSR0B_INIT; UCSR0C = UCSR0C_INIT;}void USART_Transmit( unsigned char data ){/* Wait for empty transmit buffer */while ( !( UCSR0A & (1<<UDRE0)) );/* Put data into buffer, sends the data */UDR0 = data;}uint16_t a2d_10( uint8_t Channel ){ // Select the channel in a manner which leaves REFS0 and REFS1 un touched. ADMUX = ( ADMUX & (( 1 << REFS1 ) | ( 1 << REFS0 ))) | Channel; // Start the conversion ADCSR = ADCSR | ( 1 << ADSC ); // Wait for it to complete while ( ADCSR & ( 1 << ADSC )); return ADC;} // a2d_10uint8_t a2d_8( uint8_t Channel ){ // Select the channel in a manner which leaves REFS0 and REFS1 un touched. ADMUX = ( ADMUX & (( 1 << REFS1 ) | ( 1 << REFS0 ))) | ( 1 << ADLAR ) | Channel; // Start the conversion ADCSR = ADCSR | ( 1 << ADSC ); // Wait for it to complete while ( ADCSR & ( 1 << ADSC )); // We only need the top 8 bits (left-adjusted) return ADCH;} // a2d _8uint16_t ultra_sonic (void){ uint16_t fcap=0,scap=0; ULTRA_SONIC_OUT; ULTRA_SONIC_SET; _delay_us (10); ULTRA_SONIC_RESET; ULTRA_SONIC_IN; Clear_ICF1; TCNT1=0; ULTRA_SONIC_CAP_RISING; while (ICF1_Empty);//wait for capture Clear_ICF1; fcap=ICR1; ULTRA_SONIC_CAP_FALLING; while (ICF1_Empty);//wait for capture Clear_ICF1; scap=ICR1; return scap-fcap;}int main (void){ uint16_t ac=0; char txt[]=" "; init_hardware(); _delay_ms(100); while (1) { ac=ultra_sonic(); sprintf(txt,"%u ",ac); USART_Transmit(txt[0]); USART_Transmit(txt[1]); USART_Transmit(txt[2]); USART_Transmit(txt[3]); USART_Transmit(txt[4]); USART_Transmit('\n'); USART_Transmit('\r'); _delay_ms(500); }}
#define RED_LED_PIN 4#define RED_LED_MASK ( 1 << RED_LED_PIN )#define RED_LED_DDR DDRG#define RED_LED_PORT PORTG#define BLUE_LED_PIN 3#define BLUE_LED_MASK ( 1 << BLUE_LED_PIN )#define BLUE_LED_DDR DDRG#define BLUE_LED_PORT PORTG#define YELLOW_LED_PIN 4#define YELLOW_LED_MASK ( 1 << YELLOW_LED_PIN )#define YELLOW_LED_DDR DDRB#define YELLOW_LED_PORT PORTB#define LED_ON( color ) color ## _LED_PORT &= ~color ## _LED_MASK;#define LED_OFF( color ) color ## _LED_PORT |= color ## _LED_MASK;#define MOTOR_MASK ( 1 << 3)#define SERVO_WHEEL_MASK ( 1 << 4)#define SERVO_IR_MASK ( 1 << 5)#define ULTRA_SONIC_MASK (1<<4)#define ULTRA_SONIC_OUT (DDRD|=ULTRA_SONIC_MASK)#define ULTRA_SONIC_IN (DDRD&=~ULTRA_SONIC_MASK)#define ULTRA_SONIC_SET (PORTD|=ULTRA_SONIC_MASK)#define ULTRA_SONIC_RESET (PORTD&=~ULTRA_SONIC_MASK)#define ULTRA_SONIC_CAP_RISING (TCCR1B|=(1<<ICES1))#define ULTRA_SONIC_CAP_FALLING (TCCR1B&=~(1<<ICES1))#define Clear_ICF1 TIFR|=(1<<5)#define ICF1_Empty !(TIFR & ( 1 << 5 ))#define ADC_PRESCALAR_2 (( 0 << ADPS2 ) | ( 0 << ADPS1 ) | ( 1 << ADPS0 ))#define ADC_PRESCALAR_4 (( 0 << ADPS2 ) | ( 1 << ADPS1 ) | ( 0 << ADPS0 ))#define ADC_PRESCALAR_8 (( 0 << ADPS2 ) | ( 1 << ADPS1 ) | ( 1 << ADPS0 ))#define ADC_PRESCALAR_16 (( 1 << ADPS2 ) | ( 0 << ADPS1 ) | ( 0 << ADPS0 ))#define ADC_PRESCALAR_32 (( 1 << ADPS2 ) | ( 0 << ADPS1 ) | ( 1 << ADPS0 ))#define ADC_PRESCALAR_64 (( 1 << ADPS2 ) | ( 1 << ADPS1 ) | ( 0 << ADPS0 ))#define ADC_PRESCALAR_128 (( 1 << ADPS2 ) | ( 1 << ADPS1 ) | ( 1 << ADPS0 ))#define UART0_BAUD_RATE 9600#define UART1_BAUD_RATE 38400#define UART_DATA_BIT_8 (( 1 << UCSZ1 ) | ( 1 << UCSZ0 ))#define UART_PARITY_NONE (( 0 << UPM1 ) | ( 0 << UPM0 ))#define UART_STOP_BIT_1 ( 1 << USBS )#define UBRR0_INIT (( 1000000 / UART0_BAUD_RATE ) - 1 )#define UBRR1_INIT (( 1000000 / UART1_BAUD_RATE ) - 1 )#define UCSR0A_INIT 0#define UCSR0B_INIT (( 1 << RXEN ) | ( 1 << TXEN ))#define UCSR0C_INIT ( UART_DATA_BIT_8 | UART_PARITY_NONE | UART_STOP_BIT_1 )#define UCSR1A_INIT 0#define UCSR1B_INIT (( 1 << RXEN ) | ( 1 << TXEN ))#define UCSR1C_INIT ( UART_DATA_BIT_8 | UART_PARITY_NONE | UART_STOP_BIT_1 )
#define PORT_ON( port_letter, number ) port_letter |= (1<<number) // ultrasonic out, ultrasonic set#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number) // ultrasonic in, ultrasonic reset#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )PORT_ON(DDRC, PingPin); // Set Pin to OUTPUT//----Trigger Pulse----------PORT_ON(PORTC, PingPin); delay_us(10);PORT_OFF(PORTC, PingPin);//-----End Trigger Pulse-----PORT_OFF(DDRC, PingPin); // Set Pin to INPUT
#define ULTRA_SONIC_CAP_RISING (TCCR1B|=(1<<ICES1))#define Clear_ICF1 TIFR|=(1<<5)#define ICF1_Empty !(TIFR & ( 1 << 5 )) Clear_ICF1; TCNT1=0; ULTRA_SONIC_CAP_RISING; while (ICF1_Empty);//wait for capture Clear_ICF1; fcap=ICR1; ULTRA_SONIC_CAP_FALLING; while (ICF1_Empty);//wait for capture Clear_ICF1; scap=ICR1; return scap-fcap;
uint16_t ultra_sonic (void){ uint16_t fcap=0,scap=0;//fcap stores first capture, scap stores second ULTRA_SONIC_OUT;//set pin to output ULTRA_SONIC_SET;//set pin to high _delay_us (10);//delay for a pulse ULTRA_SONIC_RESET;//set pin low ULTRA_SONIC_IN;//set pin to input Clear_ICF1;//clear the capture flag TCNT1=0;//set timer 1 to 0 to prevent overflow ULTRA_SONIC_CAP_RISING;//capture the rising edge of the pulse while (ICF1_Empty);//wait for capture Clear_ICF1;//clear the capture flag fcap=ICR1;//save the captured value ULTRA_SONIC_CAP_FALLING;//capture the falling edge of the pulse while (ICF1_Empty);//wait for capture Clear_ICF1;//clear the capture flag scap=ICR1;//save the captured value return scap-fcap;//subtract the timer values to find the pulse lenght}
TCCR1A=0;TCCR1B=2;
void ping(void) { int PingPin = 0x02;// assign a pin to the Ping Sensor int PingVal = 0;// initialize and assign Ping Sensor Reading ValuePORT_ON(DDRC, PingPin); // Set Pin to OUTPUT//----Trigger Pulse----------PORT_ON(PORTC, PingPin); delay_us(10);PORT_OFF(PORTC, PingPin);//-----End Trigger Pulse-----PORT_OFF(DDRC, PingPin); // Set Pin to INPUT //PORT_ON(DDRC, 1);// Switch signalpin to output // ------Trigger Pulse-------------- //PORT_OFF(PORTC, 1); // Send low pulse, 0=low, 1=high //delay_us(2); // Wait for 2 microseconds //PORT_ON(PORTC, 1); // Send high pulse, 0=low, 1=high //delay_us(5); // Wait for 5 microseconds //PORT_OFF(PORTC, 1);; // Holdoff low //--------End Trigger Pulse--------------------- //FLIP_PORT(DDRC, 1); // Switch signalpin to input //clears timer, reset overflow counter reset_timer_0();//reset timer 0 delay_us(200); // wait for burst frequency to end. PINC = PINC | PingPin; while(PINC == 1) { // wait until pin goes low or times out //asm volatile ("NOP"); nop(); // to prevent an optimized compiler from removing the loop } //read timer0's overflow counter //255 is count before overflow, dependent on clock int elapsed_time=timer0GetOverflowCount()*255+TCNT0; PingVal = elapsed_time/93; rprintf("Ping: %d", PingVal ); delay_us(200); // delay before next measurment } // end ping function
#define ULTRA_SONIC_MASK (1<<4)#define ULTRA_SONIC_OUT (DDRD|=ULTRA_SONIC_MASK)#define ULTRA_SONIC_IN (DDRD&=~ULTRA_SONIC_MASK)#define ULTRA_SONIC_SET (PORTD|=ULTRA_SONIC_MASK)#define ULTRA_SONIC_RESET (PORTD&=~ULTRA_SONIC_MASK)#define ULTRA_SONIC_IS_LOW (!(PIND&ULTRA_SONIC_MASK))#define ULTRA_SONIC_IS_HIGH ((PIND&ULTRA_SONIC_MASK)==ULTRA_SONIC_MASK)uint16_t ultra_sonic (void){ uint16_t fcap=0,scap=0; ULTRA_SONIC_OUT; ULTRA_SONIC_SET; _delay_ms (1); ULTRA_SONIC_RESET; ULTRA_SONIC_IN; TCNT1=0; while(ULTRA_SONIC_IS_LOW); fcap=TCNT1; while(ULTRA_SONIC_IS_HIGH); scap=TCNT1; return scap-fcap;}
while (is_set)++pulse_time;
int PingPin = 1; // assign a pin to the Ping Sensorint PingVal = 0; // initialize and assign Ping Sensor Reading ValuePORT_ON(DDRC, PingPin); // Switch PingPin to OUPUT// ------Trigger Pulse--------------PORT_OFF(PORTC, PingPin); // Bring PingPin low before starting trigger pulsedelay_us(2); // Wait for 2 microsecondsPORT_ON(PORTC, PingPin); // Bring PingPin High for 5us according to spec sheet.delay_us(5); // Wait for 5 microsecondsPORT_OFF(PORTC, PingPin);; // Bring PingPin Low and standby//--------End Trigger Pulse---------------------FLIP_PORT(DDRC, PingPin); // Switch PingPin to INPUTloop_until_bit_is_set(PINC, PingPin); // Loop until the the PingPin goes high (macro found in sfr_def.h)//clears timer, reset overflow counterreset_timer_0(); //reset timer 0loop_until_bit_is_clear(PINC, 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 clockint elapsed_time=timer0GetOverflowCount()*255+TCNT0;// The PING))) returns a pulse width of 29.033 uS per centimeterPingVal = elapsed_time;
elapsed_time*= 2.068965517;