多个ds18b20c程序.docx
《多个ds18b20c程序.docx》由会员分享,可在线阅读,更多相关《多个ds18b20c程序.docx(10页珍藏版)》请在冰豆网上搜索。
多个ds18b20c程序
bitds_init()//复位并检测单线器件,返回0,否则死循环
{
bitflag;
uchari;
DQ=0;
i=210;while(i)i--;//delay(500);//>480
DQ=1;
i=16;while(i)i--;//delay(40);//15-60
while(DQ);
flag=DQ; //存在器件
i=25;while(i)i--;//delay(60);//60-240
while(~DQ);
i=105;while(i)i--;//delay(420);//>(480-60)
returnflag;
}
voidds_writebyte(ucharb)//先读写最低位
{
uchari,j,temp;
for(i=0;i<8;i++)
{
temp=b&0x01;
b=b>>1;
DQ=0;
j=2;while(j)j--;// delay
(2);//>1
if(temp)
{
DQ=1;
j=25;while(j)j--;// delay(60);//>15
}
else
{
j=32;while(j)j--;// delay(80);//>60
DQ=1;
j=5;while(j)j--;//>1
}
}
}
bitds_readbit()
{
bitj;
uchark;
DQ=0;
k=2;while(k)k--;//>1
DQ=1;
k=2;while(k)k--;
j=DQ;
k=25;while(k)k--;//delay(60);
returnj;
}
uchards_readbyte()//先读的为最低位
{
uchari,j,b;
b=0;
for(i=0;i<8;i++)
{
j=ds_readbit();
b=(j<
}
returnb;
}
ucharcrctest(uchar*p)
{
ucharcrc,i,n,cbit,bit0,byte,r;
crc=0;
for(i=0;i<9;i++)
{
byte=*p;
for(n=0;n<8;n++)
{
cbit=crc&0x01;
bit0=byte&0x01;
crc=crc>>1;
r=cbit^bit0;
if(r==1)crc=crc^0x8c;
byte=byte>>1;
}
p++;
}//crc==0校正正确
returncrc;
}
voidGet_wendu()//得到float型全局量wendu[number_18B20]
{
ucharcodeserial_number[4][8]={0x28,0x2e,0xe0,0xce,0x01,0x00,0x00,0xe6,//一号18b20ROM号
0x28,0x13,0x2d,0x6c,0x01,0x00,0x00,0x21,//二号
0x28,0xea,0x07,0x6c,0x01,0x00,0x00,0x80,//三号
0x28,0x2a,0x0e,0x6c,0x01,0x00,0x00,0xec};//四号
//四个18b20的八字节序列号数据
ucharscratch[9];//TL,TH,ThR,TlR,Con,Res,Res,Res,CRC;//温度高低字节,结构寄存器和卷积码字节
uintT_18B20;//整型,合并存储温度数据
uchari,j,crc;
charfuhao;//表示正负温度+1,-1;
while(ds_init());//返回1则继续复位,直到检测到低电平的应答脉冲则向下执行
ds_writebyte(0xcc);//SKIPROM,不发送ROM命令则要发送跳过ROM命令(0xcc)
ds_writebyte(0x44);//命令单总线上的所有器件开始转换
delayms(10);//一般转换要750us,这里等待3ms足够
for(j=0;j{
while(ds_init()); //返回1则继续复位,直到检测到低电平的应答脉冲则向下执行
ds_writebyte(0x55);//ROM匹配
for(i=0;i<8;i++)
ds_writebyte(serial_number[j][i]);//ROM匹配注意:
发送序列号后,只有匹配的18B20才会对接下来的命令响应
ds_writebyte(0xbe);//readscratchpad命令
for(i=0;i<9;i++)
{
scratch[i]=ds_readbyte();
}
crc=crctest(scratch);//crc==0校正正确
////////////////////////
T_18B20=0;
T_18B20=scratch[1];
T_18B20=(T_18B20<<8)+scratch[0];
fuhao=1;
if(T_18B20>0xf000) //为负温度
{
T_18B20=~T_18B20+1;
fuhao=-1;
}
wendu[j]=fuhao*T_18B20/16.0+T_18B20%16/16.0;//将温度高低字节变成float
if(crc!
=0)wendu[j]=85.5;
}
}
发表于2008/4/1620:
59:
28
标签:
无标签
DS18B20温度采集通过串口送入电脑
DS18B20温度采集通过串口送入电脑
程序肯定还是把其它的驱动组合,呵呵,EDN51验证。
显示4位温度。
串口助手设置12008N1哦
给点支持吧
#include
#include
//*****************************************
sbit DS18b20_DQ=P1^6;
bit flag;
unsignedchar*p;
chara[16]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,0xc6,0x86,0x8e,0x82};
charb[16]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,
0x00,0x19,0x08,0x03,0x46,0x06,0x0e,0x02};
charone,two,three,four;
unsignedchar codeCrcTable[256]={
0, 94,188, 226, 97, 63, 221, 131, 194, 156, 126, 32, 163, 253, 31, 65,
157, 195, 33, 127, 252, 162, 64, 30, 95, 1, 227, 189, 62, 96, 130, 220,
35, 125, 159, 193, 66, 28, 254, 160, 225, 191, 93, 3, 128, 222, 60, 98,
190, 224, 2, 92, 223, 129, 99, 61, 124, 34, 192, 158, 29, 67, 161, 255,
70, 24, 250, 164, 39, 121, 155, 197, 132, 218, 56, 102, 229, 187, 89, 7,
219, 133,103, 57, 186, 228, 6, 88, 25, 71, 165, 251, 120, 38, 196, 154,
101, 59,217, 135, 4, 90, 184, 230, 167, 249, 27, 69, 198, 152, 122, 36,
248, 166,68, 26, 153, 199, 37, 123, 58, 100, 134, 216, 91, 5, 231, 185,
140, 210,48, 110, 237, 179, 81, 15, 78, 16, 242, 172, 47, 113, 147, 205,
17, 79, 173, 243, 112, 46, 204, 146, 211, 141, 111, 49, 178, 236, 14, 80,
175, 241,19, 77, 206, 144, 114, 44, 109, 51, 209, 143, 12, 82, 176, 238,
50, 108, 142, 208, 83, 13, 239, 177, 240, 174, 76, 18, 145, 207, 45, 115,
202, 148,118, 40, 171, 245, 23, 73, 8, 86, 180, 234, 105, 55, 213,139,
87, 9, 235, 181, 54, 104, 138, 212, 149, 203, 41, 119, 244, 170, 72, 22,
233, 183, 85, 11, 136, 214, 52, 106, 43, 117, 151, 201, 74, 20, 246, 168,
116, 42, 200, 150, 21, 75, 169, 247, 182, 232, 10, 84, 215, 137, 107, 53};
unsignedchartemp_buff[9];//存储读取的字节,readscratchpad为9字节,readromID为8字节
unsignedcharcrc_data;
floatTemperature;
unsignedcharid_buff[8];
/******定义函数****************/
#defineucharunsignedchar
#defineuintunsignedint
voidLCD_init(void);//初始化函数
voidLCD_write_command(ucharcommand);//写指令函数
voidLCD_write_data(uchardat);//写数据函数
voidLCD_disp_char(ucharx,uchary,uchardat);//在某个屏幕位置上显示一个字符,X(0-16),y(1-2)
voiddelay_n40us(uintn);//延时函数
//****************************************************/
voidTempDelay(uintus)
{
while(us--);
}
//******************************************************/
voidInit18b20(void)
{
DS18b20_DQ=1;
_nop_();
DS18b20_DQ=0;
TempDelay(86); //delay530uS//80
_nop_();
DS18b20_DQ=1;
TempDelay(14); //delay100uS//14
_nop_();
_nop_();
_nop_();
if(DS18b20_DQ==0)
flag=1; //detect1820success!
else
flag=0; //detect1820fail!
TempDelay(20); //20
_nop_();
_nop_();
DS18b20_DQ =1;
}
/*****************************
*Function:
向18B20写入一个字节*/
voidWriteByte(ucharwr) //单字节写入
{
uchari;
for(i=0;i<8;i++)
{
DS18b20_DQ = 0;
_nop_();
DS18b20_DQ=wr&0x01;
TempDelay(5); //delay45uS//5
_nop_();
_nop_();
DS18b20_DQ=1;
wr>>=1;
}
}
/************************************************************
*Function:
读18B20的一个字节
*************************************************************/
ucharReadByte(void) //读取单字节
{
uchari,u=0;
for(i=0;i<8;i++)
{
DS18b20_DQ=0;
u>>=1;
DS18b20_DQ=1;
if(DS18b20_DQ==1)
u|=0x80;
TempDelay(4);
_nop_();
}
return(u);
}
/************************************************************
*Function:
读18B20
*************************************************************/
voidread_bytes(ucharj)
{
uchari;
for(i=0;i {
*p=ReadByte();
p++;
}
}
/*Function:
CRC校验********************************/
ucharCRC(ucharj)
{
uchari,crc_data=0;
for(i=0;i crc_data=CrcTable[crc_data^temp_buff[i]];
return(crc_data);
}
/************************************************************
*Function:
读取温度
*************************************************************/
voidGemTemp(void)
{
read_bytes(9);
if(CRC(9)==0)//校验正确
{
Temperature=temp_buff[1]*0x100+temp_buff[0];
// Temperature*=0.625;
Temperature/=16;
TempDelay
(1);
}
}
/************************************************************
*Function:
内部配置
*************************************************************/
voidConfig18b20(void) //重新配置报警限定值和分辨率
{
Init18b20();
WriteByte(0xcc); //skiprom
WriteByte(0x4e); //writescratchpad
WriteByte(0x19); //上限
WriteByte(0x1a); //下限
WriteByte(0x7f); //set11bit(0.125)
Init18b20();
WriteByte(0xcc); //skiprom
WriteByte(0x48); //保存设定值
Init18b20();
WriteByte(0xcc); //skiprom
WriteByte(0xb8); //回调设定值
}
/************************************************************
*Function:
读18B20ID
*parameter:
*Return:
*Modify:
*************************************************************/
voidReadID(void)//读取器件id
{
Init18b20();
WriteByte(0x33); //readrom
read_bytes(8);
}
/************************************************************
*Function:
18B20ID全处理*/
voidTemperatuerResult(void)
{
p=id_buff;
ReadID();
Config18b20();
Init18b20();
WriteByte(0xcc); //skiprom
WriteByte(0x44); //Temperatureconvert
Init18b20();
WriteByte(0xcc); //skiprom
WriteByte(0xbe);
}
voidGet_Temperature()
{
p =temp_buff;
GemTemp();
}
/*voiddelay_n40us(uintn)
{
uinti;
ucharj;
for(i=n;i>0;i--)
for(j=0;j<15;j++)
; //
}*/
//初始化232
voidInit_RSC232()
{
//波特率:
1200bit/s,1位起始位,1位停止位,8位数据位
TMOD=0x20;
TL1 =0xE8;
TH1 =0xE8;
TR1 =1;
PCON=0x00;
SCON=0x50;
}
voidprintc(unsignedcharc)
{
TI = 0;
SBUF= c;
while(TI==0)
;
TI =0 ;
}
voidprints(unsignedcharc[])
{
unsignedcharindex=0;
while(c[index]!
='\0')
{
printc(c[index]);
index++;
}
}
voidprinthex(unsignedchari)
{
unsignedcharj;
j=i+0x30;
printc(j);
}
voiddelay_n40us(uintn)
{
uinti;
ucharj;
for(i=n;i>0;i--)
for(j=0;j<15;j++)
; //
} //
//*******************************
//*********主函数*****************
voidmain(void)
{ intTemper;
Init_RSC232();
while
(1)
{
TemperatuerResult();
Get_Temperature();
//prints("Welcometojizzll'sblog!
\n\r");
Temper=(int)Temperature;
one="Temper/10";
two="Temper"%10;
three=(Temperature-