当前位置:网站首页>[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
边栏推荐
- numpy之 警告VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences
- pytorch是什么?pytorch是一个软件吗?
- MySQL MAC download and installation tutorial
- Summary of electromagnetic spectrum
- SAP ui5 application development tutorial 105 - detailed introduction to the linkage effect implementation of SAP ui5 master detail layout mode
- [mathematical logic] propositional logic (propositional and connective review | propositional formula | connective priority | truth table satisfiable contradiction tautology)
- 静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
- Nanning water leakage detection: warmly congratulate Guangxi Zhongshui on winning the first famous brand in Guangxi
- User value is the last word in the competition of mobile phone market
- MongoDB主配置文件
猜你喜欢
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Avec trois. JS fait une scène 3D simple
What can learning pytorch do?
How to move towards IPv6: IPv6 Transition Technology - Shangwen network quigo
QQ小程序开发之 一些前期准备:预约开发账号、下载安装开发者工具、创建qq小程序
简易版 微信小程序开发之页面跳转、数据绑定、获取用户信息、获取用户位置信息
Elsevier latex submitted the article pdftex def Error: File `thumbnails/cas-email. jpeg‘ not found: using draf
Wechat applet + Alibaba IOT platform + Hezhou air724ug built with server version system analysis
docker安装及启动mysql服务
js中#号的作用
随机推荐
PHP generates PDF tcpdf
403 error displayed when vs cloning
pytorch怎么下载?pytorch在哪里下载?
@The difference between Autowired, @qualifier, @resource
Download and install node, NPM and yarn
如何迈向IPv6之IPv6过渡技术-尚文网络奎哥
Hutool dynamically adds scheduled tasks
可分离债券与可转债
[combinatorics] brief introduction to generating function (definition of generating function | Newton binomial coefficient | commonly used generating function | correlation with constant | correlation
[learning notes] seckill - seckill project - (11) project summary
释放数据力量的Ceph-尚文网络xUP楠哥
[mathematical logic] propositional logic (judgment of the correctness of propositional logic reasoning | formal structure is eternal truth - equivalent calculus | deduction from premise - logical reas
[mathematical logic] predicate logic (first-order predicate logic formula | example)
【学习笔记】seckill-秒杀项目--(11)项目总结
Avec trois. JS fait une scène 3D simple
FileZilla Client下載安裝
Applet (continuous update)
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
js中#号的作用
8.8.2-PointersOnC-20220214