BSP430  20141115
Board Support Package for MSP430 microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
sensors/ds18b20/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 */
}
}