单片机数字温度计设计用C语言写程序

单片机数字温度计设计用C语言写程序,第1张

#include <reg51.h>

#define uchar unsigned char

sbit BEEP=P3^7

//接控制继电器

sbit DQ = P3^6

//接温度传感器18B20

uchar t[2],number=0,*pt

//温度值

uchar TempBuffer1[4]={0,0,0,0}

uchar Tmax=50,Tmin=10

uchar distab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e,0xff,0xfe,0xf7}

uchar currtemp

void t0isr() interrupt 1

{

TH0=(65536-5000)/256

TL0=(65536-5000)%256

P2=1<<number

if(number==2)P0=distab[TempBuffer1[0]]&0x7f

else P0=distab[TempBuffer1[0]]

number++

if(number>3)number=0

}

void delay_18B20(unsigned int i)

{

while(i--)

}

/**********ds18b20初始化函数**********************/

void Init_DS18B20(void)

{

bit x=0

do{

DQ=1

delay_18B20(8)

DQ = 0 //单片机将DQ拉低

delay_18B20(90)//精确延时 大于 480us

DQ = 1 //拉高总线

delay_18B20(14)

x=DQ //稍做延时后 如果x=0则初始化成功 x=1则初始化失败,继续初始化

}while(x)

delay_18B20(20)

}

/***********ds18b20读一个字节**************/

unsigned char ReadOneChar(void)

{

unsigned char i=0

unsigned char dat = 0

for (i=8i>0i--)

{

DQ = 0// 给脉冲信号

dat>>=1

DQ = 1// 给脉冲信号

if(DQ)

dat|=0x80

delay_18B20(4)

}

return(dat)

}

/*************ds18b20写一个字节****************/

void WriteOneChar(unsigned char dat)

{

unsigned char i=0

for (i=8i>0i--)

{

DQ = 0

DQ = dat&0x01

delay_18B20(5)

DQ = 1

dat>>=1

}

}

/**************读取ds18b20当前温度************/

unsigned char *ReadTemperature(unsigned char rs)

{

unsigned char tt[2]

delay_18B20(80)

Init_DS18B20()

WriteOneChar(0xCC) //跳过读序号列号的 *** 作

WriteOneChar(0x44)

//启动温度转换

delay_18B20(80)

Init_DS18B20()

WriteOneChar(0xCC)

//跳过读序号列号的 *** 作

WriteOneChar(0xBE)

//读取温度寄存器等(共可读9个寄存器)前两个就是温度

tt[0]=ReadOneChar() //读取温度值低位

tt[1]=ReadOneChar() //读取温度值高位

return(tt)

}

void covert1(void)

//将温度转换为LED显示的数据

{

uchar x=0x00,y=0x00

t[0]=*pt

pt++

t[1]=*pt

if(t[1]&0x080) //判断正负温度

{

TempBuffer1[0]=0x0c

//c代表负

t[1]=~t[1]

/*下面几句把负数的补码*/

t[0]=~t[0]

/*换算成绝对值*********/

x=t[0]+1

t[0]=x

if(x==0x00)t[1]++

}

else TempBuffer1[0]=0x0a

//A代表正

t[1]<<=4

//将高字节左移4位

t[1]=t[1]&0xf0

x=t[0]

//将t[0]暂存到X,因为取小数部分还要用到它

x>>=4

//右移4位

x=x&0x0f

//和前面两句就是取出t[0]的高四位

y=t[1]|x

//将高低字节的有效值的整数部分拼成一个字节

TempBuffer1[1]=(y%100)/10

TempBuffer1[2]=(y%100)%10

t[0]=t[0]&0x0f

//小数部分

TempBuffer1[3]=t[0]*10/16

if(currtemp<Tmin || currtemp>Tmax)BEEP=1

else BEEP=0

}

void convert(char tmp)

{

uchar a

if(tmp<0)

{

TempBuffer1[0]=0x0c

a=~tmp+1

}

else

{

TempBuffer1[0]=0x0a

a=tmp

}

TempBuffer1[1]=(a%100)/10

TempBuffer1[2]=(a%100)%10

}

