使用8051的超声波水位控制器

时间:2018-10-31 18:03:58

标签: microcontroller sensor 8051

Circuit Diagram我正在就上述主题在AT89C51上进行项目。我没有在硬件/软件上获得输出。我在项目中使用HC-SR04超声波传感器。距容器顶部的高度大于8厘米,当距顶部的高度小于2厘米时关闭。我自省了很多,但仍然找不到我的代码中的错误。有人可以找出代码有什么问题吗?我最怀疑传感器部分。这是代码:-

 RS EQU P2.7                 ;equates RS to P2.7
 RW EQU P2.6                 ;equates RW to P2.6  
 E  EQU P2.5                 ;equates E to P2.5

 ORG 00H                  ;origin
  MOV   DPTR,#LUT         ;move starting address of LUT to DPTR        
  CLR   P3.0              ;clears P3.0(output)       
  SETB  P3.1              ;sets p3.1(input)     
  MOV   TMOD,#00100001B   ;sets Timer1 as Mode2 timer and Timer0 as Mode1 timer

 MAIN:
  ACALL DINT            ;calls DINT subroutine
  ACALL TEXT1           ;calls TEXT1 subroutine
  MOV   TL1,#200D       ;loads TL1 with start value
  MOV   TH1,#200D       ;loads TH1 with reload value 
  MOV   A,#00000000B    ;loads A with all Zeros
  SETB  P3.0            ;sets P3.0(trigger pulse) 
  ACALL DELAY1          ;calls DELAY1 subroutine (1uS)   
  CLR   P3.0            ;clears P3.0 
 HERE: 
  JNB   P3.1,HERE       ;loops here until echo is receive
 BACK:
  SETB  TR1             ;starts Timer1 
 HERE1:
  JNB   TF1,HERE1       ;loop here until Timer1 roll over occurs
  CLR   TR1             ;stops Timer1      
  CLR   TF1             ;clears Timer Flag 1         
  INC   A               ;Increments accumulator         
  JB    P3.1,BACK       ;jumps to label BACK if echo is still present     
  MOV   R7,A            ;saves accumulator to R7
  ACALL SPLIT           ;calls SPLIT subroutine
  ACALL LINE2           ;calls LINE2 subroutine
  ACALL LEVEL           ;calls LEVEL subroutine
  ACALL TEXT2           ;calls TEXT2 subroutine
  ACALL TEXT3           ;calls TEXT3 subroutine
  ACALL CHECK           ;calls CHECK subroutine
  JB    P2.0,JUMP       ;jumps to label JUMP if P2.0 is set
  ACALL TEXT4           ;calls TEXT4 subroutine
  SJMP  EXIT2           ;jumps to label EXIT2

  JUMP:
  ACALL TEXT5           ;calls TEXT5 subroutine
  EXIT2:
  ACALL DELAY2          ;calls DELAY2 subroutine(1S)
  SJMP  MAIN            ;jumps to label MAIN

  CHECK:
  MOV   A,R7            ;loads value of R7 to accumulator
  SUBB  A,#20D          ;subtracts 20D from A
  JNC   ON              ;jumps to label ON if carry flag not set(A>20D)
  ACALL OFF             ;else calls OFF subrotine
  SJMP  EXIT            ;jumps to label EXIT

  ON:
  SETB  P2.0            ;sets P2.0 (motor ON)
  EXIT:
  CLR   CY              ;clears carry flag
  RET                   ;return from subroutine

  OFF: 
  MOV   A,R7            ;loads value of R7 to accumulator
  SUBB  A,#5D           ;subtracts 5D from A 
  JNC   EXIT1           ;jumps to label EXIT1 if carry flag not set(A>5D)
  CLR   P2.0            ;else clears P2.0(motor OFF)
  EXIT1:
  CLR   CY              ;clears carry flag
  RET                   ;return from subroutine

  DELAY1: 
  MOV   R6,#2D          ;loads R6 with 2D
  LABEL1: 
  DJNZ  R6,LABEL1       ;loops here until R6 is 0
  RET                   ;return from subroutine

  DELAY2:
  MOV   R0,#15D         ;loads R0 with 15D
  BACK1: 
  MOV   TH0,#00000000B  ;loads TH0 with start value
  MOV   TL0,#00000000B  ;loads TL0 with start value
  SETB  TR0             ;starts Timer0

  HERE2: 
  JNB   TF0,HERE2       ;loops here until roll over occurs
  CLR   TR0             ;stops Timer0
  CLR   TF0             ;clear Timer Flag 0
  DJNZ  R0,BACK1        ;loops the timer session 15 times for 1S delay
  RET                   ;return from subroutine

  TEXT1: 
  MOV   A,#48H          ;ascii of "H"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#32H          ;ascii of "2"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4FH          ;ascii of "0"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#20H          ;ascii of " "
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4CH          ;ascii of "L"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#45H          ;ascii of "E"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#56H          ;ascii of "V"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#45H          ;ascii of "E"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4CH          ;ascii of "L"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#20H          ;ascii of " "
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#43H          ;ascii of "c"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#54H          ;ascii of "T"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#52H          ;ascii of "R"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4CH          ;ascii of "L"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  RET                   ;return from subroutine

  TEXT2:  
  MOV   A,#63H          ;ascii of "c"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#6DH          ;ascii of "m"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  RET                   ;return from subroutine

  TEXT3:
  MOV   A,#20H          ;ascii of " "
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4DH          ;ascii of "M"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#6FH          ;ascii of "o"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#74H          ;ascii of "t"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#6FH          ;ascii of "o"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#72H          ;ascii of "r"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#20H          ;ascii of " "
  ACALL DISPLAY         ;calls DISPLAY subroutine
  RET                   ;return from subroutine

  TEXT5: 
  MOV   A,#4FH          ;ascii of "O"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#4EH          ;ascii of "N"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  RET                   ;return from subroutine

  TEXT4:
  MOV   A,#4FH          ;ascii of "O"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#46H          ;ascii of "F"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  MOV   A,#46H          ;ascii of "F"
  ACALL DISPLAY         ;calls DISPLAY subroutine
  RET                   ;return from subroutine

  SPLIT:
  MOV   B,#10D           ;loads 10D to B
  DIV   AB               ;divides A with B
  MOV   R3,B             ;saves the remainder to R3 (3rd digit)
  MOV   B,#10D           ;loads 10D to B
  DIV   AB               ;divides A with B
  MOV   R2,B             ;saves the remainder to R2 (2nd digit)
  MOV   R1,A             ;saves the quotient to R1 (1st digit)
  RET                    ;return from subroutine

  LEVEL:
  MOV   A,R1             ;moves R1 to A (1st digit)
  ACALL ASCII            ;calls ASCII subroutine
  ACALL DISPLAY          ;calls DISPLAY subroutine
  MOV   A,R2             ;moves R2 to A (2nd digit)
  ACALL ASCII            ;calls ASCII subroutine
  ACALL DISPLAY          ;calls DISPLAY subroutine
  MOV   A,R3             ;moves R3 to A (3rd digit)
  ACALL ASCII            ;calls ASCII subroutine
  ACALL DISPLAY          ;calls DISPLAY subroutine
  RET                    ;return from subroutine

  DINT:
  MOV   A,#0CH           ;display ON cursor OFF
  ACALL CMD              ;calls CMD subroutine
  MOV   A,#01H           ;clear display screen
  ACALL CMD              ;calls CMD subroutine
  MOV   A,#06H           ;increment cursor
  ACALL CMD              ;calls CMD subroutine
  MOV   A,#80H           ;force cursor to the begining of 1st line
  ACALL CMD              ;calls CMD subroutine
  MOV   A,#3CH           ;activate 2nd line
  ACALL CMD              ;calls CMD subroutine
  RET                    ;return from subroutine

  LINE2:
  MOV   A,#0C0H          ;jump to 2nd line position 0
  ACALL CMD              ;calls CMD subroutine
  RET                    ;return from subroutine 

  CMD: 
  MOV   P0,A              ;moves A to P0
  CLR   RS                ;clears RS pin of LCD
  CLR   RW                ;clears RW pin of LCD
  SETB  E                 ;sets E pin of LCD
  CLR   E                 ;clears E pin of LCD
  ACALL DELAY             ;calls DELAY subroutine
  RET                     ;return from subroutine

  DISPLAY:
  MOV   P0,A              ;moves A to P0
  SETB  RS                ;sets RS pin of LCD
  CLR   RW                ;clears RW pin of LCD
  SETB  E                 ;sets E pin of LCD
  CLR   E                 ;clears E pin of LCD
  ACALL DELAY             ;calls DELAY subroutine
  RET                     ;return from subroutine

  DELAY:
  CLR   E                 ;clears E pin of LCD
  CLR   RS                ;clears RS pin of LCD
  SETB  RW                ;sets RW pin of LCD
  MOV   P0,#0FFH          ;sets P0 as input
  SETB  E                 ;sets E pin of LCD
  MOV   A,P0              ;moves P0 to A
  JB    ACC.7,DELAY       ;jumps back to label DELAY if ACC.7 is set
  CLR   E                 ;clears E pin of LCD
  CLR   RW                ;clears RW pin of LCD
  RET                     ;return from subroutine

  ASCII: 
  MOVC  A,@A+DPTR         ;over-writes A with ascii code of data in A 
  RET                     ;return from subroutine

  LUT: 
  DB  48D                ;ascii of "0"
  DB  49D                ;ascii of "1"
  DB  50D                ;ascii of "2"
  DB  51D                ;ascii of "3"
  DB  52D                ;ascii of "4"
  DB  53D                ;ascii of "5"
  DB  54D                ;ascii of "6"
  DB  55D                ;ascii of "7"
  DB  56D                ;ascii of "8"
  DB  57D                ;ascii of "9"

  END                     ;end statement

0 个答案:

没有答案