double ds18b20_gettemp(void){ uint8_t temperature_l; uint8_t temperature_h; double retd = 0; #if DS18B20_StopInterruptOnRead == 1 cli(); #endif ds18b20_reset(); ds18b20_writebyte(DS18B20_SkipROM); ds18b20_writebyte(DS18B20_ConvertTemp); while(!ds18b20_readbit()); ds18b20_reset(); ds18b20_writebyte(DS18B20_SkipROM); ds18b20_writebyte(DS18B20_RScratchPad); temperature_l = ds18b20_readbyte(); temperature_h = ds18b20_readbyte(); #if DS18B20_StopInterruptOnRead==1 sei(); #endif retd = ((temperature_h << 8) + temperature_l) * 0.0625; return retd; }
/* * get temperature */ double ds18b20_gettemp() { uint8_t temperature_l; uint8_t temperature_h; double retd = 0; #if DS18B20_STOPINTERRUPTONREAD == 1 cli(); #endif ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion while(!ds18b20_readbit()); //wait until conversion is complete ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read 2 byte from scratchpad temperature_l = ds18b20_readbyte(); temperature_h = ds18b20_readbyte(); #if DS18B20_STOPINTERRUPTONREAD == 1 sei(); #endif char buffer[10]; sprintf(buffer,"%d:%d\n\r",temperature_h,temperature_l); USART1_sendStr(buffer); //convert the 12 bit value obtained retd = ( ( temperature_h << 8 ) + temperature_l ) * 0.0625; return retd; }
/* * get temperature */ double ds18b20_gettemp() { uint8_t temperature[2]; int8_t digit; uint16_t decimal; double retd = 0; ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion while(!ds18b20_readbit()); //wait until conversion is complete ds18b20_reset(); //reset ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read 2 byte from scratchpad temperature[0] = ds18b20_readbyte(); temperature[1] = ds18b20_readbyte(); ds18b20_reset(); //reset //store temperature integer digits digit = temperature[0]>>4; digit |= (temperature[1]&0x7)<<4; //store temperature decimal digits decimal = temperature[0]&0xf; decimal *= DS18B20_DECIMALSTEPS; //compose the double temperature value and return it retd = digit + decimal * 0.0001; return retd; }
/******************************************************* * 函数功能:向scratchpad TH TL CONFIGURATION中写入数据 * 入口参数: * 出口参数: *******************************************************/ void ds18b20_writealarm(uchar th,uchar tl) { ds18b20_initial(); ds18b20_writebyte(0xcc); //Skip ROM command ds18b20_writebyte(0x4e); //Write scratchpad ds18b20_writebyte(th); //TH 25 ds18b20_writebyte(tl); //TL 0 ds18b20_writebyte(0x7f); //configuration register }
void ds1820_readalarm() { ds18b20_initial(); ds18b20_writebyte(0xcc); //Sip ROM command ds18b20_writebyte(0xbe);//Read sratchpad command ds18b20_readbyte(); ds18b20_readbyte(); a=ds18b20_readbyte(); b=ds18b20_readbyte(); // ds18b20_initial(); }
/* * get temperature */ double ds18b20_gettemp() { uint8_t scratchpad[SCRATCHPAD_SIZE]; uint8_t i; double temp_value = DS18B20_ERR; cli(); // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_WSCRATCHPAD); //write to scratchpad ds18b20_writebyte(0x00); //alarm trigger TH ds18b20_writebyte(0x00); //alarm trigger TL ds18b20_writebyte(DS18B20_RES); //conversion resolution // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_CONVERTTEMP); //start temperature conversion sei(); while(!ds18b20_readbit()); //wait until conversion is complete cli(); // reset if (ds18b20_reset()) { sei(); return DS18B20_ERR; } ds18b20_writebyte(DS18B20_CMD_SKIPROM); //skip ROM ds18b20_writebyte(DS18B20_CMD_RSCRATCHPAD); //read scratchpad //read scratchpad for (i=0; i<SCRATCHPAD_SIZE; i++) scratchpad[i] = ds18b20_readbyte(); sei(); if (crc8(scratchpad, SCRATCHPAD_SIZE - 1) == scratchpad[SCRATCHPAD_CRC]) //convert the value obtained temp_value = ((scratchpad[SCRATCHPAD_TEMP_H] << 8) + scratchpad[SCRATCHPAD_TEMP_L]) * 0.0625; return temp_value; }
void set_alarm() { uchar i; keyfive_flag=0; lcd_writecommand(0x01); ds1820_readalarm(); while(keyfive_flag==0) { lcd_writecommand(0x80+0x02); for(i=0;i<12;i++) { lcd_writedata(alarm_panel[i]); } lcd_writecommand(0x80+0x42); for(i=13;i<16;i++) { lcd_writedata(alarm_panel[i]); } lcd_writedata(decimal_character(a/10)); lcd_writedata(decimal_character(a%10)); lcd_writecommand(0x80+0x49); for(i=17;i<20;i++) { lcd_writedata(alarm_panel[i]); } lcd_writedata(decimal_character(b/10)); lcd_writedata(decimal_character(b%10)); if(keysix_flag==1) shift_cursor(); keyfive_scan(); keysix_scan(); } ds18b20_initial(); ds18b20_writebyte(0xcc); ds18b20_writebyte(0x48); }