基于单片机的数字温度计c程序(Microcontroller based digital thermometer C program)
- 格式:doc
- 大小:53.50 KB
- 文档页数:17
摘要本文将介绍一种基于单片机控制的数字温度计。
在硬件方面介绍单片机温度控制系统的设计, 对硬件原理图做简洁的描述。
系统程序主要包括主程序、读出温度子程序、温度转换命令子程序、计算温度子程序、显示数据刷新子程序。
软硬件分别调试完成以后,将程序下载入单片机中,电路板接上电源,电源指示灯亮,按下开关按钮,数码管显示当前温度。
由于采用了智能温度传感器DS18B20,所以本文所介绍的数字温度计与传统的温度计相比它的转换速率极快,进行读、写操作非常简便。
它具有数字化输出,可测量远距离的点温度。
系统具有微型化、微功耗、测量精度高、功能强大等特点,加之DS18B20内部的差错检验,所以它的抗干扰能力强,性能可靠,结构简单。
关键词:单片机,数字控制,温度计,DS18B20AbstractThis article introduces a microcomputer-based control of digital thermometers from the hardware description Temperature Control system design, hardware schematics and concise description. System program includes the main program, subroutine read temperature, the temperature conversion command subroutine, subroutines calculate the temperature, display data refresh routines. Debugging of hardware and software were completed, the program downloaded into the MCU, the circuit board connected to the power supply, power indicator light, press the switch button, digital display the current temperature. As a result of smart temperature sensor DS18B20,therefore, this article describes the traditional thermometer digital thermometer and the conversion rate compared to its fast, read, write with ease. It has a digital output, measurable distance of the point temperature. System has a micro, micro-power, high precision, powerful characteristics, combined with the internal error DS18B20 test, its anti-interference ability, reliable, simple in structure.Key words: Single-chip microcomputer, digital control, thermometer, DS18B20目录1 绪论 (1)1.1 背景 (1)1.2 设计的目的和意义 (2)2 设计要求与方案论证 (3)2.1 设计要求 (3)2.2 方案论证 (3)2.3 总体设计方案 (4)3 硬件设计 (5)3.1 主要元件介绍 (6)3.1.1 主控制器 (6)3.1.2 温度传感器DS18B20 (8)3.2 显示电路 (15)3.3 DS18B20与单片机的接口电路 (18)3.4 复位电路 (20)4 软件设计 (21)5 调试 (22)5.1 软件调试 (22)5.2 系统调试 (22)5.3 数据检测 (23)总结 (24)致谢 (25)参考文献....................................................................................................... 错误!未定义书签。
前言科技发展到今天,人们的生活中涌现出各种各样的科技产品,各种各样的电子产品更是花样百出、遍及人们生活中的每一部分,现在人们更是感觉到了科技给人们带来的巨大发展,科学技术作为第一生产力在人类社会的发展中起了很大的推动作用,人类从原始向先进的发展都伴随着科学的发展。
当今微型计算机技术发展形成两大分支,一是以微处理器(Micro Processor Unit)为核心所构成的通用微机系统,主要用于科学计算、数据处理、图形图像处理、数据库管理、人工智能、数字模拟与仿真等领域。
另一分支是为控制器( Micro Controller Unit),俗称单片机。
单片机主要用于工业测控,如家用电器、计算机外围设备、工业智能化仪表、机器人、生产过程的自动控制、农业、化工、军事、航空航天等领域。
单片机是一种集成在电路芯片,是采用超大规模集成电路技术把具有数据处理能力的中央处理器CPU随机存储器RAM、只读存储器ROM、多种I/O口和中断系统、定时器/计时器等功能(可能还包括显示驱动电路、脉宽调制电路、模拟多路转换器、A/D转换器等电路)集成到一块硅片上构成的一个小而完善的计算机系统。
单片机也被称为微控制器(Microcontroller Unit),常用英文字母的缩写MCU 表示单片机,它最早是被用在工业控制领域。
单片机由芯片内仅有CPU的专用处理器发展而来。
最早的设计理念是通过将大量外围设备和CPU集成在一个芯片中,使计算机系统更小,更容易集成进复杂的而对体积要求严格的控制设备当中。
INTEL的Z80是最早按照这种思想设计出的处理器,从此以后,单片机和专用处理器的发展便分道扬镳。
随着时代的进步和发展,单片机技术已经普及到我们生活、工作、科研、各个领域,已经成为一种比较成熟的技术。
摘要:单片机具有体积小、功耗低、控制功能强、扩展灵活、微型化和使用方便等优点,广泛应用于仪器仪表中,结合不同类型的传感器,可实现诸如电压、功率、频率、湿度、温度、流量、速度、厚度、角度、长度、硬度、元素、压力等物理量的测量。
基于单片机的数字显示体温计设计摘要本文针对传统的测温仪器自身存在的诸多缺点以及在现实生活中所暴露的使用不便,缺少安全性等缺陷,提出了一种非接触式红外测温系统设计方案。
该系统是以STC89C52作为红外测温传感器数据传输和控制核心。
此外,还设计了液晶显示器、功能按键等外围模块。
该系统最大的创新之处在于实现基本的非接触式温度测量功能,它的安全性,方便性更有利于普通百姓的使用。
本次红外测温系统的设计简化了电路结构,提高了测温的稳定性及可靠性。
该系统的反应速度快、传输效率高、测量精度高、可靠性高等优点,将会在和传统测温仪器的竞争中脱颖而出,在不久的将来一定会具有广阔的市场前景。
关键词:STC89C52;红外测温;数字显示Digital Display Thermometer Design Dased on single chip microcomputerAiming at disadvantages of the traditional temperature measurement instrument own existence and exposure of difficult to use in real life, the lack of security, this paper proposes a non-contact infrared temperature measurement system design. The system is based on STC89C52 as infrared measuring temperature sensor data transmission and control core. In addition, design the function of liquid crystal displays, buttons and other peripheral modules. The biggest innovation in the system realizes the basic contactless temperature measurement function, its safety, convenience is more advantageous to the use of the common people. The infrared temperature measurement system is designed to simplify the circuit structure, improves the stability and reliability of measuring temperature. The system response speed, high transmission efficiency, high measurement precision, high reliability, etc. Will be in competition and the traditional temperature measurement instruments, in基于单片机的数字显示体温计设计the near future will have broad market prospects.Key words:STC89C52;Infrared Temperature Measurement;Digital Display目录1总设计方案论述 (6)1.1选题的背景及意义 (6)1.2 课题的研究现状与发展趋势 (5)1.2.1 研究现状 (5)1.2.2发展趋势 (6)2红外数字显示测温系统硬件电路设计 (7)2.1红外数字显示测温系统基本原理与构成 (7)2.1.1红外数字显示测温系统基本原理 (7)2.1.2红外数字显示测温系统构成 (7)2.2单片机的主控电路设计 (8)2.2.1STC89C52芯片简介 (8)2.2.2系统主控电路 (9)2.3红外温度传感器模块电路的设计 (10)2.3.1TN901红外测温模块 (10)2.3.2红外测温原理 (10)2.3.3红外测温模块的工作时序 (12)2.3.4 红外测温模块温度值计算 (12)2.3.5 红外测温模块电路 (12)2.4 LCD1602显示电路设计 (13)2.4.1 LCD1602显示器简介 (13)2.4.2 LCD1602显示电路 (14)2.5 按键电路的设计 (15)2.6系统其它硬件电路 (16)2.6.1系统的电源电路 (16)2.6.2系统晶振电路 (16)2.6.3复位电路的设计 (17)3系统软件设计 (18)3.1软件编译KeilC51开发环境 (18)3.2系统软件设计要求及任务 (18)3.3系统主程序流程图 (18)3.4系统子程序流程图 (19)3.4.1功能按键流程图 (19)3.4.2红外测温流程图 (20)参考文献 (21)致谢 (23)附录A (24)附录B (26)基于单片机的数字显示体温计设计1总设计方案论述1.1选题的背景及意义随着经济的发展,社会生活水平的提高,人们对自身身体情况愈来愈重视。
目录1、绪论 (3)2、方案选择2.1、主控芯片选择 (3)2.2、显示模块 (3)2.3、温度检测模块 (4)3、系统硬件设计3.1、51单片机最小系统设计 (4)3.2、电源供电电路设计 (5)3.3、LCD显示电路设计 (6)3.4、温度检测电路设计 (7)4、系统软件设计4.1、温度传感器数据读取流程图 (9)4.2、系统程序设计 (10)5、编程和仿真5.1、Keil编程软件 (11)5.2、proteus (11)5.3、仿真界面 (11)6、总结 (12)7、附录附录1、原理图 (12)附录2、程序清单 (13)1、绪论在信息高速发展的21世纪,科学技术的发展日新月异,科技的进步带动了测量技术的发展,现代控制设备的性能和结构发生了翻天覆地的变化。
我们已经进入了高速发展的信息时代,测量技术也成为当今科技的一个主流,广泛地深入到研究和应用工程的各个领域。
温度和人们的生活息息相关,温度的测量也就变得很重要。
2、系统方案选择2.1 主控芯片选择方案一:STC89C52RCSTC89C52RC是采用8051核的ISP在线可编程芯片,最高工作时钟频率80MHz,片内含8KB的可反复擦写1000次的Flash只读存储器,器件兼容MCS-51指令系统及8051引脚结构,芯片内集成了通用8位中央处理器和ISP Flash 存储单元,具有在线可编程特定,配合PC端的控制程序即可将用户的程序代码下载进单片机内部,省去了购买通用编程器,而且速度更快。
STC89C52RC系列单片机是单时钟周期、高速、低功耗的新一代8051单片机。
方案二:ATmega8ATmega8是ATMAL公司在2002年第一季度推出的一款新型AVR高档单片机。
在AVR家族中,ATmega8是一种非常特殊的单片机,它的芯片内部集成了较大容量的存储器和丰富强大的硬件接口电路,具备AVR高档单片机MEGA系列的全部性能和特点。
但由于采用了小引脚封装(为DIP28和TQFP/MLF32),所以其价格仅与低档单片机相当,再加上AVR单片机的系统内可编程特性,使得无需购买昂贵的仿真器和编程器也可进行单片机嵌入式系统的设计和开发,同时也为单片机的初学者提供了非常方便和简捷的开发环境。
基于单片机的数字温度计的设计摘要温度作为一个重要的物理量,是工业生产过程中最普遍、最常用的测量及被控参数之一。
随着社会的发展、科学技术的不断更新,温度的测量范围要求不断扩大,同时温度的测量准确性要求不断提高。
它直接影响燃烧、化学反应、发酵、烘烤、蒸馏、浓度、挤压成形、结晶以及空气流动等物理和化学过程。
温度控制失误就可能引起生产安全、产品质量、产品产量等一系列问题, 因此对温度测量的意义就越来越大。
本文主要介绍了一个基于89S51单片机的测温系统,详细描述了利用数字温度传感器DS18B20开发测温系统的过程,重点对传感器在单片机下的硬件连接,软件编程以及各模块系统流程进行了详尽分析,对各部分的电路也一一进行了介绍,该系统可以方便的实现实现温度采集和显示,并可根据需要任意设定上下限报警温度,它使用起来相当方便,具有精度高、量程宽、灵敏度高、体积小、功耗低等优点,适合于我们日常生活和工、农业生产中的温度测量,也可以当作温度处理模块嵌入其它系统中,作为其他主系统的辅助扩展。
DS18B20与AT89S51结合实现最简温度检测系统,该系统结构简单,抗干扰能力强,适合于恶劣环境下进行现场温度测量,有广泛的应用前景。
本文主要包括两个方面的设计:一方面是硬件电路的设计,另一方面是系统程序的设计。
其中,硬件电路各部分组成如下:中央控制器采用单片机AT89S51,温度检测部分采用DS18B20温度传感器,用LCD液晶1602作为显示器。
其次,系统软件程序包括主程序,温度值读取、转换及计算程序,显示数据刷新程序等。
该温度计整体功能的实现是通过温度传感器DS18B20采集温度信号送该给单片机处理,单片机再把温度数据送液晶显示器1602显示。
关键词:单片机;温度传感器;DS18B20;LCD液晶显示器THE DESEGN OF THE ELECTRONIC THERMOMETERBASED ON MICROCONTROLLERAbstractTemperature as an important physics, is the most common industrial production process, the most commonly used one of measuring and controlled parameter. With the development of society, the science and technology unceasing renewal, temperature measurement range requirements, at the same time expanding temperature measurement accuracy requirement enhances unceasingly. It directly affects combustion, chemical reaction, fermentation, roasted, distillation, concentration, extrusion, crystallization and air flow etc physical and chemical processes. Temperature control error could cause a production safety, product quality, product yield and so on. So the significance of temperature measurement is more and more big.This paper describes the digital thermometer which is consisted of the central controller, temperature detectors, monitors and police components.The thermometer’s basic range is from -50 ℃ to 110 ℃,its accuracy error is less tha n 0.5 ℃ and LCD directly shows numerical temperature. When the measured temperature exceed the limit alarm doors, the system will output alarm signal.The paper includes tow aspects of the design: one is hardware circuit design, the other is the system process design. Among them, the hardware components are as following: the central controller SCM AT89S51,the temperature detection DS18B20 part of a temperature sensor,and LCD monitors, as LCD 1602 F; the system software programs, including the main program, the temperature reading, conversion and calculation procedures, the data show that refresh procedures and so on.Temperature Sensor DS18B20 acquisition of the temperature signal sent to SCM for processing, SCM then the temperature data sent 1602 F LCD display.Key words: temperature sensor;DS18B20;LCD liquid crystal display目录1 绪论 (4)1.1 设计背景及目的 (5)1.2 设计研究方法 (5)1.3 设计构成及内容 (7)2 数字温度计硬件电路的实现 (8)2.1 单片机的选取和介绍 (8)2.1.1 引脚描述 (8)2.1.2 特殊功能寄存器与存储结构 (10)2.1.3 编写方法 (11)2.2 复位电路及晶振电路设计 (12)2.2.1 复位电路 (12)2.2.2 晶振电路 (12)2.3 温度传感器设计 (12)2.3.1 DS18B20的外形及内部结构 (12)2.3.2 DS18B20工作原理 (15)2.3.3 DS18B20的外部电路图 (15)2.4 液晶显示电路设计 (16)2.4.1 1602的引脚功能介绍 (16)2.4.2 1602液晶模块内部的控制指令 (17)2.4.3 1602的外部电路图 (18)3 数字温度计软件设计流程 (18)3.1 主程序设计 (19)3.2 温度值读取程序设计 (20)3.3 计算温度程序设计 (21)3.4 显示数据及刷新程序设计 (21)4 仿真调试 (22)5 结论 (25)致谢 (26)参考文献 (27)附录 (29)附录A 原理图 (29)附录B 源程序 (29)附件附件1 开题报告(文献综述)附件2 译文及原文影印件1 绪论1.1 设计背景及目的传统的温度计有反应速度慢、读数麻烦、测量精度不高、误差大等缺点,本文介绍了一种基于 DS18B20 的数字温度计设计方案。
基于单片机的数字温度计设计1引言随着现代信息技术的飞速发展和传统工业改造的逐步实现.能够独立工作的温度检测和显示系统应用于诸多领域。
传统的温度检测以热敏电阻为温度敏感元件。
热敏电阻的成本低,但需后续信号处理电路,而且可靠性相对较差,测温准确度低,检测系统也有一定的误差。
与传统的温度计相比,这里设计的数字温度计具有读数方便,测温范围广,测温精确,数字显示,适用范围宽等特点。
选用AT89C51型单片机作为主控制器件,DSl8B20作为测温传感器通过4位共阳极LED数码管串口传送数据,实现温度显示。
通过DSl8B20直接读取被测温度值,进行数据转换,该器件的物理化学性能稳定,线性度较好,在0℃~100℃最大线性偏差小于0.1℃。
该器件可直接向单片机传输数字信号,便于单片机处理及控制。
另外,该温度计还能直接采用测温器件测量温度,从而简化数据传输与处理过程。
2 系统硬件设计方案根据系统功能要求,构造图1所示的系统原理结构框图。
图1 系统原理结构框图2.1单片机的选择AT89C51作为温度测试系统设计的核心器件。
该器件是INTEL公司生产的MCS一5l系列单片机中的基础产品,采用了可靠的CMOS工艺制造技术,具有高性能的8位单片机,属于标准的MCS—51的CMOS产品。
不仅结合了HMOS的高速和高密度技术及CHMOS 的低功耗特征,而且继承和扩展了MCS —48单片机的体系结构和指令系统。
单片机小系统的电路图如图2所示。
图2 单片机小系统电路AT89C51单片机的主要特性:(1)与MCS-51 兼容,4K 字节可编程闪烁存储器;(2)灵活的在线系统编程,掉电标识和快速编程特性;(3)寿命为1000次写/擦周期,数据保留时间可10年以上;(4)全静态工作模式:0Hz-33Hz ;(5)三级程序存储器锁定;(6)128*8位内部RAM ,32可编程I/O 线;(7)两个16位定时器/计数器,6个中断源;(8)全双工串行UART 通道,低功耗的闲置和掉电模式;(9)看门狗(WDT )及双数据指针;(9)片内振荡器和时钟电路;2.2 温度传感器介绍DS18B20可以程序设定9~12位的分辨率,精度为±0.5°C 。
哈尔滨理工大学学士学位论文基于单片机的数字温度计设计摘要随着国民经济的发展,人们需要对各中加热炉、热处理炉、反应炉和锅炉中温度进行监测和控制。
采用单片机来对他们控制不仅具有控制方便,简单和灵活性大等优点,而且可以大幅度提高被控温度的技术指标,从而能够大大的提高产品的质量和数量。
在日常生活及工业生产过程中,经常要用到温度的检测及控制,温度是生产过程和科学实验中普遍而且重要的物理参数之一。
在生产过程中,为了高效地进行生产,必须对它的主要参数,如温度、压力、流量等进行有效的控制。
温度控制在生产过程中占有相当大的比例。
温度测量是温度控制的基础,技术已经比较成熟。
传统的测温元件有热电偶和二电阻。
而热电偶和热电阻测出的一般都是电压,再转换成对应的温度,这些方法相对比较复杂,需要比较多的外部硬件支持。
我们用一种相对比较简单的方式来测量。
我们采用美国DALLAS半导体公司继DS18B20之后推出的一种改进型智能温度传感器DS18B20作为检测元件,温度范围为-55~125 ºC,最高分辨率可达0.0625 ºC。
DS18B20可以直接读出北侧温度值,而且采用三线制与单片机相连,减少了外部的硬件电路,具有低成本和易使用的特点。
本文介绍一种基于AT89C51单片机的一种温度测量及报警电路,该电路采用DS18B20作为温度监测元件,测量范围0℃-~+100℃,使用LED模块显示,能设置温度报警上下限。
正文着重给出了软硬件系统的各部分电路,介绍了集成温度传感器DS18B20的原理,AT89C51单片机功能和应用。
该电路设计新颖、功能强大、结构简单。
关键词:温度测量;DS18B20;AT89C51- I -哈尔滨理工大学学士学位论文Design of Digital Thermomer Based on SCMAbstractAlong with national economy development, the people need to each heating furnace、the heat-treatment furnace、in the reactor and the boiler the temperature carry on the monitor and the control. Not only uses the monolithic integrated circuit to come to them to control has the control to be convenient, simple and flexibility big and so on merits, moreover may enhance large scale is accused the temperature technical specification, thus can big enhance the product the quality and quantity.In daily life and industrial production process, often used in the detection and control of temperature, temperature is the production process and scientific experiments in general and one of the important physical parameter. In the production process, in order to efficiently carry out the production, to be its main parameters, such as temperature, pressure, flow control, etc... Temperature control in the production process of a large proportion. Temperature measurement is the basis of temperature-controlled, more mature technology.Traditional thermocouple and temperature components are the second resistor.The thermocouple and thermal resistance are generally measured voltage, and then replaced by the corresponding temperature, these methods are relatively complex, requiring a relatively large number of external hardware support. We use a relatively simple way to measure.We use the United States following DALLAS Semiconductor DS1820 improved after the introduction of a smart temperature sensor DS18B20 as the detection element, a temperature range of -55 ~ 125 ºC, up to a maximum resolution of 0.0625 ºC. DS18B20 can be directly read out the temperature on the north side, and three-wire system with single-chip connected to a decrease of the external hardware circuit, with low-cost and easy use.The introduction of a cost-based AT89C51 MCU a temperatur measurement circuits, the circuits used DS18B20 high-precision temperatur sensor, measuring scope 0℃-~+100℃,can set the warning limitation, the use of seven segments LED that can be display the current temperature. The paper focuses on providing a software and hardware system components circuit, introduced the theory of DS18B20, the founctions and applications of AT89C51 .This circuit design innovative, powerful, can be expansionary strong.Keywords:Temperatur measurement;DS18B20; AT89C51- II -哈尔滨理工大学学士学位论文目录摘要......................................... . (I)Abstract (II)第1章绪论 (5)1.1课题背景及研究意义 (5)1.2国内外现状 (5)1.3课题的设计目的 (6)1.4课题的主要工作 (6)1.5本文研究内容 (6)第2章开发工具Proteus与Keil (8)2.1 Proteus软件 (8)2.1.1 Proteus简介 (8)2.1.2 4大功能模块 (8)2.1.3 ISIS智能原理图输入系统 (10)2.1.4 Proteus简单应用 (10)2.2 Keil软件 (11)2.2.1 Keil软件简介 (11)2.2.2 Keil软件调试功能 (11)2.3本章小结 (12)第3章系统概述 (13)3.1方案选择 (13)3.1.1方案一 (13)3.1.2方案二 (14)3.2系统设计原理 (14)3.3系统组成 (14)3.4 DS18B20温度传感器与单片机的接口电路 (15)3.5本章小结 (16)第4章系统硬件设计 (17)4.1 80C51单片机的介绍 (17)4.1.1 80C51单片机主要特性 (18)4.1.2 80C51单片机管脚图 (19)4.1.3 80C51单片机的中断系统 (21)4.1.4 80C51单片机的定时/计数器 (21)4.2 LCD液晶显示器简介 (21)4.2.1液晶模块简介 (22)4.2.2液晶显示部分与89C51的接口 (23)- III -哈尔滨理工大学学士学位论文4.3通讯模块 (24)4.4 DS18B20介绍 (25)4.4.1温度传感器工作原理 (25)4.4.2 DS18B20相关介绍 (27)4.4.3 DS18B20使用中的注意事项 (28)4.5本章小结 (28)第5章系统软件设计 (30)5.1主程序设计 (30)5.2 DS18B20初始化 (31)5.3数据测试 (32)5.4仿真结果 (32)5.5本章小结 (34)结论 (35)致谢.............................................................................................. 错误!未定义书签。
基于单片机的数字温度计的设计引言随着人们生活水平的不断提高,单片机控制无疑是人们追求的目标之一,它所给人带来的方便也是不可否定的,其中数字温度计就是一个典型的例子,但人们对它的要求越来越高,要为现代人工作、科研、生活、提供更好的更方便的设施就需要从数单片机技术入手,一切向着数字化控制,智能化控制方向发展。
本设计所介绍的数字温度计与传统的温度计相比,具有读数方便,测温范围广,测温准确,其输出温度采用数字显示,主要用于对测温比较准确的场所,或科研实验室使用,该设计控制器使用单片机AT89S52,测温传感器使用DS18B20,用4位共阳极LED数码管以串口传送数据,实现温度显示,能准确达到以上要求。
总体设计方案考虑到用温度传感器,在单片机电路设计中,大多都是使用传感器,所以这是非常容易想到的,所以可以采用一只温度传感器DS18B20,此传感器,可以很容易直接读取被测温度值,进行转换,就可以满足设计要求。
温度计电路设计总体设计方框图如下图所示,控制器采用单片机AT89S52,温度传感器采用DS18B20,用4位LED数码管以串口传送数据实现温度显示。
总体设计方框图主控制器单片机AT89S52具有低电压供电和体积小等特点,四个端口只需要两个口就能满足电路系统的设计需要,很适合便携手持式产品的设计使用系统可用二节电池供电。
显示电路显示电路采用4位共阳LED数码管,从P3口RXD,TXD串口输出段码。
温度传感器DS18B20温度传感器是一种智能温度传感器,与传统的热敏电阻等测温元件相比,它能直接读出被测温度,并且可根据实际要求通过简单的编程实现9~12位的数字值读数方式。
DS18B20的性能特点如下:●独特的单线接口仅需要一个端口引脚进行通信;●多个DS18B20可以并联在惟一的三线上,实现多点组网功能;●无须外部器件;●可通过数据线供电,电压范围为3.0~5.5V;●零待机功耗;●温度以9或12位数字;●用户可定义报警设置;●报警搜索命令识别并标志超过程序限定温度(温度报警条件)的器件;●负电压特性,电源极性接反时,温度计不会因发热而烧毁,但不能正常工作;DS18B20采用3脚PR -35封装或8脚SOIC 封装,其内部结构框图如下图所示。
基于单片机的数字温度计软件设计系统程序主要包括主程序,读出温度子程序,温度转换命令子程序,计算温度子程序,显示数据刷新子程序等。
从DS18B20读出的二进制值必须先转换成十进制值,才能用于字符的显示DS18B20的转换精度为9~12位可选,为了提高精度采用12位。
温度寄存器里的值是以0.0625为进步的,计温度值为温度寄存器里的二进制值成一0.0625,就是实际的十进制温度值。
主要程序设计如下:1.主程序的主要功能是负责温度的实时显示、读出并处理DS18B20的测量的当前温度值,温度测量每1s 进行一次。
这样可以在一秒之内测量一次被测温度,其程序流程见图1所示。
2.读出温度子程序的主要功能是读出RAM 中的9字节,在读出时需进行CRC 校验,校验有错时不进行温度数据的改写。
,程序流程图如图2所示。
3.温度转换命令子程序主要是发温度转换开始命令,当采用12位分辨率时转换时间约为750ms ,在本程序设计中采用1s 显示程序延时法等待转换的完成,程序流程图如图3所示。
4. 计算温度子程序将RAM 中读取值进行BCD 码的转换运算,并进行温度值正负的判定,其程序流程图如图4-4所示。
5.显示数据刷新子程序主要是对显示缓冲器中的显示数据进行刷新操作,当最高显示位为0时将符号显示位移入下一位。
程序流程图如图5。
图1 主程序流程图 图2 读温度流程图图3 温度转换流程图图4 计算温度流程图 图5显示数据刷新流程图实现控制源程序:#include<reg51.h>#include<intrins.h> //_延时函数用nop_()函数 #define Disdata P1 //段码输出口 #define discan P3 //扫描口 #define uchar unsigned char #define uint unsigned intsbit DQ=P3^7; //温度输入口sbit DIN=P1^0; //LED 小数点控制//*******温度小数部分用查表法**********//uchar code ditab[16]={0x00,0x01,0x01,0x02,0x03,0x03,0x04,0x04,0x05,0x06,0x06,0x07,0x08,0x08,0x09,0x09};uchar code dis_7[13]={0x03,0x9f,0x25,0x0d,0x99,0x49,0x41,0x1f,0x01,0x09,0xff,0xfd,0x00};/* 共阳LED段码表"0" "1" "2" "3" "4" "5" "6" "7" "8" "9" "不亮" "-" "全亮"*/ uchar code scan_con[4]={0xfe,0xfd,0xfb,0xf7}; // 列扫描控制字uchar data temp_data[2]={0x00,0x00}; // 读出温度暂放uchar data display[5]={0x00,0x00,0x00,0x00,0x00};//显示单元数据,共4个数据,一个运算暂存用/////**************11us延时函数**************/void delay(uint t){for(;t>0;t--);}/**************显示扫描函数**************/scan(){char k;for(k=0;k<4;k++)//4位LED扫描控制{disdata=dis_7[display[k]];//数据显示if(k==1){DIN=0;//小数点显示}discan=scan_con[k];delay(300);}}/*************总线复位函数**************/ow_reset(void){char flag=1;while(flag){while(flag){DQ=1;_nop_();_nop_();//稍延时2usDQ=0;//DQ复位,从高拉到低delay(50);//延时550usDQ=1;delay(6);//延时66usflag=DQ;//flag=0复位成功,继续下一步}}DQ=1;// 拉高电平}/************向18B20写命令函数*************///向1-wire总线(P2.7口线)上写一字节void write_byte(uchar val){uchar i;for(i=8;i>0;i--){DQ=1;_nop_();_nop_();//稍延时2usDQ=0;//DQ复位,从高拉到低_nop_();_nop_();_nop_();_nop_();_nop_();//延时5usDQ=val&0x01;//最低位移出delay(6);//延时66usval=val/2;//右移一位}DQ=1;delay(1);//延时11us}/**************读一字节函数**************///从1-wire总线(P2.7口线)上读取一字节uchar read_byte(void){uchar i;uchar value=0;for(i=8;i>0;i--){DQ=1;_nop_();_nop_();//稍延时2usvalue>>=1;DQ=0;_nop_();_nop_();_nop_();_nop_();////稍延时4usDQ=1;_nop_();_nop_();_nop_();_nop_();//稍延时4usif(DQ) value|=0x80;delay(6);//延时66us}DQ=1;return(value);}/**************读出温度函数**************/uint read_temp()delay(200);//延时2.2mswrite_byte(0xcc);//发Skip(跳过) ROM命令write_byte(0x44);//发启动一次温度转换命令ow_reset();delay(1);//延时11uswrite_byte(0xcc);//发Skip(跳过) ROM命令write_byte(0xbe);//发读命令temp_data[0]=read_byte();//读温度值的低8位temp_data[1]=read_byte();//读温度值的高8位temp=temp_data[1];temp<<=8;temp=temp|temp_data[0];//两个字节合成一个整型变量return temp;//返回温度值}/************温度数据处理函数************/work_temp(uint tem){uchar s=0;if(tem>4095)//温度值正负判断{tem=65536-tem;s=1;}//负温度求补码,标志位置1display[4]=tem&0x0f;//取小数部分的值display[0]=ditab[display[4]];//存入小数部分显示值display[4]=tem>>4;//取中间8位,即整数部分的值display[3]=display[4]/100;//display[1]=display[4]%100;//取后两位数据暂存display[2]=display[1]/10;//存入十位display[1]=display[1]%10;//存入个位//符号位显示判断if(! display[3]){display[3]=0x0a;//最高位为0时不显示if(!display[2]){display[2]=0x0a;//次高位为0时不显示}}if(s){display[3]=0x0b;}//负温度时最高位显示"-"}/*****************主函数*****************/void main()discan=0xff;for(h=0;h<4;h++){display[h]=0;}//开机显示"0000"ow_reset();//开机先转换一次write_byte(0xcc);//发Skip(跳过) ROM命令write_byte(0x44);//发转换命令for(h=0;h<100;h++){scan();}//开机显示"0000"2swhile(1){work_temp(read_temp());//处理从18B20读出的温度数据scan();//显示温度值}}。
基于51单片机的数字温度计实现数字温度计是一种能够精确测量温度的仪器,利用数字技术将温度传感器测得的模拟信号转换成数字信号,以显示出实时温度数值。
在这个任务中,我们要使用51单片机来实现一个基于数字技术的温度计。
首先,我们需要一种温度传感器,常用的有热敏电阻传感器和数字温度传感器。
在这里,我们选择使用热敏电阻传感器,因为它价格低廉且性能稳定。
热敏电阻传感器的电阻值会随着温度的变化而发生改变,我们可以利用这个特性来测量温度。
51单片机是一种常用的微型计算机芯片,具有强大的计算和控制能力,非常适合用于实现温度计。
我们可以将热敏电阻传感器连接到单片机的模拟输入引脚上,通过读取引脚上的电压值来获取温度值。
接下来,我们需要编写51单片机的程序来实现温度的转换和显示。
首先,我们需要将模拟信号通过模数转换器(ADC)转换成数字信号。
然后,我们可以通过一定的算法将数字信号转换成对应的温度数值。
为了实现温度的显示,我们可以连接一个LCD液晶屏到51单片机上。
LCD屏幕可以显示数字和字符,我们可以在屏幕上显示实时的温度数值。
除了温度的显示,我们还可以添加一些功能来增强温度计的实用性。
例如,我们可以设置一个温度报警功能,在温度超过一定阈值时发出声音或亮起警示灯。
这样可以方便用户随时知晓温度是否正常。
另外,我们还可以为温度计添加保存数据的功能。
通过连接一个存储器芯片,我们可以将温度数据存储下来,方便后续分析和查看。
总之,基于51单片机的数字温度计实现可以通过连接热敏电阻传感器并编写相应的程序来实现温度的测量和显示。
通过添加额外的功能,例如温度报警和数据存储,可以增强温度计的实用性和功能性。
这样的温度计可以广泛应用于室内温度检测、工业控制和科研实验等领域。
基于单片机的数字温度计c程序(Microcontroller based digitalthermometer C program)#包括<< reg52。
”#包括<<信息。
”#定义unsigned char函数# unsigned int定义单元#定义delaynop() { _nop_();_nop_();_nop_();_nop_();}公司^ DQ=P2 2;普通双= P2 ^ 6;/ /定义锁存器锁存端普通焊接= P2 ^ 7;普通RS = P3 ^ 5;/ /定义1602液晶RS端公司lcden = P3 ^ 4;/ /定义1602液晶lcden端P3 ^了S1 = 0;//定义按键--功能键公司^ S2 = P3 1;/ /定义按键--增加键P3 ^了S3 = 2;//定义按键--减小键P3 ^了S4 = 6;//闹钟查看键P3 ^了RD = 7;普通哔= P2 ^ 3;/ /定义蜂鸣器端函数代码temp_disp_title [ ] = {“当前温度:“};函数current_temp_display_buffer [ ] = {“临时”};函数代码alarm_temp [ ] = {“报警温度高低”};函数alarm_hi_lo_str [ ] = {“嗨:Lo:“};函数temp_data [ 2 ] = { 0x00,0x00 };函数temp_alarm [ 2 ] = { 0x00,0x00 };函数显示[ 5 ] = { 0x00,0x00,0x00,0x00,0x00 };函数显示[ 3 ] = { 0x00,0x00,0x00 };函数代码df_table [ ] = { 0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9 };函数,= 0;//当前读取的温度整数部分函数temp_value [ ] = { 0x00,0x00 };//从DS18B20读取的温度值函数display_digit [ ] = { 0,0,0,0 };//待显示的各温度数位点ds18b20_is_ok = 1;//传感器正常标志/ / ------------------------------------- 延时/ / 1/ / ------------------------------------- 无效的极限(个X){我的函数;当(x)为(i=0;i < 200;i +);}/ / ------------------------------------- 延时/ / 2/ / ------------------------------------- 虚空Delay(个X){(x);}/ / ------------------------------------/ /忙检查/ / ------------------------------------无效write_com(UCHAR COM)/液晶写命令函数{RS=0;lcden = 0;P0 = COM;极限(5);lcden = 1;极限(5);lcden = 0;}write_lcd_data(void函数日期)/液晶写数据函数{RS=1;lcden = 0;P0 =日期;极限(5);lcden = 1;极限(5);lcden = 0;}/ / -----------------------------/ /设置液晶显示位置/ / --------------------------------- 无效set_disp_pos(uchar Pos){write_com(POS | 0x80);}/ / -----------------------------/ /液晶初始化/ / ---------------------------------无效initialize_lcd(){函数数;RD = 0;//软件将矩阵按键第4列一端置低用以分解出独立按键班杜拉= 0;//关闭两锁存器锁存端,防止操作液晶时数码管会出乱码维拉= 0;lcden = 0;write_com(0X38);/ /初始化1602液晶write_com(0x0c);write_com(0x06);write_com(0x01);write_com(0x80);/ /设置显示初始坐标对于(num = 0;数<14;数+ +)/显示年月日星期{write_lcd_data(temp_disp_title [民]);极限(5);}}/ / -------------------------------------/ /函数功能:初始化DS18B20/ /出口参数:现状——DS18B20是否复位成功的标志/ / -------------------------------------init_ds18b20()函数{函数的状态;/ /储存DS18B20是否存在的标志,状态= 0,表示存在;状态= 1,表示不存在DQ=1;Delay(8);/ / / / 6微秒略微延时约先将数据线拉高DQ=0;Delay(90);//再将数据线从高拉低,要求保持480 ~ 960us600 /略微延时约微秒以向DS18B20发出一持续480 ~ 960us的低电平复位脉冲DQ=1;Delay(8);//释放数据线(将数据线拉高)/延时约30us(释放总线后需等待15 ~ 60us让DS18B20输出存在脉冲)状态= DQ;Delay(100);To detect whether the output of the microcontroller / presence pulse (DQ=0 said there was enough long time delay) / /, waiting for the existence of output pulse.DQ=1; / / data lines pulledReturn status; / / return test success}//-------------------------------------/ / function: read a byteExport parameters: dat--- / / read data//-------------------------------------Uchar, ReadOneByte (){Uchar, I, dat=0;DQ=1; (_nop_); / / the first data line up / / wait for a machine cycleFor (i=0; i<8; i++){DQ=0; / / SCM from DS18B20 according to the reading, the data line from high low start read timingDat>>=1;(_nop_); / / wait for a machine cycleDQ=1; / / data line "artificial" pull up, ready for the detection of DS18B20 MCU output level_nop_ (_nop_); (); / / delay of about 6us, so that the host sampling in 15USIf (DQ dat|=0x80); / / if the read data is 1, will be 1 in DAT, if the 0 will remain the original value unchanged.Delay (30); / / 3us delay, there must be more than 1US of the recovery period between two read timingDQ=1; / / data lines pulled, ready for the next data read}Return dat;}//-------------------------------------/ / function: write a byteEntrance parameters: dat--- / / data to be written//-------------------------------------Void WriteOneByte (uchar DAT){Uchar i;For (i=0; i<8; i++){DQ=0; / / data line from high low start write timingDQ=dat & 0x01; / / use and operation to remove a write binary data,/ / and sent to a data line for DS18B20 samplingDelay (5); / / delay of about 30us, sampling from the data line during DS18B20 about 15~60us in the low postDQ=1; / / release the data line.Dat>>=1; / / dat in the 1 bit binary data.}}//-------------------------------------/ / function: read the temperature valueEntrance parameters: / ///-------------------------------------Void, Read_Temperature (){If (Init_DS18B20) (//DS18B20 = = 1) faultDS18B20_IS_OK=0;Else{WriteOneByte (0xCC); / / read serial number column number operation skipWriteOneByte (0x44); / / convert temperature(Init_DS18B20); / / initialize DS18B20WriteOneByte (0xCC); / / read serial number column number operation skipWriteOneByte (0xBE); / / read the temperature register, the first two are low and high temperatureTemp_Value[0]=ReadOneByte (8); / / low temperatureTemp_Value[1]=ReadOneByte (high temperature); / / 8DS18B20_IS_OK=1;}}//-------------------------------------/ / function: display the current temperature in LCD/ / entrance parameters://-------------------------------------Void, Display_Temperature (){Uchar i;/ / delay value and negative identificationUchar, t=150, ng=0;5 all 1 / / high (0xF8) is negative, negative counter plus 1, and set the negative signIf ((Temp_Value[1] & 0xF8) ==0xF8){Temp_Value[1]=~Temp_Value[1];Temp_Value[0]=~Temp_Value[0]+1;If (Temp_Value[0]==0x00 Temp_Value[1]++); / / plus 1 if the low byte is 00 bits that carry, then added to the high byteNg=1; / / the 1 negative sign}Display_Digit[0]=df_Table[Temp_Value[0] & 0x0F]; / / find the decimal part temperature/ / get the temperature (high byte integer part of the lower 3 and the low byte in the high 4 bit unsigned)CurrentT= ((Temp_Value[0] & 0xF0) (>>4) | (Temp_Value[1] & 0x07) <<4);/ / integer part is decomposed into 3 bit digital to displayDisplay_Digit[3]=CurrentT/100; / / 100 digit[CurrentT/100];Display_Digit[2]=CurrentT%100/10; / / tenDisplay_Digit[1]=CurrentT%10; / / A/ / LCD display refresh bufferCurrent_Temp_Display_Buffer[11]=Display_Digit[0]+'0'; / / the first'0' will be converted into the integer 48, then add the digital front,Gets the ASCII character of the corresponding numberCurrent_Temp_Display_Buffer[10]='.';Current_Temp_Display_Buffer[9]=Display_Digit[1]+'0'; / / ACurrent_Temp_Display_Buffer[8]=Display_Digit[2]+'0'; / / tenCurrent_Temp_Display_Buffer[7]=Display_Digit[3]+'0'; / / 100Not to display 0 / / highIf (Display_Digit[3]==0) Current_Temp_Display_Buffer[7]='';For the 0 time and high / high 0 times, do not show highIf (Display_Digit[2]==0 and Display_Digit[3]==0)Current_Temp_Display_Buffer[8]='';/ / negative symbols are displayed in the right position If (NG){If (Current_Temp_Display_Buffer[8]=='')Current_Temp_Display_Buffer[8]='-';Else if (Current_Temp_Display_Buffer[7]=='')Current_Temp_Display_Buffer[7]='-';ElseCurrent_Temp_Display_Buffer[6]='-';}/ / in the first row show titleSet_Disp_Pos (0x00);For (i=0; i<16; i++){Write_LCD_Data (Temp_Disp_Title[i]);}Set_Disp_Pos (0x40); / / in the second line shows the current temperatureFor (i=0; i<16; i++){Write_LCD_Data (Current_Temp_Display_Buffer[i]);}/ / temperature display symbols/ / Set_Disp_Pos (0x4D); Write_LCD_Data (0x00);Set_Disp_Pos (0x4D); Write_LCD_Data (0xdf);Set_Disp_Pos (0x4E); Write_LCD_Data ('C');}//-------------------------------------/ / function: the main function/ / entrance parameters://------------------------------------- Void, main (){Initialize_LCD ();Read_Temperature ();Delay (50000);Delay (50000);While (1){Read_Temperature ();If (DS18B20_IS_OK) Display_Temperature (); Delay1 (100);}}。