
;This program is for ultrasonic range meter(without temperature effect calculating)
;at first mcu sends one 40khz pulse,
;if reflected pulse is received less than 70 msec
;means that distance is short
;but if the timer is overflowed,then mcu sends ten 70khz pulse and T_Flag is set
;if reflected pulse is received less than 70 msec
;means that distance is medium
;but if the timer is overflowed,then mcu sends twenty 70khz pulse and T_Flag is set
;again,if reflected pulse is received less than 70 msec
;means that distance is long
;but if the timer is overflowed,mcu must send this message to display and T_Flag is set
;distance is undetermined (you must add this part of code to your program in START_1:)

T_FLAG                EQU 00H
REC                   EQU P2.3
REC_START             EQU P2.4
TRANS                 EQU P2.5
PULSE_POWER           EQU 40H

CLR T_FLAG

;...
;...
;...

START_OF_MEASURING:
LCALL CHANGE_POWER              ;CHANGE NUMBER OF 40 KHZ PULSES IN TRANSMITTER
MOV TMOD,#00010000B
MOV TH1,#0
MOV TL1,#0
SETB TR1
LCALL PULSE_GEN                 ;SEND 40 KHZ PULSE ACCORDING TO PULSE_POWER VALUE
SETB REC
SAS10:                          ;PREVENTING FROM FATAL ERROR
JB REC,SAS10                    ;FIRST EFFECT OF TRANSMITTER IN RECIEVER
SAS12:
JB TF1,START_1
JNB REC,SAS12                   ;CORRECT REFLECTION IS OBTAINED
CLR TR1                         ;SAVE TIMER VALUE
CLR T_FLAG                      ;POWER OF PULSE IS SUITABLE
                                ;NOW YOU CAN FIND THE DISTANCE EASILY
RET

START_1:
SJMP START_OF_MEASURING         ;DISTANCE IS UNDETERMINED(TIMER IS OVERFLOWED)
                                ;TRY AGAIN


CHANGE_POWER:
JNB T_FLAG,NEXT_3
MOV A,PULSE_POWER
CJNE A,#1,NEXT_1
MOV PULSE_POWER,#10
RET

NEXT_1:
CJNE A,#10,NEXT_2
MOV PULSE_POWER,#20
RET

NEXT_2:
CJNE A,#20,NEXT_3
MOV PULSE_POWER,#1
NEXT_3:
RET

PULSE_GEN:
MOV R7,PULSE_POWER
HEY:
SETB TRANS
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
CLR TRANS
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
NOP
DJNZ R7,HEY
SETB TRANS
RET
