void ow_write_byte(uint8_t byte) { uint8_t i; for(i = 0; i < 8; i++) { if (byte & _BV(i)) ow_write_bit(1); else ow_write_bit(0); } }
int8_t ow_read_bit(void) { uint8_t buffer[2], ret; ow_write_bit(1); buffer[0] = ds2482_read_pointer_cmd; buffer[1] = ds2482_status_register; i2c_write(ds2482_address, buffer, 2); ret = i2c_wait(); if (ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } // TODO Prüfe, ob hier write und read in einem schnurz gemacht werden könnte! i2c_write_read(ds2482_address, NULL, 0, buffer, 1); ret = i2c_wait(); if ( ret != TW_NO_INFO) { ERROR(ret); return eOWNoResponse; } if (buffer[0] & 0x20) { return 1; } else { return 0; } }
void ow_byte_wr( uint8_t b ) { for (uint8_t i=8; i>0; i--) { ow_write_bit( b & 1 ); b >>= 1; } }
void ow_write_byte(char val) { unsigned char i; unsigned char temp; for (i = 0; i < 8; i++) { temp = val >> i; temp &= 0x01; ow_write_bit(temp); } delay_usec(119); //wait 120us }
// Write BYTE Function void ow_write(uint8_t byte) { uint8_t i; uint8_t temp; for (i = 0; i < 8; i++) { temp = byte >> i; temp &= 1; ow_write_bit(temp); } }
int8_t ow_write_byte(uint32_t port, uint32_t byte) { uint32_t data = 0; uint8_t i; uint32_t byte_old = byte; for (i=0;i<8;i++) { data |= ow_write_bit(port, (byte & 0x1)) << i; byte >>= 1; } return byte_old == data ? 0 : -1; }
static int8_t ow_convert_t(uint8_t i) { if (i < 8) return ow_write_bit(0x44 & (1 << i)); else { /* Temperature conversion takes max 750 msec. */ ow_release(); ow_delay_ms(750); return 0; } }
uint8_t ow_rom_search( uint8_t diff, uint8_t *id ) { uint8_t i, j, next_diff; uint8_t b; if( ow_reset() ) return OW_PRESENCE_ERR; // error, no device found ow_byte_wr( OW_SEARCH_ROM ); // ROM search command next_diff = OW_LAST_DEVICE; // unchanged on last device i = OW_ROMCODE_SIZE * 8; // 8 bytes do { j = 8; // 8 bits do { b = ow_read_bit(); // read bit if( ow_read_bit() ) { // read complement bit if( b ) // 11 return OW_DATA_ERR; // data error } else { if( !b ) { // 00 = 2 devices if( diff > i || ((*id & 1) && diff != i) ) { b = 1; // now 1 next_diff = i; // next pass 0 } } } ow_write_bit( b ); // write bit *id >>= 1; if( b ) *id |= 0x80; // store bit i--; } while( --j ); id++; // next byte } while( i ); return next_diff; // to continue search }
void ow_write_byte(uint8_t byte) {// >= 560us uint8_t bit; for(bit=0x01;bit!=0;bit<<=1) { ow_write_bit(byte & bit); } }
int ow_search() { int id_bit_number; int last_zero, rom_byte_number, search_result; int id_bit, cmp_id_bit; unsigned char rom_byte_mask, search_direction; // initialize for search id_bit_number = 1; last_zero = 0; rom_byte_number = 0; rom_byte_mask = 1; search_result = 0; search_crc8 = 0; // if the last call was not the last one if (!search_last_device_flag) { // 1-Wire reset if (!ow_reset()) { // reset the search search_last_discrepancy = 0; search_last_device_flag = FALSE; search_last_family_discrepancy = 0; return FALSE; } // issue the search command ow_write_byte(0xF0); // loop to do the search do { // read a bit and its complement id_bit = ow_read_bit(); cmp_id_bit = ow_read_bit(); // check for no devices on 1-wire if ((id_bit == 1) && (cmp_id_bit == 1)) break; else { // all devices coupled have 0 or 1 if (id_bit != cmp_id_bit) search_direction = id_bit; // bit write value for search else { // if this discrepancy if before the Last Discrepancy // on a previous next then pick the same as last time if (id_bit_number < search_last_discrepancy) search_direction = ((search_romcode[rom_byte_number] & rom_byte_mask) > 0); else // if equal to last pick 1, if not then pick 0 search_direction = (id_bit_number == search_last_discrepancy); // if 0 was picked then record its position in LastZero if (search_direction == 0) { last_zero = id_bit_number; // check for Last discrepancy in family if (last_zero < 9) search_last_family_discrepancy = last_zero; } } // set or clear the bit in the ROM byte rom_byte_number // with mask rom_byte_mask if (search_direction == 1) search_romcode[rom_byte_number] |= rom_byte_mask; else search_romcode[rom_byte_number] &= ~rom_byte_mask; // serial number search direction write bit ow_write_bit(search_direction); // increment the byte counter id_bit_number // and shift the mask rom_byte_mask id_bit_number++; rom_byte_mask <<= 1; // if the mask is 0 then go to new SerialNum byte rom_byte_number and reset mask if (rom_byte_mask == 0) { docrc8(search_romcode[rom_byte_number]); // accumulate the CRC rom_byte_number++; rom_byte_mask = 1; } } } while(rom_byte_number < 8); // loop until through all ROM bytes 0-7 // if the search was successful then if (!((id_bit_number < 65) || (search_crc8 != 0))) { // search successful so set LastDiscrepancy,search_last_device_flag,search_result search_last_discrepancy = last_zero; // check for last device if (search_last_discrepancy == 0) search_last_device_flag = TRUE; search_result = TRUE; } } // if no device found then reset counters so next 'search' will be like a first if (!search_result || !search_romcode[0]) { search_last_discrepancy = 0; search_last_device_flag = FALSE; search_last_family_discrepancy = 0; search_result = FALSE; } return search_result; }
static int8_t ow_read_scratch(uint8_t i) { return ow_write_bit(0xbe & (1 << i)); }
static int8_t ow_skip_rom(uint8_t i) { return ow_write_bit(0xcc & (1 << i)); }
int main(void) { // Generate Hamming configuration struct Hamming_config* hamming_config = hamming_generate_config(); uint32_t nb_data = 512; char data_string[] = "01000010011011110110111001101010011011110111010101110010001011000010000001101010011001010010000001110011011101010110100101110011001000000110110001100101001000000111000001110010011001010110110101101001011001010111001000100000011101000110010101111000011101000110010100100000011001000110010100100000011011000010011101101000011010010111001101110100011011110110100101110010011001010010000001100100011001010010000001101100001001110110100001110101011011010110000101101110011010010111010011000011101010010010000000100001"; uint8_t remainder = nb_data % K; // Data number which will not be encoded uint8_t p; struct Matrix* data_loaded = matrix_generate(K, 1); // Send number of data for(uint8_t i = 0; i < 32; i++) ow_write_bit((nb_data & _BV(i)) ? 1 : 0); _delay_us(100); // Send encoded data for(uint32_t i = 0; i < nb_data; i--) { if(i % K == K - 1) { matrix_set(data_loaded, (i % K) + 1, 1, (data_string[nb_data - 1 - i] == 0x31) ? 1 : 0); struct Matrix* data_encoded = hamming_encode(data_loaded, hamming_config); for(p = 0; p < N; p++) //ow_write_bit(matrix_get(data_encoded, p + 1, 1)); matrix_show(data_encoded); matrix_free(data_encoded); _delay_us(10); } else { matrix_set(data_loaded, (i % K) + 1, 1, (data_string[nb_data - 1 - i] == 0x31) ? 1 : 0); data_show(data_loaded->data); } } /* // Remainder if(remainder != 0) { // Void other data than remainder for(p = remainder + 1; p < K; p++) matrix_set(data_loaded, j, 1, 0); // Encode struct Matrix* data_encoded = hamming_encode(data_loaded, hamming_config); // Send by one_wire for(p = 0; p < N; p++) ow_write_bit(matrix_get(data_encoded, p + 1, 1)); matrix_free(data_encoded); } */ matrix_free(data_loaded); return 0; }