PIC18F4520 single chip ultrasonic distance measurement main part program

Publisher:数字翻飞Latest update time:2015-03-11 Source: 51heiKeywords:PIC18F4520 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Test environment: MPLAB IDE v8.73a
Microcontroller model: PIC18F4520
Required devices: Ultrasonic module
The circuit connection diagram is as follows: Ultrasonic ranging subfunction:


unsigned int chaosheng()

 unsigned int temp1, temp2; //Define two unsigned integer variables to store distance values
 INPUT = 1;
 delay(50); //delay
 INPUT = 0;
 while(!OUTPUT); //When the ultrasonic output pin outputs a high level, turn on timer 0 and start timing
 TMR1ON = 1;
 while(OUTPUT); //When the ultrasonic output pin outputs a low level, turn off timer 0 and stop timing
 TMR1ON = 0;
 //Calculate the distance measured by ultrasound
 temp1 = TMR1L; //Assign the lower 8 bits of TMR1 to the temp1 variable
 temp2 = TMR1H; //Assign the upper 8 bits of TMR1 to the temp2 variable
 temp2 = (temp2 << 8) + temp1; //Shift temp2 left by 8 bits and add temp1
 TMR1H = 0; //The high 8 bits of TMR1 are cleared to prepare for the next distance measurement
 TMR1L = 0; //The low 8 bits of TMR1 are cleared to prepare for the next distance measurement
 
 //Distance = time/2 * speed
 //Distance: distance; time: temp2/2 in μs; speed: 340m/s 
 //Pay attention to the units here, and need to convert them so that the final distance unit is cm/s
 distance = temp2 * 1.7/100; //There is a certain error, which can be filled by software. According to the actual measured value, add or subtract an error value to the expression on the right.
   
 return distance; //Return the distance value measured by ultrasound. Here, distance is a global variable and can be used in other programs, such as the LCD display part, which can be passed as an actual parameter to its corresponding program for display.
 
}

Finally: I really appreciate Google's interview question: Use 3 sentences to explain to the old lady what a database is. Jobs said: "The best thing is to focus on simplicity" because simple things are harder to do than complex things.
Keywords:PIC18F4520 Reference address:PIC18F4520 single chip ultrasonic distance measurement main part program

Previous article:Design of digital vortex flowmeter based on single chip microcomputer
Next article:A brief discussion on the relationship between RAM and assembly program in PIC8-bit microcontroller

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号