void ReadTemperature() { static unsigned char num=0; if(0==num) { Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x4E); // //写入"写暂存器"命令,修改TH和TL和分辩率配置寄存器 //先写TH,再写TL,最后写配置寄存器 WriteOneChar(0x6e); //写入想设定的温度报警上限 WriteOneChar(0x00); //写入想设定的温度报警下限 WriteOneChar(0x3f); //写配置寄存器,格式为0 R1 R0 1,1 1 1 1 //R1R0=00分辨率娄9位,R1R0=11分辨率为12位 */ } else if(1==num) { // delay_18B20(80); // this message is wery important Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 } else if(2==num) { // delay_18B20(80); // this message is wery important Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 } else if(3==num) { // delay_18B20(80); low_byte=ReadOneChar(); //读取温度值低位 high_byte=ReadOneChar(); //读取温度值高位 num=0; return ; } num++; }
float DS18B20_Temperature(void) { BYTE flag = 0,i = 0; BYTE a[10]; float temp = 0.0f; flag = Init_DS18B20(); if (flag == 0x00) return -9999; DS18B20_WriteByte(DS18B20_SkipROM); DS18B20_WriteByte(DS18B20_ConvertTemp); flag = Init_DS18B20(); if (flag == 0x00) return -9999; DS18B20_WriteByte(DS18B20_SkipROM); DS18B20_WriteByte(DS18B20_ReadSCR); for(i = 0;i < 9;i++) a[i] = DS18B20_ReadByte(); if(CRC(a,9) == 0) { if(a[1]&0xf0) temp= 0-(0xffff-(unsigned short)((a[1]<<8)+a[0])+1)*0.0625; else temp=((unsigned short)(a[1]<<8)+a[0])*0.0625; } else temp = 9999; return temp; }
/*------------------------------------------------ 读取温度 ------------------------------------------------*/ unsigned int ReadTemperature(void) { unsigned char a=0; unsigned int b=0; unsigned int t=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 DelayUs2x(1000); Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 a=ReadOneChar(); //低位 b=ReadOneChar(); //高位 b<<=8; t=a+b; t=(float)t*0.625; //t为实际温度的十倍,这样方便取小数点后一位 return t; }
void Write_Temperature_alarm(void) { Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0x4e); //将设定的温度报警值写入 DS18B20 WriteOneChar(temp_alarm[0]); //写TH WriteOneChar(temp_alarm[1]); //写TL WriteOneChar(0x7f); //12位精确度 Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0x48); //把暂存器里的温度报警值拷贝到EEROM }
void Config18b20 (void) //重新配置报警限定值和分辨率 { Init_DS18B20(); WriteOneChar(0xcc); //skip rom WriteOneChar(0x4e); //write scratchpad WriteOneChar(0xff); //上限 WriteOneChar(0x00); //下限 WriteOneChar(0x7f); //set 12 bit Init_DS18B20(); WriteOneChar(0xcc); //skip rom WriteOneChar(0x48); //保存设定值 Init_DS18B20(); WriteOneChar(0xcc); //skip rom WriteOneChar(0xb8); //回调设定值 }
void Init_Hard(void){ Init_CLK(); gpio_init(); externalInterrupt_CcCv_init(); InitDelTim(); delay_ms(100); Init_Beep(); Init_Timer4(); //FAN_PWM Init_Encoder(); InitAdc(); Init_DAC_CH1(); Init_DAC_CH2(); Init_DS18B20(); Init_PVD(); Init_EXTI1(); Init_SPI2(); uart_init(USART3, BR38400); lcd_init (); //Глючная инициализация lcd_init (); lcd_init (); lcd_init (); timMeasInit(); //DEBUG }
void Read_RomCord(void) { unsigned char j; Init_DS18B20(); WriteOneChar(0x33); // 读序列码的操作 for (j = 0; j < 8; j++) { RomCode[j] = ReadOneChar() ; } }
//读取温度 unsigned char ReadTemperature(void) { unsigned char a=0; unsigned char b=0; unsigned char t=0; //float tt=0; Init_DS18B20(); WriteOneChar(0xCC); // 跳过读序号列号的操作 WriteOneChar(0x44); // 启动温度转换 delay_18B20(2000); Init_DS18B20(); WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器等(共可读9个寄存器) 前两个就是温度 a=ReadOneChar(); b=ReadOneChar(); b<<=4; b+=(a&0xf0)>>4; t=b; //tt=t*0.0625; //t= tt*10+0.5; //放大10倍输出并四舍五入 return(t); }
/*****读取温度*****/ int ReadTemperature(void) { unsigned char a=0; unsigned char b=0; unsigned int t=0; t=Init_DS18B20(); if(t) return Real_temp; WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0x44); //启动温度转换 // delay(10); t=Init_DS18B20(); if(t) return Real_temp; WriteOneChar(0xCC); //跳过读序号列号的操作 WriteOneChar(0xBE); //读取温度寄存器 a=ReadOneChar(); //读低8位 b=ReadOneChar(); //读高8位 t=b; t<<=8; t=t|a; if(t<=0||t>0x900) return Real_temp; return(t); }
/****************************************************** 函数名称:int ReadOneTemperature(void) 返回值:int t 参数: 无 作用:读温度值 *******************************************************/ int get_temp(void) ////////***读取温度值***********///// 每次读写均要先复位 { uchar a=0; uchar b=0; uint16 t=0; Init_DS18B20(); WriteOneChar(0xcc);//发跳过ROM命令 WriteOneChar(0x44);//发读开始转换命令 Init_DS18B20(); WriteOneChar(0xcc);//发跳过ROM命令 WriteOneChar(0xbe);//读寄存器,共九字节,前两字节为转换值 a=ReadOneChar(); //a存低字节 b=ReadOneChar(); //b存高字节 t=((b<<8)|a); return((t>>4)*10+table_tempfh[t&0x0f]); /* t=b; t<<=8;//高字节转换为10进制 t=t|a; tep=t*0.0625;//转换精度为0.0625/LSB t=tep*10+0.5;//保留1位小数并四舍五入****后面除10还原正确温度值) */ }