1、第 1 页 共 64 页 目录 设计总说明: . . 3 ABSTRACT:. 5 第一章:超声波测距原理论述 . 7 1.1 超声波介绍 . 7 1.2 超声波测距系统概述 . 9 1.3 超声波测距的基本原理 . 11 1.4 本课题的内容和任务 . 12 第二章 AVR 单片机介绍 . 13 2.1 ATmega16 结构框图 . 16 2.2 AVR CPU 内核 . 19 2.3 AVR ATmega16 存储器。 . 19 2.4 AVR ATmega16 系统时钟 . 19 2.5 系统控制和复位 . 20 2.6 看门狗定时器 . 20 2.7 ATmega16 的中断向量(外
2、部中断) . 20 2.8 具有 PWM 功能的 8 位定时器 / 计时器 . 21 2.9 比较输出模式和波形产生 . 22 2.10 T/C0 与 T/C1 的预分频器 . 24 2.11 串行外设接口 SPI . 24 2.12 串行外设接口 USART . 25 2.13 模数转换 器 . 25 2.14 JTAG 接口和片上调试系统 . 26 第三章 硬件电路的设计 . 26 3.1 电源电路设计 . 26 3.2 复位电路设计 . 27 3.3 时钟电路设计 . 27 3.4 数码管显示电路 . 28 3.5 报警电路设计 . 30 第 2 页 共 64 页 3.6 温度补偿电路
3、.31 3.6.1 温度计算 .33 3.6.2 DSl820 工作过程命令 .33 3.6.3 时 序 .33 3.6.4 写时间隙 .34 3.6.5 读时间隙 .34 3.6.6 多路测量 .34 3.7 在线通信电路设计 .35 第四章,超声波发射电路及接收电路的设计 . 36 4.1 超声波发射电路 . 36 4.1.1 压电陶瓷超声波传感器介绍 .36 4.1.2 发射电路原理图分析 .38 4.2 超声波接收电路 .39 4.2.1 LC 震荡选频电路设计: .39 4.2.2 比较电路的设计 .40 4.2.3 接收电路原理图分析 .41 第五章软件设计 . 43 5.1 主程
4、序流程图 .44 5.2 发射子程序设计 .44 5.3 温度测量子程序 .44 5.4 测量子程序 .46 5.5 计算子程序 .46 5.6 显示驱动子程序 .47 5.7 报警 子程序 .47 第六章 设计心得 . 49 致 谢 . 50 参考文献 . 51 附录 . 52 第 3 页 共 64 页 基于单片机的超声波测距电路的研究 设计总说明 :超声波因其指向性强 ,能量消耗缓慢 ,在介质中传播距离远等特点 ,而经常用于进行各种测量 . 如利用超声波在水中的发射 ,利用 超声波在固体中的传播 ,可以用作金属探伤、医用 A 超、 B 超等 . 利用超声波测距 ,使用单片机系统 ,设计合理
5、 ,计算处理也较方便 ,测量精度能达到各种场合使用的要求 . 这篇应用性设计报告描述了一种基于 AVR ATMEGA16 低功耗单片机的超声波测距系统,本系统发射器对着一个物体发射一定频率的超声波同时接收同频率的超声波,单片机通过计算从超声波发射时刻到接收返回的超声波时刻从而确定超声波通过的时间,根据房间的温度来确定超声波在空气中的速度大概是 340m/s , AVR 单片机计算二者的距离同时用 3个 LED 驱动电路驱动的 LED 来显示,显示距离误差大概是 1cm,最小能测量时 1cm 同时局限于发射器的传感器的设定时间,最大能测量 4m,超声波测距发射距离决定与发射物的材质和形状,例如超
6、声波可能被地毯吸收,这样测量的距离就大大的降低,假如反射波接收的频率太低就可能不被系统处理,这样显示就会出现错误。 1 设计理论: 本设计应用基于声波的反射。声波在其传播的介质中被定义为纵波。当声波受到尺寸大于其波长的目标物阻挡时就会发生反射;反射波称为回声。如果声波在介质中传播的速度是已知的,而且测量到声波从声源到达目标然后返回声源的时间,从声源到 目标的距离就可以精确地计算出来。这就是本应用的测量原理。这里声波传播的介质就是空气,采用不可见的超声波。 假设室内超声波的速度是 340m/s 则可以通过计算超声波通过时间来计算距离,但是实际温度对超声波影响很大,通过可以研究,速度和温度( T为
7、绝对温度)存在一下关系 : 0 1/273Tv v m s340 /o m sv 由于超声波通过的距离是 2 倍的实际距离,则实际距离是 d/2,所以 t/2dv 2 电路描述 : 第 4 页 共 64 页 本设计用来发射和接收超声波的设备是 40hz 压电陶瓷超声波传感器, AVR ATMEGA16单片机驱动超声波发射器 40hz 的方波来源于晶振 ,波接收器接收回波 由于 AVR ATMEGA16 单片机的计时器计算 40khz 的分辨率是 25us 是完全胜任我们的设计,我们系统的稳定性来源于晶振的工作。被超声波接收器超声波通过一个运算放大器放大对输入a放大,相对输入 a输出超声波的同时
8、触发单片机计时器 timer1 ,捕获的回波被精确计算时间来计算距离。计数器从超声波发射开始计时到收到回波停止,时间被精确记录,我们可以通过 DS18B20 芯片来确定室温,精确的确定超声波的速度,二者的距离通过 AVR ATMEGA16 精确的计算同时在 3个数码管上显示出来,一旦显示出来,单片机就进入休眠状态来节省电力能源。这篇设计的主要电路分析。 传感器的输出驱动电路直接由 9V 电池供电并提供驱动超声波发射器由一个二进制非门 CD4049 电路实现的。其中一个非门用来为驱动器的一侧提供 180 度的相移信号。另一侧由相内信号驱动。这种结构使输出端的电压提高了一倍,为发射传感器提供了足够
9、的电压。两个门并联连接以便每一侧能够为传感器提供足够的驱动电流。电容 耦合阻断了到传感器的直流通路。因为 CD4049 工作于 9V 而 AVR ATMEGA16工作于 Vcc=5V。 AVR ATMEGA16 和输出驱动器之间的逻辑电平是不匹配的,可以双极性晶体管就作为这两种逻辑电平之间的转换器。 由 LC 选频放大器对超声波接收器接收的回波在 40KHz 时提供充分的高增益。选择并丢弃除了 40KHz 之外的频率。运算放大器的输出端连接到比较器 LM393的输入端。 比较器 LM393 的参考电平内部选择为 0V。当接收到回声时电压高于参考电平从而触发比较器的输出。然后触发单片机的 INT
10、0. 本文在了解超声波测距原理的基础上,完成了基于时差测距原理的一种超声波测距系统的硬件设计,其中为了进一步提高系统测量精度和系统稳定性,在硬件上增加了温度传感器测温电路,采取声速预置和媒质温度测量相结合的办法对声速进行修正,降低了温度变化对测距精度的影响。针对噪声环境中超声波测距的情况,本文讨论了一种基于时延的估计方法,可有效地降低噪声对测距的干扰,有利于提高超声波测距系统的测量精度。 关键词 :超声波测距 AVR atmega16 DS18B20 第 5 页 共 64 页 ABSTRACT: In different occasions , the demands of the preci
11、sion on ultrasonic distance measuring system are different .Usually , the error of the ultrasonic distance measuring system is large , so they cannot be satisfied with the demands in some occasions. This article takes temper A Ture account into the ult rasonic distance measuring system and makes it
12、have higher precision han before and increases the function of broadcasting the result . It can apply in more occasions and be felt more convenient . This design application report describes a distance-measuring system based on ultrasonic sound utilizing the AVR atmega16 ultralow-power microcontroll
13、er. The system transmits a burst of ultrasonic sound waves towards the subject and then receives the corresponding echo. The time taken for the ultrasonic burst to travel the distance from the system to the subject and back to the system is accurately measured by the AVR atmega16. Assuming the speed
14、 of sound in air at room temperature to be 340m/s, the AVR atmega16 computes the distance between the system and the subject and displays it using a three-digit static LED driven by its integrated LED driver. The distance is displayed in inches with an accuracy of 1 cm. The minimum distance that thi
15、s system can measure is 1cm and is limited by the transmitters transducer settling-time. The maximum distance that can be measured is 4m. The amplitude of the echo depends on the reflecting material, shape, and size. Sound-absorbing targets such as carpets the maximum measurable range is lower for s
16、uch subjects. If the amplitude of the echo received by the system is so low that it is not detectable by the Comparator the system goes out of range. This is indicated by displaying the error message 1 Theory of Operation This application is based on the reflection of sound waves. Subjects whose Dim
17、ensions are larger than the wavelength of the impinging sound waves reflect them; the reflected waves are called the echo. If the speed of sound in the medium is known and the time taken for the sound waves to travel the distance from the source to the subject and back to the source is measured, the
18、 distance from the source to the subject can be computed accurately. This is the measurement principle of this application. Since it is inaudible to humans. Assuming that the speed of sound in air is v=340m/s at room temperature and that the measured time taken for the sound waves to travel the dist
19、ance from the source to the subject and back to the source is seconds,as we know: 第 6 页 共 64 页 0 1/273Tv v m s340 /o m sv The distance d is computed by the formula t/2dv Since the sound waves travel twice the distance between the source and the subject, the actual distance between the source and the
20、 subject will be d/2. 2 Circuit Description The devices used to transmit and receive the ultrasonic sound waves in this application are 40-kHz ceramic ultrasonic transducers. AVR ATMEGA16 drives the transmitter transducer with 40-kHz square-wave signal derived from the crystal oscillator, and the re
21、ceiver transducer receives the echo. The Timer1in the AVR is configured to count the 40-kHz crystal frequency such that the time measurement resolution is 25 s, which is more than adequate for this application. The measurement time base is very stable as it is derived from a quartz-crystal oscillato
22、r. The echo received by the receiver transducer is amplified by an operational amplifier and the amplified output is fed to the Comparator_A input. The Comparator_A senses the presence of the echo signal at its input and triggers a capture of Timer_A count value to capture compare register timer1. T
23、he capture is done exactly at the instant the echo arrives at the system. The captured count is the measure of the time taken for the ultrasonic burst to travel the distance from the system to the subject and back to the system. The distance in inches from the system to the subject is computed by th
24、e AVR ATMEGA16 using this measured time and displayed on a two-digit static LED. Immediately after updating the display, the AVR goes to sleep mode to save power. The circuit schematic diagram of this application. The output drive circuit for the transducer is powered directly from the 9-V battery a
25、nd provides drive to the ultrasonic transmitter. The is achieved by a bridge configuration with hex inverter gates CD4049. One inverter gate is used to provide a 180-degrees phase-shifted signal to one arm of the driver. The other arm is driven by the in-phase signal. This configuration doubles the
26、voltage swing at the output and provides the required to the transmitter transducer. Two gates are connected in parallel so that each arm can provide adequate current drive to the transducer. Capacitors block the dc to the transducer. Since the CD4049 operates on 9-V and the AVR ATMEGA16 operates on
27、 a VCC of 5 V, there is a logic 第 7 页 共 64 页 level mismatch between the AVR ATMEGA16 and the output driver circuit. Bipolar transistor acts as a logic-level shifter between these two logic levels. Operational amplifier NPN is made of by Circuit ,This amplifier has a high-gain bandwidth and provides
28、sufficiently high gain at 40 kHz. The amplified ultrasonic signal swings above and below this virtual midrail. provides selectivity and rejection of unwanted frequencies other than 40kHz. The output of the operational amplifier is connected to the ComparatorLM393 input of the ATMEGA16 via port pin I
29、NT0. The Comparator LM393 reference is internally selected to be 0.5VCC. When no ultrasonic echo is received, the voltage level at CA0 is slightly lower than the reference at LM393. When an echo is received, the voltage level increases above the reference and toggles the Comparator LM393 can be fine
30、-tuned for the required sensitivity and the measurable range can be optimized and give the single to the ATMEGA16. Based on the comprehension of measuring distance principle by ultrasonic, the paper completes an hardware design which based on time difference measuring distance theory , In order to improve the measurement accuracy and system stability further, we add a temperature sensor in the hardware design and adopt the improved method which combines sound velocity presetting with medium temperature measurem