コード例 #1
0
/*
 * 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;
}
コード例 #2
0
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;
}
コード例 #3
0
/*
 * 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;
}
コード例 #4
0
/*
 * read one byte
 */
uint8_t ds18b20_readbyte(void){
	uint8_t i=8, n=0;
	while(i--){
		n >>= 1;
		n |= (ds18b20_readbit()<<7);
	}
	return n;
}
コード例 #5
0
/*
 * 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;
}