void main(void) { // one wire bus struct soft_ow sow_ctx; // temperature reading uint16_t temperature = 0x00; // configure the bus sow_ctx.conf = SOFT_OW_CONF_MAKE(OW_POWER_PARASITE, OW_SINGLEDROP); sow_ctx.bus.port = &OW_PORT; sow_ctx.bus.pin = OW_PIN; // initialize one-wire sow_init(&sow_ctx); // perform HW init 12 bit resolution ds18b20_write_rom(&sow_ctx, NULL, 0x00, 0x00, 0x03, 0x00); // execution loop for (;;) { // start conversion ds18b20_start_conversion(&sow_ctx, NULL); // wait for the conversion to finnish _delay_ms(800); // read the temperature ds18b20_read_scratchpad(&sow_ctx, NULL, (uint8_t *)&temperature, 2); } }
/*---------------------------------------------------------------------------*/ PROCESS_THREAD(ds18b20_poll_process, ev, data) { static struct etimer et; static uint8_t scratchpad[DS18B20_SCRATCHPAD_SIZE]; static ow_rom_code_t id; PROCESS_BEGIN(); printf("\nDS18B20 test\n"); printf("VSEC ON\n"); power_control_vsec_set(1); /* initialize the DS18B20 hardware */ printf("Initialize 1-wire\n"); ow_init(); printf("1-wire READ ROM\n"); id = ow_read_rom(); printf("Initialize DS18B20\n"); ds18b20_init(); printf("DS18B20 init done\n"); /* Poll at 1Hz */ etimer_set(&et, CLOCK_SECOND); while(1) { PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et)); /* Reset the etimer to trig again */ etimer_reset(&et); ds18b20_read_scratchpad(id, scratchpad); ds18b20_convert_temperature(id); } PROCESS_END(); }
void ds18b20_write_rom(volatile struct soft_ow *a_bus, ow_romcode_t *a_romcode, uint8_t a_th, uint8_t a_tl, uint8_t a_res, uint8_t a_force) { uint8_t tmp[5] = { 0x00 }; if (NULL == a_romcode || OW_SINGLEDROP == SOFT_OW_TOPOLOGY_GET(a_bus->conf)) { if (!a_force) { // first read ROM and check if the configuration register // is not already set to the requested value ds18b20_read_scratchpad(a_bus, a_romcode, tmp, sizeof(tmp)); } // build the configuration value tmp[0] = a_th; tmp[1] = a_tl; tmp[2] = ((a_res & 0x03) << 5) | 0x1f; // write only if configuration register differs or force has been // requested if (tmp[4] != tmp[2] || a_force) { sow_reset(a_bus); sow_write_byte(a_bus, OWN_SKIP_ROM); sow_write_byte(a_bus, DS18B20_CMD_WRITE_SP); // save to eeprom for (uint8_t len = 0; len < 3; len++) sow_write_byte(a_bus, tmp[len]); // persistently store in rom sow_reset(a_bus); sow_write_byte(a_bus, OWN_SKIP_ROM); sow_write_byte(a_bus, DS18B20_CMD_COPY_SP); } } }