void search(onewire_t *ow, uint8_t *id, int depth, int reset) { int i, b1, b2; if (depth == 64) { // we have all 64 bit in this search branch printf("found: "); for (i = 0; i < 8; i++) printf("%02x", id[i]); printf("\n"); return; } if (reset) { if (onewire_reset(ow) != 0) { printf("reset failed\n"); return; } onewire_write_byte(ow, 0xF0); // search ROM command // send currently recognized bits for (i = 0; i < depth; i++) { b1 = onewire_read_bit(ow); b2 = onewire_read_bit(ow); onewire_write_bit(ow, id[i / 8] & (1 << (i % 8))); } } // check another bit b1 = onewire_read_bit(ow); b2 = onewire_read_bit(ow); if (b1 && b2) return; // no response to search if (!b1 && !b2) // two devices with different bits on this position { // check devices with this bit = 0 onewire_write_bit(ow, 0); id[depth / 8] &= ~(1 << (depth % 8)); search(ow, id, depth + 1, 0); // check devices with this bit = 1 id[depth / 8] |= 1 << (depth % 8); search(ow, id, depth + 1, 1); // different branch, reset must be issued } else if (b1) { // devices have 1 on this position onewire_write_bit(ow, 1); id[depth / 8] |= 1 << (depth % 8); search(ow, id, depth + 1, 0); } else if (b2) { // devices have 0 on this position onewire_write_bit(ow, 0); id[depth / 8] &= ~(1 << (depth % 8)); search(ow, id, depth + 1, 0); } }
// // Read a byte // uint8 ICACHE_FLASH_ATTR onewire_read() { uint8 bitMask; uint8 r = 0; for (bitMask = 0x01; bitMask; bitMask <<= 1) { if (onewire_read_bit()) r |= bitMask; } return r; }
uint8_t onewire_read_byte(onewire_t *ow) { int i; uint8_t byte = 0; for(i = 0; i < 8; i++) { byte >>= 1; if (onewire_read_bit(ow)) byte |= 0x80; } return byte; }
unsigned char onewire_read_byte(void) { //Einlesen eines Bytes unsigned char i; unsigned char bytewert = 0; for (i=0; i<8; i++) { if (onewire_read_bit()) { bytewert |= 1 << i; } } return(bytewert); }
int main() { onewire_t ow; int i; uint8_t scratchpad[9]; uint8_t id[8]; WDTCTL = WDTPW + WDTHOLD; //Stop watchdog timer BCSCTL1 = CALBC1_8MHZ; DCOCTL = CALDCO_8MHZ; uart_setup(); ow.port_out = &P1OUT; ow.port_in = &P1IN; ow.port_ren = &P1REN; ow.port_dir = &P1DIR; ow.pin = BIT7; printf("start\n"); search(&ow, id, 0, 1); printf("done\n"); onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0x44); // convert T command onewire_line_high(&ow); // DELAY_MS(850); // at least 750 ms for the default 12-bit resolution // while __delay_cycles(>100000) give the __delay_cycles max value error: for (i = 0; i < 65; i++) __delay_cycles(100000); onewire_reset(&ow); onewire_write_byte(&ow, 0xcc); // skip ROM command onewire_write_byte(&ow, 0xbe); // read scratchpad command for (i = 0; i < 9; i++) scratchpad[i] = onewire_read_byte(&ow); for (i = 0; i < 9; i++) printf("%02x", scratchpad[i]); //meas = scratchpad[0]; // LSB //meas |= ( (uint16_t)scratchpad[1] ) << 8; // MSB int j=0; uint16_t temp = 0; for(j = 16; j > 0; i--){ temp >>= 1; if (onewire_read_bit(&ow)) { temp |= 0x8000; } } if(temp<0x8000){ return(temp*0.0625); } else { temp=(~temp)+1; return(temp*0.0625); } printf("%02x\n", temp); printf("%02x\n", scratchpad[2]); printf("%02x°C\n"); _BIS_SR(LPM0_bits + GIE); return 0; }
/* pass array of 8 bytes in */ uint32 ICACHE_FLASH_ATTR onewire_search(struct onewire_search_state *state) { // If last search returned the last device (no conflicts). if (state->lastDeviceFlag) { init_search_state(state); return FALSE; } // 1-Wire reset if (!onewire_reset()) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_NO_DEVICES; } // issue the search command onewire_write(ONEWIRE_SEARCH_ROM, 0); uint8 search_direction; int32 last_zero = -1; // Loop through all 8 bytes = 64 bits int32 id_bit_index; for (id_bit_index = 0; id_bit_index < 8 * ROM_BYTES; id_bit_index++) { const uint32 rom_byte_number = id_bit_index / BITS_PER_BYTE; const uint32 rom_byte_mask = 1 << (id_bit_index % BITS_PER_BYTE); // Read a bit and its complement const uint32 id_bit = onewire_read_bit(); const uint32 cmp_id_bit = onewire_read_bit(); // Line high for both reads means there are no slaves on the 1wire bus. if (id_bit == 1 && cmp_id_bit == 1) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_NO_DEVICES; } // No conflict for current bit: all devices coupled have 0 or 1 if (id_bit != cmp_id_bit) { // Obviously, we continue the search using the same bit as all the devices have. search_direction = id_bit; } else { // if this discrepancy is before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_index < state->lastDiscrepancy) { search_direction = ((state->address[rom_byte_number] & rom_byte_mask) > 0); } else { // if equal to last pick 1, if not then pick 0 search_direction = (id_bit_index == state->lastDiscrepancy); } // if 0 was picked then record its position in LastZero if (search_direction == 0) { last_zero = id_bit_index; } } // set or clear the bit in the ROM byte rom_byte_number with mask rom_byte_mask if (search_direction == 1) { state->address[rom_byte_number] |= rom_byte_mask; } else { state->address[rom_byte_number] &= ~rom_byte_mask; } // For the current bit position, write the bit we choose to use in the current search. // Any devices that don't match are disabled. onewire_write_bit(search_direction); } state->lastDiscrepancy = last_zero; // check for last device if (state->lastDiscrepancy == -1) { state->lastDeviceFlag = TRUE; } if (crc8(state->address, 7) != state->address[7]) { // Reset the search and return fault code. init_search_state(state); return ONEWIRE_SEARCH_CRC_INVALID; } return ONEWIRE_SEARCH_FOUND; }