Page 1 of 1

One Wire Implementation Question

Posted: Fri Nov 07, 2014 4:16 am
by major4x
Hi,

I am implementing one wire (DS18B20) on an AtMega1280 board. I am trying to reset the sensor with the following code:

Code: Select all

/**
 * Perform the onewire reset function.  We will wait up to 250uS for
 * the bus to come high, if it doesn't then it is broken or shorted
 * and we return a 0;
 *
 * Returns 1 if a device asserted a presence pulse, 0 otherwise.
 */
uint8_t OneWire::reset(void)
{
    palSetPadMode((ioportid_t)IOPORT2, 4, PAL_MODE_INPUT);

    // Wait until the wire is high.
    uint8_t retries = 125;
    do {
        if (--retries == 0) {
            return 0;
        }
        chThdSleepMicroseconds(2);
    } while (palReadPad(IOPORT2, 4) == PAL_LOW);

    palWritePad(IOPORT2, 4, PAL_LOW); // drive output low
    palSetPadMode((ioportid_t)IOPORT2, 4, PAL_MODE_OUTPUT_PUSHPULL);
    chThdSleepMicroseconds(480);

    palSetPadMode((ioportid_t)IOPORT2, 4, PAL_MODE_INPUT);
    chThdSleepMicroseconds(70);
    uint8_t result = palReadPad(IOPORT2, 4);
    chThdSleepMicroseconds(410);

    chprintf((BaseSequentialStream *)&SD1, "%d\r\n", result);

    return result == PAL_LOW;
}


The sensor is connected to pin 10 (PB4) on the board and there is a 4.7L pull-up resistor. What am I doing wrong?

Re: One Wire Implementation Question

Posted: Fri Nov 07, 2014 9:46 am
by Giovanni
Not sure what it is supposed to do but chThdSleepMicroseconds(2) is rounded to the next system tick boundary, if your system tick is 1000hz then you cannot have delays less than 1mS.

giovanni

Re: One Wire Implementation Question

Posted: Fri Nov 07, 2014 6:10 pm
by major4x
Ah, that is good to know, thanks. I will implement busy waiting with NOPs. Thanks, again.