当前位置:网站首页>[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
边栏推荐
- FileZilla Client下載安裝
- pytorch怎么下载?pytorch在哪里下载?
- In Net 6 project using startup cs
- IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
- 动态规划:最长回文子串和子序列
- Tidal characteristics of the Bohai Sea and the Yellow Sea
- shardingsphere动态数据源
- [combinatorics] brief introduction to generating function (definition of generating function | Newton binomial coefficient | commonly used generating function | correlation with constant | correlation
- 阿洛对自己的思考
- 静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
猜你喜欢

小程序获取用户头像和昵称

Summary of electromagnetic spectrum
![Ansible introduction [unfinished (semi-finished products)]](/img/2a/0003daf761ba02d8837c4657fc3f29.png)
Ansible introduction [unfinished (semi-finished products)]

递归:一维链表和数组

Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences

Download and install node, NPM and yarn

FileZilla Client下载安装

pytorch开源吗?

Some preliminary preparations for QQ applet development: make an appointment for a development account, download and install developer tools, and create QQ applet

Use three JS make a simple 3D scene
随机推荐
[national programming] [software programming - Lecture Video] [zero foundation introduction to practical application]
Applet (continuous update)
Recursive use and multi-dimensional array object to one-dimensional array object
静态网页 和 动态网页的区别 & WEB1.0和WEB2.0的区别 & GET 和 POST 的区别
sigaction的使用
redis在服务器linux下的启动的相关命令(安装和配置)
Positioning (relative positioning, absolute positioning, fixed positioning, Z-index) 2022-2-11
In Net 6 project using startup cs
Download and install node, NPM and yarn
2022 polymerization process examination questions and polymerization process examination skills
403 error displayed when vs cloning
Makefile demo
[mathematical logic] propositional logic (propositional logic reasoning | formal structure of reasoning | inference law | additional law | simplification law | hypothetical reasoning | refusal | disju
学会pytorch能干什么?
Node start server
Change and access of median value of listening object
Ansible introduction [unfinished (semi-finished products)]
Download and install captura and configure ffmpeg in captura
Arlo's thinking about himself
用Three.js做一個簡單的3D場景