Ultrasonic rangefinder microcontroller program
#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 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);
}
}
//
/*超声接收程序(外中断0)*/
void cs_r(void) interrupt 0
{
TR0 = 0;
ET0 = 0;
EX0 = 0;
testok = 1;
}
/*超时清除程序(内中断T0)*/
void overtime(void) interrupt 1
{
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
;---------------------------------------
; Four-digit common anode LED dynamic scanning display program
; P0 is the segment code port, P2 is the bit select port ( low level is valid)
; The parameter is the string pointer to be displayed
;---------------------------------------
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 R 7, #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
;-------------------------------------
; Delay 100 machine cycles * parameter (1~65535)
;-------------------------------------
NAME DELAY
?PR?_DELAY?DELAY SEGMENT CODE
PUBLIC _DELAY
RSEG ?PR?_DELAY?DELAY
_DELAY: PUSH ACC ;2
MOV A, R7 ;1
JZ DELA1 ;2
INC R6 ;1
DELA1: MOV R5, #50D ;2
DJNZ R5, $ ;2
DJNZ R7, DELA1 ;2
DJNZ R6, DELA1 ;2
POP ACC ;2
RET ;2
;
END
Previous article:51 single chip microcomputer fast water heater program
Next article:AT89C52 clock program
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- Can anyone help explain the authentication process when the NB-IOT card is connected to the telecom network?
- [Erha Image Recognition Artificial Intelligence Vision Sensor] 4. Color recognition and object recognition
- 1. [Learning LPC1768 library functions] LED experiment
- [Synopsys IP Resources] How many steps are required for a successful RDC sign-off?
- Use Burn in software to test and vibration test the hard disk and a temperature alarm appears
- [Bing Dwen Dwen Award List] 2022 Digi-Key Innovation Design Competition
- Have you ever done this kind of impedance marking that makes people collapse and go to the hospital?
- Transimpedance amplifier circuit, question about the maximum optical power that a photodiode can receive
- Why has the GaN charger that Lei Jun recommends in various ways become a charging magic tool?
- Sell some IC chips