habe in fastavr-basic eine einfache routine ohne timer und ohne pulse für den srf04 zum testen geschrieben, ich wollte einmal nach dem datenblatt gehen mit den zeiten und es geht, jetzt weiss ich auch wie ein sfr04 funktioniert.
geht bis auf 3mm genau. musst diese in einer sub packen und am anfang interrupt disable und am ende wieder interrupt enable.
Code:
$Device= m16      
$Stack = 32      
$Clock = 8  
$Baud  = 19200
$Source= On   

Dim entfernung As Integer     

DDRD.7=0 ' eingang
DDRD.6=1 ' ausgang

Enable Interrupts

Do

PORTD.6=1
WaitUs 2
PORTD.6=0
WaitUs 38
entfernung=0
While PIND.7=1
 Incr entfernung
Wend
Print entfernung
WaitMs 150

Loop
oder von Bascom :
Code:
$regfile = "m16def.dat"
$crystal = 8000000
$baud = 19200

Declare Function Ultrasonic_re() As Integer
Declare Sub Initialize_ultrasonic_re

Dim Ultra_daten_re As Integer

Enable Interrupts

Call Initialize_ultrasonic_re

Do

Ultra_daten_re = Ultrasonic_re()

Print Ultra_daten_re
Waitms 100

Loop

Sub Initialize_ultrasonic_re
   Config Pind.6 = Output
   Config Pind.7 = Input
End Sub

Function Ultrasonic_re() As Integer
    Portd.6 = 0
    Pulseout Portd , 6 , 10
    Pulsein Ultrasonic_re , Pind , 7 , 1
End Function


noch mal eine in fastavr-basic , wurde 1 zu 1 von winavr-c umgesetzt mit timer1 und TCCR1B :
Code:
$Clock = 8  
$Baud  = 19200
$Timer1 = Timer,Prescale=1
$Source= On   
 
Dim entfernung As Word 
Dim starttimer1 As Byte 

Const Prescale_1 = 9

DDRD.7=0 ' eingang
DDRD.6=1 ' ausgang

Enable Interrupts

StartTimer1=Prescale_1

Do

PORTD.6=1
WaitUs 2
PORTD.6=0
WaitUs 24
Timer1=0
TCCR1B=StartTimer1	
While PIND.7=1
Wend
entfernung=Timer1
Print entfernung
WaitMs 150

Loop
mfg pebisoft