当前位置:网站首页>Microcontroller: temperature control DS18B20
Microcontroller: temperature control DS18B20
2022-08-05 10:17:00 【first good morning...】
题目概述:
使用单片机DS18B20进行温度检测.
编程:
//DS18B20Temperature detection and its LCD display
#include<reg51.h> //包含单片机寄存器的头文件
#include<intrins.h> //包含_nop_()函数定义的头文件
unsigned char code digit[10]={
"0123456789"}; //定义字符数组显示数字
unsigned char code Str[]={
"Test by DS18B20"}; //The description shows the temperature
unsigned char code Error[]={
"Error!Check!"}; //说明没有检测到DS18B20
unsigned char code Temp[]={
"Temp:"}; //The description shows the temperature
unsigned char code Cent[]={
"Cent"}; //温度单位
volatile unsigned char sending;
sbit beep=P2^3;
sbit dula=P2^0;
sbit wela=P2^1;
sbit JD=P0^6;
sbit FM=P2^3;
sbit P20 = P2^0;//This is to close the actual application of the digital tube on the development board
sbit P21 = P2^1;//This is to close the actual application of the digital tube on the development board
sbit CS88 = P2^2;//This is to close the actual application of the dot matrix on the development board
void ioin() //IOport relay、Digital tube latch function
{
wela=1;
P0 = 1;
JD = 0;
wela=0;
dula=1;
P0=0;
dula=0;
}
/******************************************************************************* The following is the operating procedure for the LCD module *******************************************************************************/
sbit RS=P1^0; //寄存器选择位,将RS位定义为P2.0引脚
sbit RW=P1^1; //读写选择位,将RW位定义为P2.1引脚
sbit E=P2^5; //使能信号位,将E位定义为P2.2引脚
sbit BF=P0^7; //忙碌标志位,,将BF位定义为P0.7引脚
/***************************************************** 函数功能:延时1ms (3j+2)*i=(3×33+2)×10=1010(微秒),可以认为是1毫秒 ***************************************************/
void delay1ms()
{
unsigned char i,j;
for(i=0;i<4;i++)
for(j=0;j<33;j++)
;
}
/***************************************************** 函数功能:延时若干毫秒 入口参数:n ***************************************************/
void delaynms(unsigned char n)
{
unsigned char i;
for(i=0;i<n;i++)
delay1ms();
}
/***************************************************** 函数功能:判断液晶模块的忙碌状态 返回值:result.result=1,忙碌;result=0,不忙 ***************************************************/
bit BusyTest(void)
{
bit result;
RS=0; //根据规定,RS为低电平,RW为高电平时,Status can be read
RW=1;
E=1; //E=1,Only read and write are allowed
_nop_(); //空操作
_nop_();
_nop_();
_nop_(); //Four machine cycles of no-op,给硬件反应时间
result=BF; //Assign the busy flag level toresult
E=0; //将E恢复低电平
return result;
}
/***************************************************** 函数功能:Write the mode setting command or display address into the LCD module 入口参数:dictate ***************************************************/
void WriteInstruction (unsigned char dictate)
{
while(BusyTest()==1); //如果忙就等待
RS=0; //根据规定,RS和R/WWhen both are low,Commands can be written
RW=0;
E=0; //E置低电平(根据表8-6,写指令时,Efor the high pulse,
// 就是让E从0到1A positive transition occurs,So it should be set first"0"
_nop_();
_nop_(); //No-op for two machine cycles,给硬件反应时间
P0=dictate; //将数据送入P0口,That is, write instructions or addresses
_nop_();
_nop_();
_nop_();
_nop_(); //Four machine cycles of no-op,给硬件反应时间
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_(); //Four machine cycles of no-op,给硬件反应时间
E=0; //当EWhen transitioning from high to low,液晶模块开始执行命令
}
/***************************************************** 函数功能:指定字符显示的实际地址 入口参数:x ***************************************************/
void WriteAddress(unsigned char x)
{
WriteInstruction(x|0x80); //显示位置的确定方法规定为"80H+地址码x"
}
/***************************************************** 函数功能:将数据(character standardASCII码)Write to the LCD module 入口参数:y(为字符常量) ***************************************************/
void WriteData(unsigned char y)
{
while(BusyTest()==1);
RS=1; //RS为高电平,RW为低电平时,可以写入数据
RW=0;
E=0; //E置低电平(根据表8-6,写指令时,Efor the high pulse,
// 就是让E从0到1A positive transition occurs,So it should be set first"0"
P0=y; //将数据送入P0口,Write data to the LCD module
_nop_();
_nop_();
_nop_();
_nop_(); //Four machine cycles of no-op,给硬件反应时间
E=1; //E置高电平
_nop_();
_nop_();
_nop_();
_nop_(); //Four machine cycles of no-op,给硬件反应时间
E=0; //当EWhen transitioning from high to low,液晶模块开始执行命令
}
/***************************************************** 函数功能:对LCD的显示模式进行初始化设置 ***************************************************/
void LcdInitiate(void)
{
delaynms(15); //延时15ms,Should be given when writing the command for the first timeLCDa longer reaction time
WriteInstruction(0x38); //显示模式设置:16×2显示,5×7点阵,8位数据接口
delaynms(5); //延时5ms ,Give the hardware a little time to react
WriteInstruction(0x38);
delaynms(5); //延时5ms ,Give the hardware a little time to react
WriteInstruction(0x38); //连续三次,确保初始化成功
delaynms(5); //延时5ms ,Give the hardware a little time to react
WriteInstruction(0x0c); //显示模式设置:显示开,无光标,光标不闪烁
delaynms(5); //延时5ms ,Give the hardware a little time to react
WriteInstruction(0x06); //显示模式设置:光标右移,The characters do not move
delaynms(5); //延时5ms ,Give the hardware a little time to react
WriteInstruction(0x01); //清屏幕指令,将以前的显示内容清除
delaynms(5); //延时5ms ,Give the hardware a little time to react
}
/************************************************************************ 以下是DS18B20的操作程序 ************************************************************************/
sbit DQ=P2^4;
unsigned char time; //设置全局变量,Specifically used for strict delays
/***************************************************** 函数功能:将DS18B20传感器初始化,读取应答信号 出口参数:flag ***************************************************/
bit Init_DS18B20(void)
{
bit flag; //储存DS18B20是否存在的标志,flag=0,表示存在;flag=1,表示不存在
DQ = 1; //Pull the data line high first
for(time=0;time<2;time++) //Slight delay approx6微秒
;
DQ = 0; //Then pull the data line from high to low,要求保持480~960us
for(time=0;time<200;time++) //Slight delay approx600微秒
; //以向DS18B20Issue a continuation480~960uslow-level reset pulse
DQ = 1; //释放数据线(将数据线拉高)
for(time=0;time<10;time++)
; //延时约30us(Wait after releasing the bus15~60us让DS18B20There is a pulse on the output)
flag=DQ; //Let the microcontroller detect whether a presence pulse is output(DQ=0表示存在)
for(time=0;time<200;time++) //Delay long enough,Wait for the presence of pulse output to complete
;
return (flag); //Returns the detection success flag
}
/***************************************************** 函数功能:从DS18B20读取一个字节数据 出口参数:dat ***************************************************/
unsigned char ReadOneChar(void)
{
unsigned char i=0;
unsigned char dat; //Stores one byte of data read
for (i=0;i<8;i++)
{
DQ =1; // Pull the data line high first
_nop_(); //等待一个机器周期
DQ = 0; //单片机从DS18B20when reading,Pulling the data line from high to low starts the read sequence
dat>>=1;
_nop_(); //等待一个机器周期
DQ = 1; //将数据线"人为"拉高,Detected for single-chip microcomputerDS18B20the output level to prepare
for(time=0;time<2;time++)
; //延时约6us,make the host in15us内采样
if(DQ==1)
dat|=0x80; //If the read data is1,则将1存入dat
else
dat|=0x00;//If the read data is0,则将0存入dat
//The level signal detected by the microcontrollerDQ存入r[i]
for(time=0;time<8;time++)
; //延时3us,There must be a greater than between two read sequences1usrecovery period
}
return(dat); //Returns the read decimal data
}
/***************************************************** 函数功能:向DS18B20写入一个字节数据 入口参数:dat ***************************************************/
WriteOneChar(unsigned char dat)
{
unsigned char i=0;
for (i=0; i<8; i++)
{
DQ =1; // Pull the data line high first
_nop_(); //等待一个机器周期
DQ=0; //The write sequence is initiated when the data line is pulled from high to low
DQ=dat&0x01; //Use AND operation to take out a certain bit of binary data to be written,
//and send it to the data line to waitDS18B20采样
for(time=0;time<10;time++)
;//延时约30us,DS18B20approx. after being pulled low15~60usDuring sampling from the data line
DQ=1; //释放数据线
for(time=0;time<1;time++)
;//延时3us,At least between two write sequences is required1usrecovery period
dat>>=1; //将datThe data in each binary bit is shifted to the right1位
}
for(time=0;time<4;time++)
; //稍作延时,Give the hardware a little time to react
}
/****************************************************************************** Below are the display settings related to temperature ******************************************************************************/
/***************************************************** 函数功能:Shows not detectedDS18B20 ***************************************************/
void display_error(void)
{
unsigned char i;
WriteAddress(0x00); //Write the display address,将在第1行第1列开始显示
i = 0; //Displayed from the first character
while(Error[i] != '\0') //As long as the end marker is not written,就继续写
{
WriteData(Error[i]); //Write character constantsLCD
i++; //指向下一个字符
delaynms(100); //延时100ms较长时间,to see a description of the display
}
while(1) //进入死循环,Waiting to find out why
;
}
/***************************************************** 函数功能:显示说明信息 ***************************************************/
void display_explain(void)
{
unsigned char i;
WriteAddress(0x00); //Write the display address,将在第1行第1列开始显示
i = 0; //Displayed from the first character
while(Str[i] != '\0') //As long as the end marker is not written,就继续写
{
WriteData(Str[i]); //Write character constantsLCD
i++; //指向下一个字符
delaynms(100); //延时100ms较长时间,to see a description of the display
}
}
/***************************************************** 函数功能:Displays the temperature symbol ***************************************************/
void display_symbol(void)
{
unsigned char i;
WriteAddress(0x40); //Write the display address,将在第2行第1列开始显示
i = 0; //Displayed from the first character
while(Temp[i] != '\0') //As long as the end marker is not written,就继续写
{
WriteData(Temp[i]); //Write character constantsLCD
i++; //指向下一个字符
delaynms(50); //延时1msGive the hardware a little time to react
}
}
/***************************************************** 函数功能:Displays the decimal point of the temperature ***************************************************/
void display_dot(void)
{
WriteAddress(0x49); //Write the display address,将在第2行第10列开始显示
WriteData('.'); //Write the decimal point character constantLCD
delaynms(50); //延时1msGive the hardware a little time to react
}
/***************************************************** 函数功能:Displays the unit of temperature(Cent) ***************************************************/
void display_cent(void)
{
unsigned char i;
WriteAddress(0x4c); //Write the display address,将在第2行第13列开始显示
i = 0; //Displayed from the first character
while(Cent[i] != '\0') //As long as the end marker is not written,就继续写
{
WriteData(Cent[i]); //Write character constantsLCD
i++; //指向下一个字符
delaynms(50); //延时1msGive the hardware a little time to react
}
}
/***************************************************** 函数功能:显示温度的整数部分 入口参数:x ***************************************************/
void display_temp1(unsigned char x)
{
unsigned char j,k,l; //j,k,lStore the hundreds of temperatures separately、十位和个位
j=x/100; //取百位
k=(x%100)/10; //取十位
l=x%10; //取个位
WriteAddress(0x46); //Write the display address,将在第2行第7列开始显示
WriteData(digit[j]); //Writes a character constant for the hundreds digitLCD
SBUF=digit[j];
while (!TI); //等待发送数据完成
TI = 0;
WriteData(digit[k]); //Write a ten-digit character constantLCD
SBUF=digit[k];
while (!TI); //等待发送数据完成
TI = 0;
WriteData(digit[l]); //Writes a character constant for the ones digitLCD
SBUF=digit[l];
while (!TI); //等待发送数据完成
TI = 0;
delaynms(50); //延时1msGive the hardware a little time to react
}
/***************************************************** 函数功能:Displays the fractional part of the temperature 入口参数:x ***************************************************/
void display_temp2(unsigned char x)
{
WriteAddress(0x4a); //Write the display address,将在第2行第11列开始显示
WriteData(digit[x]); //Writes the first digit character constant of the fractional partLCD
SBUF='.';
while (!TI); //等待发送数据完成
TI = 0;
SBUF=digit[x];
while (!TI); //等待发送数据完成
TI = 0;
SBUF=' ';
while (!TI); //等待发送数据完成
TI = 0;
delaynms(50); //延时1msGive the hardware a little time to react
}
/***************************************************** 函数功能:Be prepared to read the temperature ***************************************************/
void ReadyReadTemp(void)
{
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
for(time=0;time<100;time++)
; //The temperature conversion takes a little while
Init_DS18B20(); //将DS18B20初始化
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器,The first two are the low and high temperature, respectively
}
#define jingzhen 11059200UL /*使用22.1184M晶体*/
#define botelv 9600UL /*波特率定义为9600*/
void init(void) //串口初始化
{
EA=0; //暂时关闭中断
TMOD&=0x0F; //定时器1模式控制在高4位
TMOD|=0x20; //定时器1工作在模式2,自动重装模式
SCON=0x50; //串口工作在模式1
TH1=256-jingzhen/(botelv*12*16); //计算定时器重装值
TL1=256-jingzhen/(botelv*12*16);
PCON|=0x80; //串口波特率加倍
ES=1; //串行中断允许
TR1=1; //启动定时器1
REN=1; //允许接收
EA=1; //允许中断
}
/***************************************************** 函数功能:主函数 ***************************************************/
void main(void)
{
float tt;
int temp;
unsigned char TL; //Stores the temperature low bit of the scratchpad
unsigned char TH; //Stores the temperature high level of the scratchpad
unsigned char TN; //Integer part of storage temperature
unsigned char TD; //Fractional part of storage temperature
ioin();
init();
LcdInitiate(); //Initialize the LCD
delaynms(5); //延时5msGive the hardware a little time to react
if(Init_DS18B20()==1)
display_error();
display_explain();
display_symbol(); //Displays temperature descriptions
display_dot(); //Displays the decimal point of the temperature
display_cent(); //Displays the unit of temperature
P0=0x00;//This is to close the actual application of the digital tube on the development board
P20=0; //This is to close the actual application of the digital tube on the development board
P21=0; //This is to close the actual application of the digital tube on the development board
CS88=0; //This is to close the actual application of the dot matrix on the development board
while(1) //Constantly detects and displays temperature
{
ReadyReadTemp(); //Read temperature ready
TL=ReadOneChar(); //先读的是温度值低位
TH=ReadOneChar(); //接着读的是温度值高位
TN=TH*16+TL/16; //实际温度值=(TH*256+TL)/16,即:TH*16+TL/16
temp=TH; //这样得出的是温度的整数部分,小数部分被丢弃了
temp<<=8;
temp=temp|TL;
tt=temp*0.0625;
temp=tt*10+0.5;
TD=(TL%16)*10/16; //计算温度的小数部分,将余数乘以10再除以16取整,
//这样得到的是温度小数部分的第一位数字(保留1位小数)
display_temp1(TN); //显示温度的整数部分
display_temp2(TD); //显示温度的小数部分
if(temp>=310) //当温度超过31度(For testing purposes only,It can actually be set to other higher values),The buzzer will alarm.
{
P1=0x00;
beep=0;
}
else
{
beep=1;
P1=0xff;
}
}
}
上机实践:
边栏推荐
- 【AGC】增长服务1-远程配置示例
- IDEA performs the Test operation, resulting in duplicate data when data is inserted
- Microservice Technology Stack
- Confessing in the era of digital transformation: Mai Cong Software allows enterprises to use data in the easiest way
- static linking and dynamic linking
- Egg framework usage (1)
- [Strong Net Cup 2022] WP-UM
- FPGA: Basic Getting Started LED Lights Blinking
- 5. Deploy the web project to the cloud server
- [Office] Collection of Microsoft Office download addresses (offline installation and download of Microsoft's official original version)
猜你喜欢

NowCoderTOP35-40 - continuous update ing

【 temperature warning program DE development 】 event driven model instance

数据中台建设(十):数据安全管理

Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU

What is SPL?

The founder of the DFINITY Foundation talks about the ups and downs of the bear market, and where should DeFi projects go?

首次去中心化抢劫?近2亿美元损失:跨链桥Nomad 被攻击事件分析
![[强网杯2022]WP-UM](/img/3d/caeab05ddca278af274dbf6e2f8ba1.png)
[强网杯2022]WP-UM

高质量 DeFi 应用构建指南,助力开发者玩转 DeFi Summer

mysql索引
随机推荐
第四章:activiti RuntimeService设置获和取流程变量,及与taskService的区别,开始和完成任务时设置流程变量[通俗易懂]
第五章:多线程通信—wait和notify
【 temperature warning program DE development 】 event driven model instance
static linking and dynamic linking
First Decentralized Heist?Loss of nearly 200 million US dollars: analysis of the attack on the cross-chain bridge Nomad
Meteorological data processing example - matlab string cutting matching and R language date matching (data splicing)
IO stream articles -- based on io stream to realize folder copy (copy subfolders and files in subfolders) full of dry goods
Go编译原理系列6(类型检查)
Technical dry goods | Hausdorff distance for image segmentation based on MindSpore
FPGA:开发环境Vivado的使用
七夕浪漫约会不加班,RPA机器人帮你搞定工作
MySQL事务
Huawei's lightweight neural network architecture GhostNet has been upgraded again, and G-GhostNet (IJCV22) has shown its talents on the GPU
上位机开发C#语言:模拟STC串口助手接收单片机发送数据
如何选币与确定对应策略研究
Jenkins使用手册(2) —— 软件配置
2022杭电多校 第6场 1008.Shinobu Loves Segment Tree 规律题
Keil升级到AC6后,到底有哪些变化?
第三章 : redis数据结构种类
PHP 操作mangoDb