当前位置:网站首页>[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
[Blue Bridge Road -- bug free code] DS18B20 temperature reading code analysis
2022-07-03 03:49:00 【The journey of a bald girl is the sea of stars】
adopt DS18B20 Measure the outside temperature and display it on the nixie tube , The temperature value should be accurate to the decimal point 2 position
main.c
#include"STC15F2K60S2.h"
#include"onewire.h"
#include"intrins.h"
typedef unsigned char uchar;
typedef unsigned int uint;
uchar dsp_code[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
uchar dsp_Temp[8]={0xff,0xff,0xff,0xff};
uint temp;
uchar count_temp;
void Timer_Init()
{
AUXR|= 0x80;
TMOD&= 0xf0;
TL0=0xcd;
TH0=0xd4;
TR0=1;
ET0=1;
EA=1;
}
void serviceTimer() interrupt 1
{
static uchar dsp_com=0;
P0=0;P2=0xc0;P2=0;
P0=dsp_Temp[dsp_com];P2=0xe0;P2=0;
P0=1<<dsp_com;P2=0xc0;P2=0;
if(++dsp_com==8)dsp_com=0;
++count_temp;
}
void Initsystem()
{
P0=0xff;P2=0x80;P2=0;
P0=0x00;P2=0xa0;P2=0;
}
void main()
{
char i=0;
Initsystem();
for(i=0;i<70;i++)
{
temp=read_temp()*100+0.5;
}
Timer_Init();
while(1)
{
if(count_temp>759)
{
count_temp=0;
temp=read_temp()*100+0.5;
}
dsp_Temp[4]=dsp_code[temp/1000];
dsp_Temp[5]=dsp_code[temp/100%10]&0x7f;
dsp_Temp[6]=dsp_code[temp/10%10];
dsp_Temp[7]=dsp_code[temp%10];
}
}
onewire.c
/*
Program description : Single bus driver
Software environment : Keil uVision 4.10
Hardware environment : CT107 SCM comprehensive training platform ( External crystal oscillator 12MHz) STC89C52RC Single chip microcomputer
Japan period : 2011-8-9
*/
#include "reg52.h"
#include"onewire.h"
#include"intrins.h"
sbit DQ = P1^4; // Single bus interface
// Single bus delay function
void Delay_OneWire(unsigned int t); //STC89C52RC
// Through a single bus to DS18B20 Write a byte
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
// from DS18B20 Read a byte
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
//DS18B20 Device initialization
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
void Delay_Onewire(unsigned int t)
{
t*=11;
while(t--);
}
float read_temp()
{
float temp;
unsigned char low,high;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
Delay_OneWire(200);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
low=Read_DS18B20();
high=Read_DS18B20();
temp=(high<<8|low)*0.0625;
return temp;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
float read_temp();
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Delay_OneWire(unsigned int t);
void Write_DS18B20(unsigned char dat);
unsigned char rd_temperature(void); //; ;
#endif
边栏推荐
- shardingsphere动态数据源
- docker安装及启动mysql服务
- 静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
- Is pytorch difficult to learn? How to learn pytorch well?
- Makefile demo
- [mathematical logic] predicate logic (first-order predicate logic formula | example)
- 递归使用和多维数组对象变一维数组对象
- Open Visual Studio 2010 hangs when opening a SQL file sql file
- Mongodb master profile
- 2022年已过半,得抓紧
猜你喜欢
2022 tea master (intermediate) examination questions and analysis and tea master (intermediate) practical examination video
Ansible introduction [unfinished (semi-finished products)]
NPM: the 'NPM' item cannot be recognized as the name of a cmdlet, function, script file, or runnable program. Please check the spelling of the name. If the path is included, make sure the path is corr
Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
Message queue addition failure
Makefile demo
Wechat applet + Alibaba IOT platform + Hezhou air724ug build a serverless IOT system (III) -- wechat applet is directly connected to Alibaba IOT platform aliiot
Docker install and start MySQL service
学会pytorch能干什么?
Is pytorch difficult to learn? How to learn pytorch well?
随机推荐
Recursion: one dimensional linked lists and arrays
ffmpeg录制屏幕和截屏
MongoDB复制集【主从复制】
CEPH Shangwen network xUP Nange that releases the power of data
Simple wechat applet development page Jump, data binding, obtaining user information, obtaining user location information
Hutool dynamically adds scheduled tasks
Ffmpeg download and installation tutorial and introduction
Mongodb replication set [master-slave replication]
小程序获取用户头像和昵称
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
阿洛对自己的思考
动态规划:最长公共子串和最长公共子序列
Tidal characteristics of the Bohai Sea and the Yellow Sea
动态规划:最长回文子串和子序列
105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍
Read a paper_ ChineseBert
PHP generates PDF tcpdf
Bid farewell to artificial mental retardation: Mengzi open source project team received RMB 100 million financing to help NLP develop
Web session management security issues
SAP UI5 应用开发教程之一百零五 - SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