The STC89C52RC single-chip ultrasonic ranging program and electronic production object collected on the Internet are tested using the single-chip minimum system. The ultrasonic part uses the HC-SR04 module, with a measurement range of 4-300cm. The actual measurement can reach up to 5.70m. The following is a physical picture, and the source program is attached at the end of the article.
4-300cm, the longest actual measurement can reach 5.7m
#include 'AT89X51.H' //header file
unsigned char code fseg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
unsigned char code segbit[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char disbuf[8]={0,0,0,0,0,0,0,0};
//------------------------------------------------ --------------------------
// Function prototype definition
#define uchar unsigned char
#define uint unsigned int
void main (void); // Main function
void LED4_Display (void); // LED display
void LED_OUT(uchar X); //LED single byte serial shift function
void delayms(uint); //delay sub-function ms
void jisuan(void);
unsigned char code LED_0F[]; //LED font table
sbit DIO=P1^0; //Serial data input
sbit RCLK=P1^1; //Clock pulse signal——rising edge is valid
sbit SCLK=P1^2; //Input signal————Rising edge is valid
sbit echo=P3^2; //echo
sbit trig=P3^3; //trig
//------------------------------------------------ --------------------------
// Global variables
uchar LED[8]; //8-bit display buffer for LED
uint temp;
uint temp1;
uint a,b; //define a variable a, b, the latter is used to determine whether the signal is received
//
// Main program
//
void main(void) //main function starts
{
uint f;
echo=0; //Pull down the echo and trig pins first
trig=0;
f=500;
while(f>0); //Start delay to eliminate the fluctuation caused by the first power-on
{
LED[3]=16;
LED[2]=15;
LED[1]=15;
LED[0]=16;
f--;
}
EA=1; //Open the general interrupt
TMOD=0x11; //Set the timer to mode 1
ET0=1; //Enable timer interrupt This is mainly to prevent the ultrasonic module from not sending a signal
ET1=1;
while(1)
{
echo=0;//a assignment
a=0;
b=1;
TH0=0; //Initial value of timer
TL0=0;
TH1=(65536-25000)/256; //Timer initial value
TL1=(65536-25000)%6;
trig=1; //trig sends high
LED4_Display (); // Delay 3ms
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
trig=0; //trig is sent low
TR1=1;
while(echo==0); //Wait for echo to become high level
if(b==1) determines whether the signal is received
{
TR1=0; //Turn off timer 1
EX0=1; //Open external interrupt
TR0=1; //Start the timer
while(a==0); //Note here! a is assigned 0 before. The program stops here and waits for interruption.
//Originally I added a screen scanning program here, but I found a major BUG so I deleted it
}
else
{
LED[3]=16;
LED[2]=15;
LED[1]=15;
LED[0]=16;
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
LED4_Display();
}
}
}
void waibu() interrupt 0 //External interrupt service subroutine
{
temp=TH0; //Get the value of the timer
temp1=TL0;
EX0=0; //Disable external interrupt
TR0=0; //Turn off the timer
jisuan(); //Run calculation subroutine
a=1; //a is assigned a value of 1. The program returns to while(a) because the value of a has become 1, and the program starts from the beginning.
}
void time1() interrupt 1 //Timer interrupt service subroutine
{
TH0=0; //Reload initial value
TL0=0;
}
void time2() interrupt 3
{
TR1=0;
TH0=(65536-25000)/256; //Timer initial value
TL0=(65536-25000)%6;
b=0;
echo=1;
}
void jisuan(void) //Calculation subroutine
{
uint c,d; //define a variable c, d to determine the distance
c=0; //assign value 0
d=0; //Reassign value to b
LED4_Display (); //Scan the digital tube
temp=(temp<<8)+temp1; //TH0 TL0 merge
temp=temp/5; //I didn't calculate it exactly, so I just divided it by 5 to get an approximate value
if(temp>40) //Judge whether the distance is too close
{
c=1;
}
LED4_Display (); //Scan the digital tube
if(temp<40)
{
c=0;
}
LED4_Display();
if(temp<3000) //Judge whether the distance is too far
{
d=1;
}
LED4_Display();
if(temp>3000)
{
d=0;
}
c=c&d; //AND operation
if(c==1) //Judge whether the distance is normal
{
LED[3]=temp/1000; //Separate display of values
LED[2]=temp00/100;
LED[1]=(temp000/10)+20; //Why do we need to add 20? Because this is the unit digit and needs to display a decimal point
LED[0]=temp000;
}
if(c==0) //Judge whether the distance is normal
{
LED[3]=16;
LED[2]=14;
LED[1]=14;
LED[0]=16;
}
}
void delayms(uint xms) //delay sub-function ms
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
//The following program is the hc595 module display program
//The program of each module can be transplanted to the routine of the data
//
void LED4_Display (void) //Screen scanning sub-function
{
unsigned char code *led_table; // table pointer
uchar i;
// Display the first
led_table = LED_0F + LED[0];
i = *led_table;
LED_OUT(i);
LED_OUT(0x01);
RCLK = 0;
RCLK = 1;
//Display the second digit
led_table = LED_0F + LED[1];
i = *led_table;
LED_OUT(i);
LED_OUT(0x02);
RCLK = 0;
RCLK = 1;
//Display the 3rd digit
led_table = LED_0F + LED[2];
i = *led_table;
LED_OUT(i);
LED_OUT(0x04);
RCLK = 0;
RCLK = 1;
//Display the 4th digit
led_table = LED_0F + LED[3];
i = *led_table;
LED_OUT(i);
LED_OUT(0x08);
RCLK = 0;
RCLK = 1;
}
void LED_OUT(uchar X)
{
uchar i;
for(i=8;i>=1;i--)
{
if (X&0x80) DIO=1; else DIO=0;
X<<=1;
SCLK = 0;
SCLK = 1;
}
}
//The following is the display array
unsigned char code LED_0F[] =
{// 0 1 2 3 4 5 6 7 8 9 A b C d E F - .
0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x8C,0xBF,0xC6,0xA1,0x86,0x8e,0xbf,0x7f,0x00,0x00,
// 20 21 22 23 24 25 26 27 28 29 30
0x40,0x79,0x24,0x30,0x19,0x12,0x12,0x78,0x00,0x10,0xbf,
};
Previous article:STC12C2052 single chip LED dot matrix electronic clock production
Next article:Analysis of simulation results of ultrasonic distance measurement using 51 single chip microcomputer
Recommended ReadingLatest update time:2024-11-23 13:47
- Popular Resources
- Popular amplifiers
- Naxin Micro and Xinxian jointly launched the NS800RT series of real-time control MCUs
- How to learn embedded systems based on ARM platform
- Summary of jffs2_scan_eraseblock issues
- Application of SPCOMM Control in Serial Communication of Delphi7.0
- Using TComm component to realize serial communication in Delphi environment
- Bar chart code for embedded development practices
- Embedded Development Learning (10)
- Embedded Development Learning (8)
- Embedded Development Learning (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Intel promotes AI with multi-dimensional efforts in technology, application, and ecology
- ChinaJoy Qualcomm Snapdragon Theme Pavilion takes you to experience the new changes in digital entertainment in the 5G era
- Infineon's latest generation IGBT technology platform enables precise control of speed and position
- Two test methods for LED lighting life
- Don't Let Lightning Induced Surges Scare You
- Application of brushless motor controller ML4425/4426
- Easy identification of LED power supply quality
- World's first integrated photovoltaic solar system completed in Israel
- Sliding window mean filter for avr microcontroller AD conversion
- What does call mean in the detailed explanation of ABB robot programming instructions?
- STMicroelectronics discloses its 2027-2028 financial model and path to achieve its 2030 goals
- 2024 China Automotive Charging and Battery Swapping Ecosystem Conference held in Taiyuan
- State-owned enterprises team up to invest in solid-state battery giant
- The evolution of electronic and electrical architecture is accelerating
- The first! National Automotive Chip Quality Inspection Center established
- BYD releases self-developed automotive chip using 4nm process, with a running score of up to 1.15 million
- GEODNET launches GEO-PULSE, a car GPS navigation device
- Should Chinese car companies develop their own high-computing chips?
- Infineon and Siemens combine embedded automotive software platform with microcontrollers to provide the necessary functions for next-generation SDVs
- Continental launches invisible biometric sensor display to monitor passengers' vital signs
- Solution for MSP430F149 BSL download program failure
- Mobile Phone RF Design Skills (I)
- Is SUNSCO a single-chip microcomputer model?
- High voltage IGBT inverter and its application
- What are the factors that affect the power consumption of microcontrollers?
- Review of the 2019 National Electronic Design Competition - The small plane I made
- The origin of Chinese Valentine's Day and the legend of Chinese Valentine's Day
- Difference between TMS320C6713 and TMS320C6713B
- Working principle of LM393 voltage comparator
- The difference between MCU interface and RGB interface