日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

温度传感器的c语言程序,DS18B20数字温度传感器C语言程序实例

發布時間:2024/3/7 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 温度传感器的c语言程序,DS18B20数字温度传感器C语言程序实例 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

51單片機DS18B20數字溫度傳感器設計 與C程序

#include

#define uchar unsigned char

#define uint unsigned int

#define DQ RC1

#define DQ_HIGH() TRISC1=1

#define DQ_LOW() TRISC1=0;DQ=0

__CONFIG(0x3B31);

const uchar table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,

0x07,0x7f,0x6f};

const uchar table1[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,

0x87,0xff,0xef};

uint temper;

uchar a1,a2,a3,a4;

void delay(uint x);

void delayus(uint,uchar);

void init();

void disp(uchar num1,uchar num2,uchar num3,uchar num4);

void write_byte(uchar date);

uchar read_byte();

void get_tem();

void main()

{

init();

while(1)

{

get_tem();

}

}

void reset()

{

uchar st=1;

DQ_HIGH();

NOP();NOP();

while(st)

{

DQ_LOW();

delayus(70,30);

DQ_HIGH();

delayus(4,4);

if(DQ==1)

st=1;

else

st=0;

delayus(50,10);

}

}

void write_byte(uchar date)

{

uchar i,temp;

DQ_HIGH();

NOP();NOP();

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

{

temp=date&0x01;//01010101

DQ_LOW();

delayus(0,0);

if(temp==1)

DQ_HIGH();

delayus(2,2);

DQ_HIGH();

date=date>>1;//00101010

}

}

uchar read_byte()

{

uchar i,date;

static bit j;

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

{

date=date>>1;

DQ_HIGH();

NOP();NOP();

DQ_LOW();

NOP();NOP();NOP();NOP();NOP();NOP();

DQ_HIGH();

NOP();NOP();NOP();NOP();

j=DQ;

if(j==1)

date=date|0x80;//1000 0000

delayus(1,1);

}

return (date);

}

void get_tem()

{

uchar tem1,tem2,num;

float aaa;

reset(); //復位

write_byte(0xCC);//跳過ROM

write_byte(0x44);//溫度轉換

for(num=100;num>0;num--)

disp(a1,a2,a3,a4);

reset();

write_byte(0xCC);

write_byte(0xBE);

tem1=read_byte();

tem2=read_byte();

aaa=(tem2*256+tem1)*6.25;

temper=(int)aaa;

a1=temper/1000;

a2=temper%1000/100;

a3=temper%100/10;

a4=temper%10;

}

void delayus(uint x,uchar y)

{

uint i;

uchar j;

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

for(j=y;j>0;j--);

}

void delay(uint x)

{

uint a,b;

for(a=x;a>0;a--)

for(b=110;b>0;b--);

}

void init()

{

TRISD=0;

TRISA=0;

PORTD=0;

PORTA=0;

}

void disp(uchar num1,uchar num2,uchar num3,uchar num4)

{

PORTD=table[num1];//顯示第一個數碼管

PORTA=0x20;//0010 0000

delay(2);

PORTD=table1[num2];//顯示第二個數碼管

PORTA=0x10;//0001 0000

delay(2);

PORTD=table[num3];//顯示第三個數碼管

PORTA=0x08;//0000 1000

delay(2);

PORTD=table[num4];//顯示第四個數碼管

PORTA=0x04;//0000 0100

delay(2);

}

總結

以上是生活随笔為你收集整理的温度传感器的c语言程序,DS18B20数字温度传感器C语言程序实例的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。