单片机温度检测的程序?

单片机温度检测的程序?,第1张

/***************   writer:shopping.w   ******************/

#include <reg51.h>

#include <intrins.h>

#define uint unsigned int

#define uchar unsigned char

sbit DQ = P3^6

uchar code DSY_CODE[] =

{ 0X3F,0X06,0X5B,0X4F,0X66,0X6D,0X7D,0X07,0X7F,0X6F,0X00}

uchar code df_Table[] = {0,1,1,2,3,3,4,4,5,6,6,7,8,8,9,9}

uchar CurrentT = 0

uchar Temp_Value[]={0x11,0x22}

uchar Display_Digit[]={0,0,0,0}

bit DS18B20_IS_OK = 1

void Delay(uint x)

{

while(--x)

}

uchar Init_DS18B20()

{

uchar status

DQ = 1

Delay(8)

DQ = 0

Delay(90)

DQ = 1

Delay(8)

DQ = 1

return status

}

uchar ReadOneByte()

{

uchar i,dat=0

DQ = 1

_nop_()

for(i=0i<8i++)

{

  DQ = 0

dat >>= 1

DQ = 1

_nop_()

_nop_()

if(DQ)

dat |= 0X80

Delay(30)

DQ = 1

}

return dat

}

void WriteOneByte(uchar dat)

{

uchar i

for(i=0i<8i++)

{

  DQ = 0

DQ = dat& 0x01

Delay(5)

DQ = 1

dat >>= 1

}

}

void Read_Temperature()

{

if(Init_DS18B20() ==1 )

DS18B20_IS_OK = 0

else

{

WriteOneByte(0xcc)

WriteOneByte(0x44)

Init_DS18B20()

WriteOneByte(0xcc)

WriteOneByte(0xbe)

Temp_Value[0] = ReadOneByte()

Temp_Value[1] = ReadOneByte()

DS18B20_IS_OK=1

}

}

void Display_Temperature()

{

uchar i

uchar t=150

uchar ng=0, np=0

if ( (Temp_Value[1] & 0xf8) == 0xf8)

{

Temp_Value[1] = ~Temp_Value[1]

Temp_Value[0] = ~Temp_Value[0]+1

if (Temp_Value[0] == 0x00) Temp_Value[1]++

ng=1np=0xfd

}

Display_Digit[0] = df_Table[ Temp_Value[0] & 0x0f ]

CurrentT = ((Temp_Value[0] & 0xf0)>>4) | ((Temp_Value[1] & 0x07)<<4)

Display_Digit[3] = CurrentT / 100

Display_Digit[2] = CurrentT % 100 / 10

Display_Digit[1] = CurrentT % 10

if (Display_Digit[3] == 0)

{

Display_Digit[3] = 10

np = 0xfb

if (Display_Digit[2] == 0)

{

Display_Digit[2] = 10

np = 0xf7

}

}

for (i=0i<30i++)

{

P0=0x39P2=0x7fDelay(t)P2=0xFF

P0=0x63P2=0xbfDelay(t)P2=0xff

P0=DSY_CODE[Display_Digit[0]]

P2=0xDFDelay(t)P2=0xff

P0=(DSY_CODE[Display_Digit[1]]) | 0x80

P2=0xefDelay(t)P2=0xff

P0=DSY_CODE[Display_Digit[2]]

P2=0xf7Delay(t)P2=0xff

P0=DSY_CODE[Display_Digit[3]]

P2=0xfb Delay(t) P2=0xff

if (ng)

{

P0 = 0x40 P2 = np Delay(t) P2=0xff

}

}

}

void main()

{

Read_Temperature()

Delay(50000)

Delay(50000)

while(1)

{

  Read_Temperature()

if(DS18B20_IS_OK)

Display_Temperature()

}

}

这两句话是在读取18B20里面的数据,这个温度传感器是将温度已16个字节上传的,高5位表示的时温度的正负,后面12位表示的是无符号数值。最大精度为0.0625度。第一句是将16字节数据的低8位读出来,第二局是将数据的高8位读出来。

/*

必要 *** 作:连接传感器DS18B20到U6

*/

#pragma db code

#include<AT89X52.H>

#include "INTRINS.H"

#define BUSY1(DQ1==0) //定义busy信号

sbit LED_0=P1^0 //定义数码管控制脚为P1口的0-3脚

sbit LED_1=P1^1

sbit LED_2=P1^2

sbit LED_3=P1^3

sbit DQ1=P3^5 //定义18B20单总线引脚

void display(unsigned char d1,unsigned char d2,unsigned char d3,unsigned char d4)//声明显示函数

void ds_reset_1(void) //声明18B20复位函数

void wr_ds18_1(char dat) //声明18B20写入函数

void time_delay(unsigned char time)//声明延时函数

int get_temp_1(void) //声明18B20读入温度函数

void delay(unsigned int x) //声明延时函数

void read_ROM(void) //声明18B20读ROM函数

int get_temp_d(void) //声明获取温度函数

void ds_init(void) //声明18B20初始化函数

void ds_getT(void) //声明18B20获得温度显示值函数

/*定义数码管段码=====0-9=====A-G=====*/

unsigned char a[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,

0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e}

//共阳极数码管的段码0 1 2 3 4 5 6 7 8 9 A B C D E F

/****************以下定义各种变量********************/

unsigned char ResultSignal

