锐单电子商城 , 一站式电子元器件采购平台!
  • 电话:400-990-0325

51单片机热敏电阻测温程序.doc

时间:2023-01-20 23:00:00 105t电阻1812j电阻

//本程序通过热敏电阻测量温度(30c-50c

#include

#include

#include

#define uchar unsigned char

#define uint unsigned int

uchar smg[]={0x88,0xeb,0x4c,0x49,0x2b,0x19,0x18,0xcb,0x08,0x09};

uchar b,d;

uint shuju;

int a,temp;

sbit start=P2^7;

sbit ale=P2^7;

sbit addc=P2^6;

sbit addb=P2^5;

sbit adda=P2^4;

sbit eoc=P2^3;

sbit oe=P2^2;

sbit clk=P3^2./0809小时钟脚

sbit dat=P3^0; ///串行数字管数据端数据端

sbit clock=P3^1; ///串行数码管时钟端

sbit DQ=P2^0;

/******************delay**************************/

void delay(uint x)

{

while(x--);

}

void delay1(uint x)

{

uint i,j;

for(i=0;i

for(j=0;j<110;j );

}

/*******************ds18b20***********************/

void Init_DS18B20(void)

{

unsigned char x=0;

DQ = 1; //DQ复位

delay(8); //稍作延迟

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

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

DQ = 1; ///拉高总线

delay(14);

x=DQ; //稍作延迟后 如果x=0初始化成功 x=一是初始化失败

delay(20);

}

/******************************从18B中读一个字节****************************/

uchar Read_OneChar(void)

{

uchar i = 0;

uchar dat = 0;

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

{

DQ = 0; // 给脉冲信号

dat >>= 1;

DQ = 1; // 给脉冲信号

if(DQ)

dat |= 0x80;

delay(8);

}

return(dat);

}

/******************************向18B20写字节****************************/

void Write_OneChar(uchar dat)

{

uchar i=0;

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

{

DQ = 0;

DQ = dat&0x01;

delay(10);

DQ = 1;

dat >>= 1;

}

delay(8);

}

/***********************************读取温度**********************************/

uchar Read_Temperature(void)

{

uchar i = 0,t = 0;

Init_DS18B20();

Write_OneChar(0xcc); // 跳过读序号列号的操作

Write_OneChar(0x44); // 启动温度转换

Init_DS18B20();

Write_OneChar(0xcc); ////跳过读序号列号的操作

Write_OneChar(0xbe); //读取温度寄存器等(共9个寄存器) 前两个是温度

i = Read_OneChar(); //读取低温值

t = Read_OneChar(); //读取高温值

b = t;

d =0x88;

/*if(b&0x80==0x80) //显示负数

{

t = ~t;

//t = 1;

i = ~i;

i = 1;

d = 0xbf;

}*/

a = i & 0x0f;

i = i >> 4; ///低位右移4位,放弃少数部分

t = t << 4; //高位左移4位,

锐单商城拥有海量元器件数据手册IC替代型号,打造电子元器件IC百科大全!

相关文章