您现在的位置: 主页 > MCU > 单片机技术应用 > 51单片机超声测距程序 -
本文所属标签:
为本文创立个标签吧:

51单片机超声测距程序 -

来源: 网络用户发布,如有版权联系网管删除 2018-09-06 

[导读]
超声测距器单片机程序#include#defineucharunsignedchar#defineuintunsignedint#defineulongunsignedlongexternvoidcs_t(void);externvoiddelay(uint);externvoiddisplay(uchar*);datauchartestok;/*主

超声测距器单片机程序

本文引用地址: http://www.21ic.com/app/mcu/201806/764913.htm

#include
#defineucharunsignedchar
#defineuintunsignedint
#defineulongunsignedlong

externvoidcs_t(void);

externvoiddelay(uint);
externvoiddisplay(uchar*);
datauchartestok;
/*主程序*/
voidmain(void)
{
datauchardispram[5];
datauinti;
dataulongtime;
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);
}
}
//
/*超声接收程序(外中断0)*/
voidcs_r(void)interrupt0
{
TR0=0;
ET0=0;
EX0=0;
testok=1;
}

/*超时清除程序(内中断T0)*/
voidovertime(void)interrupt1
{
EX0=0;
TR0=0;
ET0=0;
testok=2;
}

;-------------------------------------
;超声发生子程序(12M晶振38.5Hz)
;-------------------------------------
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



;---------------------------------------
; 四位共阳LED动态扫描显示程序
; P0为段码口,P2为位选口(低电平有效)
; 参数为要显示的字符串指针
;---------------------------------------
NAME DISPLAY
?PR?_DISPLAY?DISPLAY SEGMENT CODE
?CO?_DISPLAY?DISPLAY SEGMENT DATA
EXTRN CODE (_DELAY)
PUBLIC _DISPLAY
RSEG ?CO?_DISPLAY?DISPLAY
?_DISPLAY?BYTE:
DISPBIT: DS 1
DISPNUM: DS 1
RSEG ?PR?_DISPLAY?DISPLAY
_DISPLAY: PUSH ACC
PUSH DPH
PUSH DPL
PUSH PSW
INC DISPNUM
MOV A, DISPNUM
CJNE A, #4D, DISP1
DISP1: JC DISP2
MOV DISPNUM, #00H
MOV DISPBIT, #0FEH
DISP2: MOV A, R1
ADD A, DISPNUM
MOV R0, A
MOV A, @R0
MOV DPTR, #DISPTABLE
MOVC A, @A+DPTR
MOV P0, A
MOV A, DISPNUM
CJNE A, #2D, DISP3
CLR P0.7
DISP3: MOV P2, DISPBIT
MOV R6, #00H
MOV R7, #0AH
LCALL _DELAY
MOV P0, #0FFH
MOV P2, #0FFH
MOV A, DISPBIT
RL A
MOV DISPBIT, A
POP PSW
POP DPL
POP DPH
POP ACC
RET
DISPTABLE:DB 0C0H,0F9H,0A4H,0B0H,99H,92H,82H,0F8H,80H,90H,88H,83H,0C6H,0A1H,86H,8EH,0BFH,0FFH
; "0", "1", "2", "3","4", "5","6","7", "8","9","A","B","C", "D", "E","F", "-", " "
END



              查看评论 回复



嵌入式交流网主页 > MCU > 单片机技术应用 > 51单片机超声测距程序 -
 

"51单片机超声测距程序 -"的相关文章

网站地图

围观()