main()

{

TMOD=0x01

TH0=(65536-5000)/256

TL0=(65536-5000)%256

TR0=1

ET0=1

EA=1

out=1

flag=0

ReadTemperature(0x3f)

delay_18B20(50000)

//延时等待18B20数据稳定

while(1)

{

pt=ReadTemperature(0x7f)//读取温度,温度值存放在一个两个字节的数组中

if(dismod==0)covert1()

delay_18B20(30000)

}

}

//18B20单线温度检测的应用样例程序

#include<REG52.H>

#include<math.h>

#include<INTRINS.H>

#define uchar unsigned char

#define uint unsigned int

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

//sbit seg1=P1^0

//sbit seg2=P1^1

//sbit seg3=P1^2

sbit key = P1^3 // 继电器开关 1 打开 0 关断

sbit DQ=P2^7//ds18b20 端口

sfr dataled=0x80//显示数据端口

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

uchar temp

uchar flag_get,count,num,minute,second

uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}//7段数码管段码表共阴

uchar a,b

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

void delay1(uchar MS)

unsigned char ReadTemperature(void)

void Init_DS18B20(void)

unsigned char ReadOneChar(void)

void WriteOneChar(unsigned char dat)

void delay(unsigned int i)

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

main()

{

TMOD|=0x01//定时器设置

TH0=0xef

TL0=0xf0

IE=0x82

TR0=1//启动定时器

P1=0x00

count=0

while(1)

{

// str[2]=0x39//显示C符号

a=tab[temp/10]//十位温度

b=tab[temp%10]//个位温度

if(flag_get==1) //定时读取当前温度

{

temp=ReadTemperature()-1 //temp是当前温度 通过这个温度与你设定温度比较

if(temp==你设定的值)

key=0 //关掉继电器停止加热

key=1 //否则继续加热

flag_get=0

}

}

}

void tim(void) interrupt 1 using 1//中断,用于数码管扫描和温度检测间隔

{

TH0=0xef//定时器重装值

TL0=0xf0

num++

if (num==50)

{num=0

flag_get=1//标志位有效

second++

if(second>=60)

{second=0

minute++

}

}

count++

if(count==1)

{P1=0xfe

dataled=a}//数码管扫描

if(count==2)

{P1=0xfd

dataled=bcount=0}

/*if(count==3)

{ P1=2

dataled=str[2]

count=0}*/

}

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

void delay(unsigned int i)//延时函数

{

while(i--)

}

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

//18b20初始化函数

void Init_DS18B20(void)

{

unsigned char x=0

DQ = 1 //DQ复位

delay(8) //稍做延时

DQ = 0 //单片机将DQ拉低

delay(80)//精确延时 大于 480us

DQ = 1 //拉高总线

delay(10)

x=DQ //稍做延时后 如果x=0则初始化成功 x=1则初始化失败

delay(5)

}

//读一个字节

unsigned char ReadOneChar(void)

{

unsigned char i=0

unsigned char dat = 0

for (i=8i>0i--)

{

DQ = 0// 给脉冲信号

dat>>=1

DQ = 1// 给脉冲信号

if(DQ)

dat|=0x80

delay(5)

}

return(dat)

}

//写一个字节

void WriteOneChar(unsigned char dat)

{

unsigned char i=0

for (i=8i>0i--)

{

DQ = 0

DQ = dat&0x01

delay(5)

DQ = 1

dat>>=1

}

delay(5)

}

//读取温度

unsigned char ReadTemperature(void)

{

unsigned char a=0

unsigned char b=0

unsigned char t=0

//float tt=0

Init_DS18B20()

WriteOneChar(0xCC)// 跳过读序号列号的 *** 作

WriteOneChar(0x44)// 启动温度转换

delay(200)

Init_DS18B20()

WriteOneChar(0xCC)//跳过读序号列号的 *** 作

WriteOneChar(0xBE)//读取温度寄存器等(共可读9个寄存器) 前两个就是温度

a=ReadOneChar()

b=ReadOneChar()

b<<=4

b+=(a&0xf0)>>4

t=b

//tt=t*0.0625

//t= tt*10+0.5//放大10倍输出并四舍五入

return(t)

}

