gramo
New Member
Hell no! The ADC is far too slow! There are much faster solutions...
You will need some sort of amp and filter circuit on the receiver, so the moment it get a signal, it will go low, or go high (depends on the circuit) - but this is crucial for generating accurate samples!
Now that's out of the way, creating a 40Khz signal, and waiting for the response is not hard at all with a little thought, Consider this;
Written in Proton PIC Basic
I wrote that based on the Project from the Proton User site found **broken link removed**.
Within that file is a great receiver circuit, and a gold mine of ultrasonic fundamentals
You will need some sort of amp and filter circuit on the receiver, so the moment it get a signal, it will go low, or go high (depends on the circuit) - but this is crucial for generating accurate samples!
Now that's out of the way, creating a 40Khz signal, and waiting for the response is not hard at all with a little thought, Consider this;
Code:
Device = 16F876
XTAL = 20
Symbol Distance_Per_Instruction = (1 / (XTAL / 4)) * 340.29 ' This is the distance per instruction in micro meters (1 instruction = 68.058uM @ 20Mhz XTAL)
Symbol Timer1 = TMR1L.WORD ' A special way of addressing both TMR1L and TMR1H with one register.
Symbol TMR1_Overflow = PIR1.0 ' TMR1 overflow flag.
Symbol TMR1_Enabled = T1CON.0 ' Enables TMR1 to start incrementing.
Symbol Input_Pin = PORTA.0 ' Defines the receivers Pin.
Dim Loops As Byte
Dim Insructions_Taken As Float
Dim Distance As Float
ALL_DIGITAL = True ' Make all pins digital I/O's.
Low PORTB.0 ' Make both outputs to the transmitter
Low PORTB.1 ' outputs, and set them low.
Input Input_Pin ' Make the input pin an input.
TMR1_Enabled = 0
T1CON.1 = 0 ' 0 = TMR1 Increments on Internal clock.
T1CON.4 = 0 ' TMR1 Prescale Bits, 00 = 1:1.
T1CON.5 = 0 '
Main_Loop:
Timer1 = 0 ' Reset TMR1.
Insructions_Taken = 0 ' Reset the Cycles_Taken register.
Gosub Create_Ping ' Create a 40Khz Signal.
TMR1_Enabled = 1 ' Allow TMR1 to begin incrementing.
Repeat
If Input_Pin = 0 Then ' Capture the input going Low (echo has been recieved).
TMR1_Enabled = 0 ' Disable TMR1.
Insructions_Taken = Timer1 ' Read the current contents of TMR1.
Break ' Leave the Repeat Until loop.
EndIf
Until TMR1_Overflow = 1 ' If TMR1 rolls over from 65535 to 0, then exit
TMR1_Enabled = 0
If Insructions_Taken = 0 Then ' Check if Insructions_Taken has been updated.
Print At 1, 1, "Out Of Range " ' If not then a time out occurred - echo is out of range.
Else
Distance = Insructions_Taken * Distance_Per_Instruction / 1000 ' Calculate the distance in micro meters, then scale it to mm.
Print At 1, 1, "Dist = ", DEC1 Distance, " mm " ' Display on the LCD.
EndIf
DelaymS 100 ' 100mS between checks
Goto Main_Loop
Create_Ping:
Loops = 8 ' Number of 40Khz cycles to produce.
Repeat
PortB = %00000001 ' First half of cycle.
DelayuS 10 ' Delay of 10uS.
PortB = %00000010 ' Second half of cycle.
DelayuS 10 ' Delay of 10uS.
Dec Loops ' That one whole cycle, dec the counter.
Until Loops = 0 ' And repeat until its finished all cycles.
Return
Written in Proton PIC Basic
I wrote that based on the Project from the Proton User site found **broken link removed**.
Within that file is a great receiver circuit, and a gold mine of ultrasonic fundamentals
Last edited: