One Wire Implementation Question

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, tfAteba

Post Reply
major4x
Posts: 9
Joined: Fri Nov 07, 2014 4:12 am

One Wire Implementation Question

Post 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?
User avatar
Giovanni
Site Admin
Posts: 14895
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: One Wire Implementation Question

Post 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
major4x
Posts: 9
Joined: Fri Nov 07, 2014 4:12 am

Re: One Wire Implementation Question

Post by major4x »

Ah, that is good to know, thanks. I will implement busy waiting with NOPs. Thanks, again.
Post Reply