当前位置:网站首页>[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
边栏推荐
猜你喜欢
![Ansible introduction [unfinished (semi-finished products)]](/img/2a/0003daf761ba02d8837c4657fc3f29.png)
Ansible introduction [unfinished (semi-finished products)]

MongoDB复制集【主从复制】

In Net 6 project using startup cs

105. SAP UI5 Master-Detail 布局模式的联动效果实现明细介绍

Introduction à mongodb

Download and install captura and configure ffmpeg in captura

2022 tea master (primary) examination questions and tea master (primary) examination question bank

pytorch难学吗?如何学好pytorch?

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

2022 mobile crane driver examination registration and mobile crane driver operation examination question bank
随机推荐
Half of 2022 is over, so we must hurry up
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
Using jasmine to monitor constructors - spying on a constructor using Jasmine
机械臂速成小指南(八):运动学建模(标准DH法)
2022 polymerization process examination questions and polymerization process examination skills
Tidal characteristics of the Bohai Sea and the Yellow Sea
Advanced redis applications [password protection, data persistence, master-slave synchronization, sentinel mode, transactions] [not completed yet (semi-finished products)]
记一次 .NET 差旅管理后台 CPU 爆高分析
Dynamic programming: longest common substring and longest common subsequence
Commands related to the startup of redis under Linux server (installation and configuration)
IPv6过渡技术-6to4手工隧道配置实验--尚文网络奎哥
2020-01-01t00:00:00.000000z date format conversion
Numpy warning visibledeprecationwarning: creating an ndarray from ragged needed sequences
Lvgl usage experience
Docker install and start MySQL service
2022 P cylinder filling examination content and P cylinder filling practice examination video
Ffmpeg recording screen and screenshot
MongoDB基本操作【增、删、改、查】
Use three JS make a simple 3D scene
Mongodb master profile