BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
Sensors: DS18B20

The DS18B20 is a digital temperature sensor supplying a unique 48-bit serial number accessed through the 1-wire bus protocol.

main.c

/* Sanity check that the features we requested are present */
#if ! (BSP430_CONSOLE - 0)
#error Console is not configured correctly
#endif
/* Sanity check that the features we requested are present */
#if ! (BSP430_UPTIME - 0)
#error Uptime is not configured correctly
#endif
#ifndef APP_DS18B20_BUS
#define APP_DS18B20_BUS ds18b20
/* Where the device can be found */
const struct sBSP430onewireBus ds18b20 = {
.port = APP_DS18B20_PORT_HAL,
.bit = APP_DS18B20_BIT,
};
#endif /* APP_DS18B20_BUS */
#ifndef APP_DS18B20_POWER_BIT
/* Bit on APP_DS18B20_BUS.port that supplies power during temperature
* conversions. */
#define APP_DS18B20_POWER_BIT 0
#endif /* APP_DS18B20_POWER_BIT */
/* The serial number read from the device on startup */
void main ()
{
int rc;
int external_power;
static const char * const supply_type[] = { "parasitic", "external" };
const struct sBSP430onewireBus * const bus = &APP_DS18B20_BUS;
cprintf("\nHere we go...\n");
cprintf("Uptime now %lu with frequency %lu Hz\n",
cprintf("Monitoring DS18xx on %s.%u ; ",
if (APP_DS18B20_POWER_BIT) {
cprintf("parasitic conversion power from %s.%u\n",
iBSP430portBitPosition(APP_DS18B20_POWER_BIT));
BSP430_PORT_HAL_HPL_DIR(bus->port) &= ~APP_DS18B20_POWER_BIT;
} else {
cprintf("no parasitic power boost\n");
}
external_power = iBSP430onewireReadPowerSupply(bus);
cprintf("Power supply: %s\n", supply_type[external_power]);
if (0 > external_power) {
cprintf("ERROR: Device not present?\n");
} else if ((! external_power) && (! (APP_DS18B20_POWER_BIT))) {
cprintf("ERROR: Parasitic power without conversion boost?\n");
}
do {
rc = iBSP430onewireReadSerialNumber(bus, &serial);
if (0 != rc) {
cprintf("ERROR: Failed to read serial number from DS18B20: %d\n", rc);
}
} while (0 != rc);
cprintf("DS18B20 serial number: %02x%02x%02x%02x%02x%02x\n",
serial.id[0], serial.id[1], serial.id[2],
serial.id[3], serial.id[4], serial.id[5]);
while (1) {
int rc;
unsigned long start_tck;
unsigned long end_tck;
unsigned int duration_ms;
int t_c;
start_tck = ulBSP430uptime_ni();
rc = -1;
if (external_power) {
/* Wait for read to complete. Conversion time can be as long as
* 750 ms if 12-bit resolution is used (this resolution is the
* default). Timing will be wrong unless interrupts are enabled
* so uptime overflow events can be handled. Sleep for 600ms,
* then test at 10ms intervals until the result is ready. Each
* check is nominally 61 microseconds. */
while (! iBSP430onewireReadBit_ni(bus)) {
}
} else {
/* Output high on the parasitic power boost line for 750ms, to
* power the conversion. Then switch that signal back to
* input so the data can flow over the same circuit. */
BSP430_PORT_HAL_HPL_OUT(bus->port) |= APP_DS18B20_POWER_BIT;
BSP430_PORT_HAL_HPL_DIR(bus->port) |= APP_DS18B20_POWER_BIT;
BSP430_PORT_HAL_HPL_DIR(bus->port) &= ~APP_DS18B20_POWER_BIT;
}
}
end_tck = ulBSP430uptime_ni();
duration_ms = BSP430_UPTIME_UTT_TO_MS(end_tck - start_tck);
cprintf("%s: ", xBSP430uptimeAsText_ni(end_tck));
if (0 == rc) {
cprintf("Temperature %d dCel or %d d[degF] in %u ms\n",
(10 * t_c) / 16, BSP430_ONEWIRE_xCel_TO_ddegF(t_c),
duration_ms);
} else {
cprintf("Measurement failed in %u ms\n", duration_ms);
}
/* You'd want to do this if you were going to sleep here */
}
}

bsp430_config.h

/* Use a crystal if one is installed. Much more accurate timing
* results. */
#define BSP430_PLATFORM_BOOT_CONFIGURE_LFXT1 1
/* Application does output: support spin-for-jumper */
#ifndef configBSP430_PLATFORM_SPIN_FOR_JUMPER
#define configBSP430_PLATFORM_SPIN_FOR_JUMPER 1
#endif /* configBSP430_PLATFORM_SPIN_FOR_JUMPER */
#define configBSP430_CONSOLE 1
#define configBSP430_UPTIME 1
#define configBSP430_UPTIME_DELAY 1
#if (BSP430_PLATFORM_SURF - 0)
/* SuRF has a DS1825 on P3.7, but the software is the same. We use
* the platform-provided resource. No parasitic power support. */
#define configBSP430_PLATFORM_SURF_DS1825 1
#define APP_DS18B20_BUS xBSP430surfDS1825
#elif (BSP430_PLATFORM_EXP430F5529 - 0)
#define APP_DS18B20_PORT_HAL BSP430_HAL_PORT7
#define APP_DS18B20_BIT BIT7
#ifndef APP_DS18B20_POWER_BIT
#define APP_DS18B20_POWER_BIT BIT1
#endif /* APP_DS18B20_POWER_BIT */
#define configBSP430_HAL_PORT7 1
#else /* BSP430_PLATFORM */
/* External hookup DS18B20 to on P1.6 */
#define APP_DS18B20_PORT_HAL BSP430_HAL_PORT1
#define APP_DS18B20_BIT BIT6
#ifndef APP_DS18B20_POWER_BIT
#define APP_DS18B20_POWER_BIT BIT4
#endif /* APP_DS18B20_POWER_BIT */
#define configBSP430_HAL_PORT1 1
#endif /* BSP430_PLATFORM_SURF */
/* Get platform defaults */

Makefile

PLATFORM ?= exp430fr5739
MODULES=$(MODULES_PLATFORM)
MODULES += $(MODULES_CONSOLE) $(MODULES_UPTIME)
MODULES += periph/port utility/onewire
SRC=main.c
include $(BSP430_ROOT)/make/Makefile.common