uint8_t DS18x20_Search_Rom(uint8_t *devices_found, uint8_t (*serial)[ONE_WIRE_DEVICE_NUMBER_MAX][DS18x20_SERIAL_NUM_SIZE]) { unsigned long path, next, pos; uint8_t bit, chk; uint8_t cnt_bit, cnt_byte, cnt_num,tmp; path = 0; cnt_num = 0; do { /* Reset bus */ tmp = One_Wire_Reset(); if (tmp != ONE_WIRE_SUCCESS) { return tmp; } One_Wire_Write_Byte(ONE_WIRE_SEARCH_ROM); next = 0; pos = 1; for (cnt_byte = 0; cnt_byte != 8; cnt_byte++) { (*serial)[cnt_num][cnt_byte] = 0; for (cnt_bit = 0; cnt_bit != 8; cnt_bit++) { bit = One_Wire_Read_Bit(); chk = One_Wire_Read_Bit(); if (!bit && !chk) { if (pos & path) { bit = 1; } else { next = (path & (pos - 1)) | pos; } pos <<= 1; } One_Wire_Write_Bit(bit); if (bit != 0) { (*serial)[cnt_num][cnt_byte] |= (1 << cnt_bit); } } } path = next; cnt_num++; } while(path); /* Write number of devices */ *devices_found = cnt_num; return ONE_WIRE_SUCCESS; }
uint8_t DS18x20_Search_Rom_One_Device(uint8_t* serial[DS18x20_SERIAL_NUM_SIZE]) { uint8_t cnt_bits; uint8_t cnt_bytes; uint8_t tmp; uint8_t tmp2 = 0; uint8_t dev_cnt = 0; /* Reset bus */ tmp = One_Wire_Reset(); if (tmp != ONE_WIRE_SUCCESS) { return tmp; } One_Wire_Write_Byte(ONE_WIRE_SEARCH_ROM); for (cnt_bytes = 0; cnt_bytes != 8; cnt_bytes++) { for (cnt_bits = 0; cnt_bits != 8; cnt_bits++) { tmp = One_Wire_Read_Bit(); if (One_Wire_Read_Bit() == tmp) { dev_cnt++; } One_Wire_Write_Bit(tmp); if (tmp) { tmp2 |= (1 << cnt_bits); } } *serial[cnt_bytes]=tmp2; tmp2=0; } /* Verify CRC checksum */ if (Crc8Dallas(DS18x20_SERIAL_NUM_SIZE, *serial)) { return ONE_WIRE_CRC_ERROR; } return ONE_WIRE_SUCCESS; }
void One_Wire_Write_Byte(unsigned char Byte,GPIO_TypeDef * GPIOx, u16 PINx) { unsigned char cnt; for (cnt=0;cnt!=8;cnt++) One_Wire_Write_Bit(Byte&(1<<cnt),GPIOx, PINx); }