int ResultTemperatureLH,ResultTemperatureLL,ResultTemperatureH

unsigned char ROM[8]

unsigned char idata TMP

unsigned char idata TMP_d

unsigned char f

unsigned char rd_ds18_1()

unsigned int TemH,TemL //温度的整数部分和小数部分

unsigned int count //定义小数计算部分

void main()

{

ds_init() //18B20初始化

while(1)

{

ds_getT() //使用该函数获得温度,整数部分存储到TemH,小数部分存储到count的低8位

display((TemH/10)%10,TemH%10,((count/10)%10),(count%10))

//温度发送到数码管显示

}

}

/***************18B20初始化函数***********************/

void ds_init(void)

{

unsigned int k=0

ds_reset_1()

ds_reset_1() //reset

wr_ds18_1(0xcc) //skip rom

_nop_()

wr_ds18_1(0x7f)

ds_reset_1()

wr_ds18_1(0xcc)

_nop_()

wr_ds18_1(0x44)

for(k=0k<11000k++)

time_delay(255)

ds_reset_1()

}

void ds_getT(void)

{

wr_ds18_1(0xcc)

wr_ds18_1(0xbe)

TemH=get_temp_1()

TemL=get_temp_d()

TemH&=0x00ff

TemL&=0x00ff

count=(TemH*256+TemL)*6.25

}

/***************延时程序,单位us,大于10us*************/

void time_delay(unsigned char time)

{

time=time-10

time=time/6

while(time!=0)time--

}

/*****************************************************/

/*reset ds18b20 */

/*****************************************************/

void ds_reset_1(void)

{

unsigned char idata count=0

DQ1=0

time_delay(240)

time_delay(240)

DQ1=1

return

}

void check_pre_1(void)

{

while(DQ1)

while(~DQ1)

time_delay(30)

}

void read_ROM(void)

{

int n

ds_reset_1()

check_pre_1()

wr_ds18_1(0x33)

for(n=0n<8n++){ROM[n]=rd_ds18_1()}

}

/*****************************************************/

/* Read a bit from 1820 位读取 */

/*****************************************************/

bit tmrbit_1(void)

{

idata char i=0

bit dat

DQ1=0_nop_()

DQ1=1

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

_nop_()

dat = DQ1

time_delay(50)

return dat

}

/*****************************************************/

/*read a bety from ds18b20 字节读取 */

/*****************************************************/

unsigned char rd_ds18_1()

{

unsigned char idata i,j,dat=0

for(i=1i<=8i++)

{

j=tmrbit_1()

dat=(j<<(i-1))|dat

}

return dat

}

/*****************************************************/

/* write a bety from ds18b20 写字节*/

/****************************************************/

void wr_ds18_1(char dat)

{

signed char idata i=0

unsigned char idata j

bit testb

for(j=1j<=8j++)

{

testb=dat &0x01

dat = dat>>1

if(testb)

{

DQ1=0

_nop_()

_nop_()

DQ1=1

time_delay(60)

}

else

{

DQ1=0

time_delay(50)

DQ1=1

_nop_()

_nop_()

}

}

}

int get_temp_1(void)

{

unsigned char idata a=0,b=0

unsigned char idata i

EA=0

ds_reset_1()

check_pre_1()

wr_ds18_1(0xcc)

wr_ds18_1(0x44)

while(BUSY1)

ds_reset_1()

check_pre_1()

wr_ds18_1(0xcc)

wr_ds18_1(0xbe)

a=rd_ds18_1()

b=rd_ds18_1()

i=b /*若b为1则为负温 */

i=(i>>4)

if(i==0)

{

f=0

TMP=((a>>4)|(b<<4))

a=(a&0x0f)

if (a>8)

{

TMP=(TMP+1)

}

}

else

{

f=1

a=a>>4

b=b<<4

TMP=(a|b)

TMP=~TMP

TMP=(TMP+1)

}

EA=1

return(TMP)

}

int get_temp_d(void)

{

unsigned char idata a=0,b=0

unsigned char idata i,m

EA=0

ds_reset_1()//复位

check_pre_1()

wr_ds18_1(0xcc)

wr_ds18_1(0x44)

while(BUSY1)

ds_reset_1()

check_pre_1()

wr_ds18_1(0xcc)

wr_ds18_1(0xbe)

a=rd_ds18_1()

b=rd_ds18_1()

i=b /*若b为1则为负温 */

i=(i>>4)

if(i==0)

{

f=0

TMP=((a>>4)|(b<<4))

a=(a&0x0f)

TMP_d=a

}

else

{

f=1

a=~a

a=(a+1)

b=~b

b=(b+1)

m=a

a=a>>4

b=b<<4

TMP=(a|b)

m=(m&0x0f)

TMP_d=m

}

EA=1

return(TMP_d)

}void delay(unsigned int x)

{

unsigned int i

for(i=0i<xi++)

}

void display(unsigned char d1,unsigned char d2,unsigned char d3,unsigned char d4)

{

P0=a[d1]

LED_0=0

delay(100)

LED_0=1

P0=a[d2] &0x7f

LED_1=0

delay(100)

LED_1=1

P0=a[d3]

LED_2=0

delay(100)

LED_2=1

P0=a[d4]

LED_3=0

delay(100)

LED_3=1

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11103493.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-13
下一篇 2023-05-13

发表评论

登录后才能评论

评论列表(0条)

保存