The microcontroller source program is as follows:
//Crystal oscillator = 8M
//MCU=STC10F04XE
//P0.0-P0.6 common anode digital tube pin
//Trig = P1^0
//Echo = P3^2
#include #define uchar unsigned char //define for easy use #define uint unsigned int #define ulong unsigned long //*********************************************** sfr CLK_DIV = 0x97; //Defined for STC microcontroller, system clock division //Set the address definition for the IO port of the STC microcontroller sfr P0M1 = 0X93; sfr P0M0 = 0X94; sfr P1M1 = 0X91; sfr P1M0 = 0X92; sfr P2M1 = 0X95; sfr P2M0 = 0X96; //*********************************************** sbit Trig = P1^0; //Generate pulse pin sbit Echo = P3^2; //Echo pin sbit test = P1^1; //test pin uchar code SEG7[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90}; //digital tube 0-9 uint distance[4]; //Distance measurement receiving buffer uchar ge,shi,bai,temp,flag,outcomeH,outcomeL,i; //Custom register bit succeed_flag; //Measurement success flag //********Function declaration void conversion(uint temp_data); void delay_20us(); //void pai_xu(); void main(void) // Main program { uint distance_data,a,b; uchar CONT_1; CLK_DIV=0X03; //The system clock is 1/8 crystal oscillator (pdf-45 pages) P0M1 = 0; //Set the io port to push-pull output P1M1 = 0; P2M1 = 0; P0M0 = 0XFF; P1M0 = 0XFF; P2M0 = 0XFF; i=0; flag=0; test =0; Trig=0; //First pull the pulse input pin low TMOD=0x11; //Timer 0, timer 1, 16-bit working mode TR0=1; //Start timer 0 IT0=0; //Change from high level to low level, triggering external interrupt ET0=1; //Enable timer 0 interrupt //ET1=1; //Enable timer 1 interrupt EX0=0; //Disable external interrupt EA=1; //Open interrupt 0 while(1) //Program loop { EA=0; Trig=1; delay_20us(); Trig=0; //Generate a 20us pulse at the Trig pin while(Echo==0); //Wait for the Echo pin to become high succeed_flag=0; //Clear measurement success flag EX0=1; //Open external interrupt TH1=0; //Timer 1 cleared TL1=0; //Timer 1 cleared TF1=0; // TR1=1; //Start timer 1 EA=1; while(TH1 < 30); //Wait for the measurement result, period 65.535 milliseconds (can be implemented by interruption) TR1=0; //Turn off timer 1 EX0=0; //Disable external interrupt if(succeed_flag==1) { distance_data=outcomeH; //High 8 bits of the measurement result distance_data<<=8; //Put in the high 8 bits of 16 bits distance_data=distance_data|outcomeL; //Merge with the lower 8 bits to form 16-bit result data distance_data*=12; //Because the timer defaults to 12 division distance_data/=58; //microseconds divided by 58 equals centimeters } //Why is divided by 58 equal to centimeters, Y meters = (X seconds * 344) / 2 // X seconds = (2*Y meters)/344 ==》X seconds = 0.0058*Y meters ==》Centimeters = microseconds/58 if(succeed_flag==0) { distance_data=0; //clear if no echo test = !test; //Test light changes } /// distance[i]=distance_data; //Put the measurement result data into the buffer /// i++; /// if(i==3) /// { /// distance_data=(distance[0]+distance[1]+distance[2]+distance[3])/4; /// pai_xu(); /// distance_data = distance[1]; a=distance_data; if(b==a) CONT_1=0; if(b!=a) CONT_1++; if(CONT_1>=3) { CONT_1=0; b=a; conversion(b); } /// i=0; /// } } } //******************************************************************* //External interrupt 0, used to determine the echo level INTO_() interrupt 0 // External interrupt is number 0 { outcomeH =TH1; //Get the timer value outcomeL =TL1; //Get the timer value succeed_flag=1; //Flag of successful measurement EX0=0; //Disable external interrupt } //**************************************************************** //Timer 0 interrupt, used for display timer0() interrupt 1 // Timer 0 interrupt is number 1 { TH0=0xfd; //Write timer 0 initial value TL0=0x77; switch(flag) {case 0x00:P0=ge; P2=0xfd;flag++;break; case 0x01:P0=shi;P2=0xfe;flag++;break; case 0x02:P0=bai;P2=0xfb;flag=0;break; } } //******************************************************************** /* //Timer 1 interrupt, used for ultrasonic ranging timing timer1() interrupt 3 // Timer 0 interrupt is number 1 { TH1=0; TL1=0; } */ //********************************************************************** //Display data conversion program void conversion(uint temp_data) { uchar ge_data,shi_data,bai_data; bai_data=temp_data/100; temp_data=temp_data%100; //Remainder operation shi_data=temp_data/10; temp_data=temp_data%10; //Remainder operation ge_data=temp_data; bai_data=SEG7[bai_data]; shi_data=SEG7[shi_data]; ge_data =SEG7[ge_data]; EA=0; bai = bai_data; shi = shi_data; ge = ge_data; EA=1; } //********************************************************************** void delay_20us() { uchar bt ; for(bt=0;bt<100;bt++); } /* void pai_xu() { uint t; if (distance[0]>distance[1]) {t=distance[0];distance[0]=distance[1];distance[1]=t;} /*Exchange values if(distance[0]>distance[2]) {t=distance[2];distance[2]=distance[0];distance[0]=t;} /*Exchange values if(distance[1]>distance[2]) {t=distance[1];distance[1]=distance[2];distance[2]=t;} /*Exchange values } */
Previous article:PIC12F675 motor control program example source code
Next article:pic16f single chip source code, LED0 - LED5 light up in a row
- Popular Resources
- Popular amplifiers
- Learn ARM development(16)
- Learn ARM development(17)
- Learn ARM development(18)
- Embedded system debugging simulation tool
- A small question that has been bothering me recently has finally been solved~~
- Learn ARM development (1)
- Learn ARM development (2)
- Learn ARM development (4)
- Learn ARM development (6)
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- LED chemical incompatibility test to see which chemicals LEDs can be used with
- Application of ARM9 hardware coprocessor on WinCE embedded motherboard
- What are the key points for selecting rotor flowmeter?
- LM317 high power charger circuit
- A brief analysis of Embest's application and development of embedded medical devices
- Single-phase RC protection circuit
- stm32 PVD programmable voltage monitor
- Introduction and measurement of edge trigger and level trigger of 51 single chip microcomputer
- Improved design of Linux system software shell protection technology
- What to do if the ABB robot protection device stops
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Microchip Accelerates Real-Time Edge AI Deployment with NVIDIA Holoscan Platform
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Melexis launches ultra-low power automotive contactless micro-power switch chip
- Molex leverages SAP solutions to drive smart supply chain collaboration
- Pickering Launches New Future-Proof PXIe Single-Slot Controller for High-Performance Test and Measurement Applications
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- Apple faces class action lawsuit from 40 million UK iCloud users, faces $27.6 billion in claims
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- The US asked TSMC to restrict the export of high-end chips, and the Ministry of Commerce responded
- LIS2DW12 Accelerometer MakeCode Driver
- Problems with STM32F4 serial port 4 standard library program
- Recruiting part-time dsp training instructors
- Software platform installation for SILICON LABS PG22-DK2503A development board
- CCS CMD file invalidation causes memory allocation failure
- [ADI Practical Knowledge Sharing] The hidden costs of isolation system design cannot be ignored!
- EEWORLD University - Amplifier Design in Test and Measurement
- Design and implementation of FM function in handheld devices
- What is the io write speed of R16 itself?
- MSP430G2553 electronic clock experiment