اخى اولا انا علاقتى بالبيك بيزيك ضعيفة لكن وجدت هذا البرانامج جاهزا
مع العلم ان البرنامج مكتوب بالبروتون
وهذا هو الكود كاملا
كود:
'
Include "PROTON_4.INC" ' Use the PROTON Development board with a 4MHz xtal
Device = 16F871 ' Fake a smaller device for a small routine
WARNINGS = OFF ' Disable warning messages
Symbol ECHO = PORTB.0 ' Echo signals from comparator
Symbol TIMER1 = TMR1L.WORD ' Create a 16-bit variable out of TMR1L/TMR1H
Symbol TMR1ON = T1CON.0 ' TIMER1 Enable/Disable
Symbol TMR1IF = PIR1.0 ' TIMER1 overflow flag
Dim PING_LOOP as Byte ' PING Loop counter
Dim PULSE_LENGTH as Word ' TOF (Time Of Flight) value
'---------------------------------------------------------------------------------
' Program starts here
Delayms 500 ' Wait for PICmicro to stabilise
INTCON = 0 ' Make sure all interrupts are OFF
T1CON = %00000001 ' Enable Timer1 with a prescaler of 1:1
TRISB = %00000001 ' Set ECHO pin as input, all others as outputs
Cls ' Clear the LCD
Goto MAIN_PROGRAM_LOOP ' Jump over the PING subroutine
'---------------------------------------------------------------------------------
' The PING routine generates a 40khz burst of 8 cycles.
PING:
PING_LOOP = 8 ' Number of cycles in ping
PING1:
PORTB = %00010000 ' 1st half of cycle
Delayus 10 ' Create a delay of 10uS
PORTB = %00100000 ' 2nd half of cycle
Delayus 9 ' Create a delay of 9uS
Djnz PING_LOOP,PING1 ' Special mnemonic to form a fast loop
Return
'---------------------------------------------------------------------------------
' The main program loop starts here
MAIN_PROGRAM_LOOP:
While 1 = 1 ' Create an infinite loop
TMR1ON = 1 ' Enable TIMER1
Delayms 100 ' Delay 100ms between samples
TMR1IF = 0 ' Clear TIMER1 overflow
Gosub PING ' Transmit a 40KHz pulse
TIMER1 = 0 ' Reset TIMER1 before entering the loop
Repeat ' Loop until TIMER1 overflows
If ECHO = 0 Then ' Capture TIMER1 if a LOW on ECHO pin detected
TMR1ON = 0 ' Disable TIMER1 at this point
PULSE_LENGTH = TIMER1 ' Store the value of TIMER1
Break ' Exit the loop
Endif
PULSE_LENGTH = 0 ' If we reached here then Out of Range
Until TMR1IF = 1 ' Timeout if TIMER1 overflows
If PULSE_LENGTH = 0 Then ' Did we reach the end of the loop ?
Print at 1,1,"OUT OF RANGE " ' Yes. So Display text if out of range
Else ' Otherwise...
' Display distance in inches
Print at 1,1,"DIST = ",DEC PULSE_LENGTH / 146,34," "
Endif
Wend
لاحظ ان البرنامج يعرض المسافة بالبوصة
لكن اخى هل تتوقع ان يعكس الماء الصوتَ ؟
بالتوفيق