This page shows how to use DS18X20 library made by Martin Thomas.
The code of the library has been found at http://siwawi.bauing.uni-kl.de/avr_projects/tempsensor/
A demo code is placed in vscp_firmware/avr/ds18x20_demo. A good start is to try to program you AVR device with this code to make sure your CPU clock and timers are propoerly set.
The demo program should output the following:
In order to use the DS18X20 library you need to include the headers in your code:
#include "onewire.h" #include "ds18x20.h"
One wire bus port can be set in your code like so:
#ifndef OW_ONE_BUS ow_set_bus(&PINC,&PORTC,&DDRC,PC0); #endif
Then define a few variables to run init functions and store temperature data:
uint8_t nSensors, i; int16_t decicelsius;
A good practice is to use a function from the demo code to let the library search for the number of sensors:
nSensors = search_sensors();
You can find the function above in the demo code.
Then get the family code:
i = gSensorIDs[0][0]; // family-code for conversion-routine
Now everything is ready to run temperature conversion and temperature reading:
DS18X20_start_meas( DS18X20_POWER_PARASITE, NULL ); _delay_ms( DS18B20_TCONV_12BIT ); DS18X20_read_decicelsius_single( i, &decicelsius );
The temperature is now saved in a 16 bits signed integer variable (two's complement), you could send it to UART with something like below:
uart_puts( itoa( decicelsius, buf2, 10 ) );
Now you can convert the temperature data in your desired format and send it on the bus.
You can send the temperature data like below when you want to use normalized integer data type:
vscp_omsg.priority = 0x00; vscp_omsg.flags = VSCP_VALID_MSG + 4; vscp_omsg.vscp_class = VSCP_CLASS1_MEASUREMENT; vscp_omsg.vscp_type = VSCP_TYPE_MEASUREMENT_TEMPERATURE;
vscp_omsg.data[ 0 ] = 0x88; // data type set as two's complement vscp_omsg.data[ 1 ] = 0x81; // number of decimals + bit seven set. vscp_omsg.data[ 2 ] = decicelsius >> 8; // first half of the temperature vscp_omsg.data[ 3 ] = decicelsius & 0xff; // second half of the temperature
vscp_sendEvent(); // Send data
The result will be seen as below in VSCP Works: