微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > 单片机超声波传感器测量距离

单片机超声波传感器测量距离

时间:11-18 来源:互联网 点击:

T1中断,发超声波用

INTT1: CPL VOUT

DJNZ R4,RETIOUT

CLR TR1 ;超声波发送完毕,关T1

CLR ET1

MOV R4,#04H

SETB EX0 ;开启接收回波中断

RETIOUT: RETI

;外中断0,收到回波时进入

PINT0: CLR TR0 ;关计数器

CLR TR1

CLR ET1

CLR EA

CLR EX0

MOV 44H, TL0 ;将计数值移入处理单元

MOV 45H, TH0

SETB 00H ;接收成功标志

RETI

*延时程序*

DL1MS: MOV R6, #14H

DL1: MOV R7, #19H

DL2: DJNZ R6, DL2

DJNZ R6, DL1

RET

*显示程序*

;40H为最高位,43H为最低位,先扫描高位

DISPLAY: MOV R1, #40H;G

MOV R5,#0F7H;G

PLAY: MOV A, R5

MOV P0, #0FFH

MOV P2, A

MOV A, @R1

MOV DPTR, #TAB

MOVC A, @A+DPTR

MOV P0, A

LCALL DLIMS

INC R1

MOV A, R5

JNB ACC.0, ENDOUT;G

RR A

MOV R5, A

AJMP PLAY

ENDOUT; MOV P2, #0FFH

MOV P0, #0FFH

RET

TAB; DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,0FFH,88H,0BFH

;共阳数码管 0 ,1, 2,3,4,5,6,7,8,9,不亮,A, —

*距离计算程序(=计算值×17/1000cm) 近似

WORK: PUSH ACC

PUSH PSW

PUSH B

MOV PSW, #18H

MOV R3, 45H

MOV R2, 44H

MOV R1, #00D

MOV R0, #17D

LCALL MUL2BY2

MOV R3, #03H

MOV R2, #0E8H

LCALL div4BY2

LCALL div4BY2

MOV 40H, R4

MOV A, 40H

JNZ JJ0

MOV 40H, #0AH ;最高位为0,不点亮

JJ0: MOV A R0

MOV R4, A

MOV A R1

MOV R5 A

MOV R3, #00D

MOV R2, #100D

LCALL div4BY2

MOV 41H, R4

MOV A, 41H

JNZ JJ1

MOV A, 40H ;此高位为0,先看最高位是否为不亮

SUBB A, #0AH

JNZ JJ1

MOV 41H, #0AH ; 最高位不亮,次高位也不亮

JJ1: MOV A, R0

MOV R4, A

MOV A, R1

MOV R5, A

MOV R3, #00D

MOV R2, #10D

LCALL div4BY2

MOV 42H, R4

MOV A 42H

JNZ JJ2

MOV A, 41H ;次高位为0,先看次高位是否为不亮

SUBB A, #0AH

JNZ JJ2

MOV 42H, #0AH ;次高位不亮,次高位也不亮

JJ2: MOV 43H, R0

POP B

POP PSW

POP ACC

RET

*两字节无符号数乘法程序

MUL2BY2: CLR A

MOV R7, A

MOV R6, A

MOV R5, A

MOV R4, A

MOV 46H, #10H

MULLOOP1: CLR C

MOV A, R4

RLC A

MOV R4, A

MOV A, R5

RLC A

MOV R5, A

MOV A, R6

RLC A

MOV R6, A

MOV A, R7

RLC A

MOV R7, A

MOV A, R0

RLC A

MOV R0, A

MOV A, R1

RLC A

MOV R1, A

JNC MULLOOP2

MOV A, R4

ADD A, R2

MOV R4, A

MOV A, R5

ADDC A, R3

MOV R5, A

MOV A, R6

ADDC A, #00H

MOV R6, A

MOV A, R7

ADDC A, #00H

MOV R7, A

MULLOOP2: DJNZ 46H, MULLOOP1

RET

*四字节/两字节无符号数除法程序*

div4BY2: MOV 46H, #20H

MOV R0, #00H

MOV R1, #00H

divLOOP1: MOV A, R4

RLC A

MOV R4, A

MOV A, R5

RLC A

MOV R5, A

MOV A, R6

RLC A

MOV R6, A

MOV A, R7

RLC A

MOV R7, A

MOV A, R0

RLC A

MOV R0, A

MOV A, R1

RLC A

MOV R1, A

CLR C

MOV A, R0

SUBB A, R2

MOV B, A

MOV A, R1

SUBB A, R3

JC divLOOP2

MOV R0, B

MOV R1, A

divLOOP2: CPL C

DJNZ 46H, divLOOP1

MOV A, R4

RLC A

MOV R4, A

MOV A, R5

RLC A

MOV R5, A

MOV A, R6

RLC A

MOV R6, A

MOV A, R7

RLC A

MOV R7, A

RET

;

END

附C51程序

#include

#define uchar unsigned char

#define uint unsigned int

#define ulong unsigned long

extern void cs_t(void);

extern void delay(uint);

extern void display(uchar*);

//data uchar display(uchar*);

data uchar testok;

void main (void)

{

data uchar dispram[5];

data uint i;

data ulong time;

P0=0xff;

P2=0xff;

TMOD=0x11;

IE=0x80;

while (1)

{

cs_t();

delay(1);

testok=0;

EX0=1;

ET0=1;

while(! testok) display(dispram);

if (1==testok)

{

time=TH0;

time=(time<8)| TL0;

time*=172;

time/=10000;

dispram[0]=(uchar) (time%10);

time/=10;

dispram[1]=(uchar) (time%10);

time/=10;

dispram[2]=(uchar) (time%10);

dispram[3]=(uchar) (time/10);

if (0==dispram[3]) dispram[3]=17;

} else

{

dispram [0]=16;

dispram [1]=16;

dispram [2]=16;

dispram [3]=16;

}

for (i=0;i<300;i++) display(dispram);

}

}

void cs_r(void) interrupt 0

{

TR0=0;

ET0=0;

EX0=0;

testok=1;

}

void overtime(void) interrupt 1

{

EX0=0;

TR0=0;

ET0=0;

testok=2;

}

NAME CS_T

?PR?CS_T?CS_T SEGMENT CODE

PUBLIC CS_T

RSEG ?PR?CS_T?CS_T

CS_T: PUSH ACC

MOV TH0, #00H

MOV TL0, #00H

MOV A, #4D

SETB TR0

CS_T1: CPL p1.0

NOP

NOP

NOP

NOP

NOP

NOP

NOP

NOP

NOP

NOP

DJNZ ACC,CS_T1

POP ACC

RET

;

END

name delay

?pr?_delay?delay segment code

public _delay

rseg ?pr?_delay?delay

_delay: push acc

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top