单片机温度控制c语言程序,单片机控制的温度传感器C语言程序.doc

.

.

#include

#define DO P3_7

#define SEG P0 //数码管自左至右依次为1234位

#define MS2L 0x18 //1ms的延时参数

#define MS2H 0xfc

#define uchar unsigned char

#define uint unsigned int

uchar code comm[4] = {0x01,0x02,0x04,0x08};

uchar code seg[10] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};

uchar code seg_add_dicimal[10] = {0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10};

uchar code seg_dicimal[2] ={0xc0, 0x92};

uchar k = 0;

uchar temp_l = 0;

uchar temp_h = 0;

uchar tempsign = 0;

uchar hundreds = 0;

uchar tens = 0;

uchar ones = 0;

uchar low_four = 0;

/*延时以ms为单位的t时间*/

void Delay(uint t)

{

uint i;

while(t--)

{

for(i=0; i<125; i++)

{ }

}

}

/*us级延时,延时时间约4+2*i*/

void Delayus(uchar i)

{

while(--i);

}

/*产生复位脉冲,等待应答信号*/

void Resetpaulse()

{

DO = 0; //拉低约600us

Delayus(150);

Delayus(150);

DO = 1;//产生上升沿,延时约15~60us

Delayus(30);

while(~DO); //等待应答信号

}

/*读取数据一位*/

bit Readbit()

{

uint i = 0;

bit b = 0;

DO = 0; //产生读时隙

i++; //维持低电平至少1us

DO = 1; //1us以上后拉高

Delayus(2); //延时8us,DO下降沿15内ds18b20输出的数据才有效

b = DO; //读取数据

Delayus(40); //每个读时隙至少持续60us

return(b);

}

/*读取一个字节*/

uchar Readbyte()

{

uchar byte_read = 0;

uchar i, j;

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

{

j = Readbit();

byte_read = (j<

}

return(byte_read);

}

/*写一个字节*/

void Writebyte(uchar byte_to_write)

{

uchar i = 0;

uchar j = 0;

bit write_bit = 0;

for(j=0; j<8; j++)

{

write_bit = (byte_to_write & 0x01);

if(write_bit == 1) //写1

{

DO = 0; //产生写时隙

Delayus(3); //延时15us

DO = 1; //写1

Delayus(40); //延时,写时隙不得低于60us

}

else

{

DO = 0; //产生写时隙

Delayus(50); //延时,保持低约60us~120us

DO = 1;

i++;

}

byte_to_write = byte_to_write >> 1;

}

}

/*配置ds18b20,9位分辨率 */

void Configurate()

{

EA = 0;

Resetpaulse(); //发出复位脉冲,每次操作都从复位开始

Delay(1);

Writebyte(0xcc); //skip room命令

Writebyte(0x4e);

Writebyte(0x7f);

Writebyte(0x80);

Writebyte(0x1f);

EA = 1;

}

/*启动温度转换*/

void StartConvert(

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值