欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136
標(biāo)題:
三點(diǎn)溫度采集及串口發(fā)送51單片機(jī)源碼
[打印本頁(yè)]
作者:
ZdaRui
時(shí)間:
2018-4-18 21:02
標(biāo)題:
三點(diǎn)溫度采集及串口發(fā)送51單片機(jī)源碼
三個(gè)DS18B20的溫度采集單片機(jī)源程序如下:
#include <reg52.h>
#include <stdio.h>
#define uchar unsigned char
#define uint unsigned int
sbit ds = P2^0;
sbit ds2 = P2^1;
sbit ds3 = P2^2;
sbit Led1 = P0^4;
sbit Led2 = P0^5;
sbit Led3 = P0^7;
uint temp,temp2,temp3; //整形溫度數(shù)據(jù)
float f_temp1,f_temp2,f_temp3; //浮點(diǎn)型溫度數(shù)據(jù)
uchar rev=0,flag_rev=0;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
/*****DS18B20復(fù)位,初始化函數(shù)*****/
void dsreset(void)
{
uint i;
ds = 0;
i = 103;
while(i>0)
i--;
ds = 1;
i = 4;
while(i>0)
i--;
}
void dsreset2(void)
{
uint i;
ds2 = 0;
i = 103;
while(i>0)
i--;
ds2 = 1;
i = 4;
while(i>0)
i--;
}
void dsreset3(void)
{
uint i;
ds3 = 0;
i = 103;
while(i>0)
i--;
ds3 = 1;
i = 4;
while(i>0)
i--;
}
/*****讀1位數(shù)據(jù)函數(shù)*****/
bit tempreadbit(void)
{
uint i;
bit dat;
ds = 0;
i++;
ds = 1;
i++;
i++;
dat = ds;
i = 8;
while(i>0)
i--;
return (dat);
}
bit tempreadbit2(void)
{
uint i;
bit dat;
ds2 = 0;
i++;
ds2 = 1;
i++;
i++;
dat = ds2;
i = 8;
while(i>0)
i--;
return (dat);
}
bit tempreadbit3(void)
{
uint i;
bit dat;
ds3 = 0;
i++;
ds3 = 1;
i++;
i++;
dat = ds3;
i = 8;
while(i>0)
i--;
return (dat);
}
/*****讀一個(gè)字節(jié)數(shù)據(jù)函數(shù)*****/
uchar tempread(void)
{
uchar i,j,dat;
dat = 0;
for(i=1;i<=8;i++)
{
j = tempreadbit();
dat = (j<<7)|(dat>>1);
}
return (dat);
}
uchar tempread2(void)
{
uchar i,j,dat;
dat = 0;
for(i=1;i<=8;i++)
{
j = tempreadbit2();
dat = (j<<7)|(dat>>1);
}
return (dat);
}
uchar tempread3(void)
{
uchar i,j,dat;
dat = 0;
for(i=1;i<=8;i++)
{
j = tempreadbit3();
dat = (j<<7)|(dat>>1);
}
return (dat);
}
/*****向DS18B20寫(xiě)一個(gè)字節(jié)數(shù)據(jù)函數(shù)*****/
void tempwritebyte(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb = dat&0x01;
dat = dat>>1;
if(testb)
{
ds = 0;
i++; i++;
ds = 1;
i = 8;
while(i>0)
i--;
}
else
{
ds = 0;
i = 8;
while(i>0)
i--;
ds = 1;
i++; i++;
}
}
}
void tempwritebyte2(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb = dat&0x01;
dat = dat>>1;
if(testb)
{
ds2 = 0;
i++; i++;
ds2 = 1;
i = 8;
while(i>0)
i--;
}
else
{
ds2 = 0;
i = 8;
while(i>0)
i--;
ds2 = 1;
i++; i++;
}
}
}
void tempwritebyte3(uchar dat)
{
uint i;
uchar j;
bit testb;
for(j=1;j<=8;j++)
{
testb = dat&0x01;
dat = dat>>1;
if(testb)
{
ds3 = 0;
i++; i++;
ds3 = 1;
i = 8;
while(i>0)
i--;
}
else
{
ds3 = 0;
i = 8;
while(i>0)
i--;
ds3 = 1;
i++; i++;
}
}
}
void tempchange(void)
{
dsreset();
delay(1);
tempwritebyte(0xcc); //寫(xiě)跳過(guò)讀ROM指令
tempwritebyte(0x44); //寫(xiě)溫度轉(zhuǎn)換指令
}
void tempchange2(void)
{
dsreset2();
delay(1);
tempwritebyte2(0xcc); //寫(xiě)跳過(guò)讀ROM指令
tempwritebyte2(0x44); //寫(xiě)溫度轉(zhuǎn)換指令
}
void tempchange3(void)
{
dsreset3();
delay(1);
tempwritebyte3(0xcc); //寫(xiě)跳過(guò)讀ROM指令
tempwritebyte3(0x44); //寫(xiě)溫度轉(zhuǎn)換指令
}
/*****讀取寄存器中存儲(chǔ)的溫度數(shù)據(jù)*****/
uint get_temp()
{
uchar a,b;
dsreset();
delay(1);
tempwritebyte(0xcc);
tempwritebyte(0xbe);
a = tempread(); //低八位
b = tempread(); //高八位
temp = b;
temp<<=8;
temp = temp|a;
f_temp1 = temp*0.0625;
temp = f_temp1*10+0.5;
f_temp1 = f_temp1+0.05;
return temp;
}
uint get_temp2()
{
uchar a,b;
dsreset2();
delay(1);
tempwritebyte2(0xcc);
tempwritebyte2(0xbe);
a = tempread2(); //低八位
b = tempread2(); //高八位
temp2 = b;
temp2<<=8;
temp2 = temp2|a;
f_temp2 = temp2*0.0625;
temp2 = f_temp2*10+0.5;
f_temp2 = f_temp2+0.05;
return temp2;
}
uint get_temp3()
{
uchar a,b;
dsreset3();
delay(1);
tempwritebyte3(0xcc);
tempwritebyte3(0xbe);
a = tempread3(); //低八位
b = tempread3(); //高八位
temp3 = b;
temp3<<=8;
temp3 = temp3|a;
f_temp3 = temp3*0.0625;
temp3 = f_temp3*10+0.5;
f_temp3 = f_temp3+0.05;
return temp3;
}
/*****串口配置部分*****/
void int_com(void)
{
TMOD = 0X20;
PCON = 0X00;
SCON = 0X50;
TH1 = 0XFd; //波特率9600
TL1 = 0XFd;
TR1 = 1;
SM0 = 0;
SM1 = 1;
REN = 1;
EA = 1;
ES = 1;
}
void comm(char *parr)
{
do
{
SBUF = *parr++; //發(fā)送數(shù)據(jù)
while(!TI); //等待發(fā)送完成標(biāo)志為1
TI = 0; //標(biāo)志位清0
}
while(*parr); //保持循環(huán)直到字符為'\0'
}
void main()
{
uchar buff1[4],buff2[4],buff3[4],buffer[12];
uint m;
int_com();
while(1)
{
tempchange();
tempchange2();
tempchange3();
get_temp(); //獲取溫度
get_temp2();
get_temp3();
sprintf(buff1,"%f",f_temp1); //浮點(diǎn)型溫度格式轉(zhuǎn)換為字符型
sprintf(buff2,"%f",f_temp2);
sprintf(buff3,"%f",f_temp3);
/*****數(shù)據(jù)打包處理*****/
for(m=0;m<4;m++)
{
buffer[m] = buff1[m];
buffer[m+4] = buff2[m];
buffer[m+8] = buff3[m];
}
comm(buffer); //串口發(fā)送數(shù)據(jù)
if(flag_rev == 1)
{
ES = 0;
switch(rev)
{
case '0': Led1=1;Led2=1;Led3=1;
break;
case '1': Led1=0;Led2=1;Led3=1;
break;
case '2': Led1=1;Led2=0;Led3=1;
break;
case '3': Led1=1;Led2=1;Led3=0;
break;
case '4': Led1=0;Led2=0;Led3=1;
break;
case '5': Led1=1;Led2=0;Led3=0;
break;
case '6': Led1=1;Led2=0;Led3=1;
break;
case '7': Led1=0;Led2=0;Led3=0;
break;
}
ES = 1;
flag_rev = 0;
}
}
}
void ser() interrupt 4
{
RI = 0;
rev = SBUF;
flag_rev = 1;
}
復(fù)制代碼
所有資料51hei提供下載:
三點(diǎn)溫度采集及串口發(fā)送51.zip
(88.42 KB, 下載次數(shù): 37)
2018-4-18 21:02 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.raoushi.com/bbs/)
Powered by Discuz! X3.1