这是一个带数码管显示的程序这样可以更直观的看到现在温度是多少,不要可以不连接外围电路。

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

*

文件名

温度采集DS18B20.c

*

描述

:

该文件实现了用温度传感器件DS18B20对温度的采集,并在数码管上显示出来。

*

创建人

东流,2009年4月10日

*

版本号

2.0

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

#include<reg52.h>

#define

uchar

unsigned

char

#define

uint

unsigned

int

#define

jump_ROM

0xCC

#define

start

0x44

#define

read_EEROM

0xBE

sbit

DQ

=

P2^3

//DS18B20数据口

unsigned

char

TMPH,TMPL

uchar

code

table[10]

=

{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}

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

*

名称

:

delay()

*

功能

:

延时,延时时间大概为140US。

*

输入

:

*

输出

:

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

void

delay_1()

{

int

i,j

for(i=0

i<=10

i++)

for(j=0

j<=2

j++)

}

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

*

名称

:

delay()

*

功能

:

延时函数

*

输入

:

*

输出

:

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

void

delay(uint

N)

{

int

i

for(i=0

i<N

i++)

}

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

*

名称

:

Delay_1ms()

*

功能

:

延时子程序,延时时间为

1ms

*

x

*

输入

:

x

(延时一毫秒的个数)

*

输出

:

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

void

Delay_1ms(uint

i)//1ms延时

{

uchar

x,j

for(j=0j<ij++)

for(x=0x<=148x++)

}

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

*

名称

:

Reset()

*

功能

:

复位DS18B20

*

输入

:

*

输出

:

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

uchar

Reset(void)

{

uchar

deceive_ready

DQ

=

0

delay(29)

DQ

=

1

delay(3)

deceive_ready

=

DQ

delay(25)

return(deceive_ready)

}

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

*

名称

:

read_bit()

*

功能

:

从DS18B20读一个位值

*

输入

:

*

输出

:

从DS18B20读出的一个位值

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

uchar

read_bit(void)

{

uchar

i

DQ

=

0

DQ

=

1

for(i=0

i<3

i++)

return(DQ)

}

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

*

名称

:

write_bit()

*

功能

:

向DS18B20写一位

*

输入

:

bitval(要对DS18B20写入的位值)

*

输出

:

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

void

write_bit(uchar

bitval)

{

DQ=0if(bitval==1)

DQ=1

delay(5)

DQ=1

}

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

*

名称

:

read_byte()

*

功能

:

从DS18B20读一个字节

*

输入

:

*

输出

:

从DS18B20读到的值

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

uchar

read_byte(void)

{

uchar

i,m,receive_data

m

=

1

receive_data

=

0

for(i=0

i<8

i++)

{

if(read_bit())

{

receive_data

=

receive_data

+

(m

<<

i)

}

delay(6)

}

return(receive_data)

}

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

*

名称

:

write_byte()

*

功能

:

向DS18B20写一个字节

*

输入

:

val(要对DS18B20写入的命令值)

*

输出

:

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

void

write_byte(uchar

val)

{

uchar

i,temp

for(i=0

i<8

i++)

{

temp

=

val

>>

i

temp

=

temp

&

0x01

write_bit(temp)

delay(5)

}

}

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

*

名称

:

Main()

*

功能

:

主函数

*

输入

:

*

输出

:

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

void

main()

{

float

tt

uint

temp

P2

=

0x00

while(1)

{

Reset()

write_byte(jump_ROM)

write_byte(start)

Reset()

write_byte(jump_ROM)

write_byte(read_EEROM)

TMPL

=

read_byte()

TMPH

=

read_byte()

temp

=

TMPL

/

16

+

TMPH

*

16

P0

=

table[temp/10%10]

P2

=

6

Delay_1ms(5)

P0

=

table[temp%10]

P2

=

7

Delay_1ms(5)

}

}


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

原文地址: https://outofmemory.cn/yw/11833060.html

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

发表评论

登录后才能评论

评论列表(0条)

保存