Ultrasonic Flow Meter


 

Please click on Circuit Diagram.pdf for circuit diagram

 

The code is written in BASCOM AVR IDE from MCS Electronics It is a powerfull tool for development of AVR microcontroller based embedded syatems 

 

Code :


$regfile = "m8def.dat"
$crystal = 16000000 'L:FF; H:DF
'$sim
'Divide your crystal frequency by 1024 and put below
'F.e. 16000000/1024=15625; 20000000/1024=19531,25 (use 19531); 25000000/1024=24414,0625 (use 24414)
Const Compare = 15625

Ddrd.4 = 0 : Portd.4 = 1 ' Freq. I/P

Ddrc.5 = 1 : Portc.5 = 1 ' Freq. O/P '

Cursor Off Noblink

Cls
Locate 1 , 1
Lcd " Ultrasonic F.M."
Locate 2 , 1
Lcd "f= "

Wait 2

Locate 1 , 1
Lcd " "

Wait 1

Config Timer0 = Counter , Edge = Falling
Enable Timer0
On Timer0 Count Nosave

Config Timer1 = Timer , Prescale = 1024 , Clear Timer = 1
On Oc1a 1s Nosave
Enable Compare1a
Compare1a = Compare

Config Timer2 = Timer , Prescale = 1 , Clear Timer = 2
Timer2 = 122
On Timer2 Myisr
Enable Timer2

Enable Interrupts
Dim Multiplier As Word At &H70
Dim Fr_byte_copy As Byte At &H74

Dim Frequency As Dword
Dim A As Dword

Dim Freq_str As String * 7
Dim Dlug As Byte
Dim Vf_1 As String * 4 , Vf_2 As String * 3 , Vs As String * 9


Do

Loop
End

1s:
$asm
push R24
IN R24,counter0
STS &H74,R24
IN R24, SREG
push R24
PUSH R10
push R11
push R12
push R13
push R14
push R15
push R16
push R17
push R18
push R19
push R20
push R21
push R22
push R23
push R25
push R26
push R27
push R28
push R29
push R30
push R31
$end Asm


Frequency = Multiplier
Shift Frequency , Left , 8
Frequency = Frequency + Fr_byte_copy
Freq_str = Str(frequency)
Multiplier = 0

A = Val(freq_str)

A = 40000 - Frequency
A = A / 10
A = A * 1520000
A = A / 64500000

Locate 1 , 1
Lcd "F.RATE: " ; A ; " m/s"

'A = A * 1520
'A = A / 6400
'A = A * 1000
'A = A + 2

 

Reset Portb.0
Dlug = Len(freq_str)
Locate 2 , 4
Vs = Space(9)
If Dlug < 4 Then
Vs = Space(9)
Lcd Freq_str ; Chr(32) ; "Hz" ; Vs
End If
If Dlug >= 4 And Dlug < 7 Then
Vs = Space(4)
Freq_str = Format(freq_str , "0.000")
Lcd Freq_str ; Chr(32) ; "KHz" ; Vs
End If
If Dlug = 7 Then
Vf_1 = Left(freq_str , 4 )
Vf_1 = Format(vf_1 , "0.00")
Lcd Vf_1 ; Chr(32)
Vf_2 = Right(freq_str , 3)
Lcd Vf_2 ; Chr(32) ; "MHz"

End If

$asm
pop R31
pop R30
pop R29
pop R28
pop R27
pop R26
pop R25
pop R23
pop R22
pop R21
pop R20
pop R19
pop R18
pop R17
pop R16
pop R15
pop R14
pop R13
pop R12
pop R11
pop R10

'reset timer1 & counter0 at the same time
ldi R24,&H00
Out Tcnt1l , R24
Out Tcnt1h , R24
Out Tcnt0 , R24

'erase timer0 interrupt flag
in R24,tifr
SBR R24,&H01
Out Tifr , R24
pop R24
Out Sreg , R24
pop R24
$end Asm
Return

Count:
$asm
push R16
IN R16, SREG
push R24
push R25

'Incr Multiplier
lds R24,&H70
lds R25,&H71
adiw R24,&H01
sts &H70,R24
STS &H71,R25

POP R25
POP R24
Out Sreg , R16
pop R16
$end Asm
Return

Myisr:

Toggle Portc.5

Timer2 = 122

Return