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?