GyverLibs / microDS18B20

Легкая и удобная в обращении библиотека для работы с 1-Wire термометрами DS18B20
MIT License
45 stars 11 forks source link

getResolution() #16

Open cubik2k opened 1 year ago

cubik2k commented 1 year ago

added getResolution()

GyverLibs commented 1 year ago

You left sum and crc, it's doesn't make sense :) it would be better to get resolution byte from readTemp routine

cubik2k commented 1 year ago

Yes, you are right, thank you :)

do you mean this: ?

    // прочитать температуру с датчика. true если успешно
    bool readTemp(uint8_t idx = 0) {
        state[idx] = 1;
        if (!oneWire_reset(DS_PIN)) return 0;       // датчик оффлайн
        addressRoutine(idx);                        // Процедура адресации
        oneWire_write(0xBE, DS_PIN);                // Запросить температуру
        uint8_t res = 0;                            // variable for resolution
        uint8_t crc = 0;                            // обнуляем crc
        int16_t temp;                               // переменная для расчёта температуры
        uint16_t sum = 0;                           // контрольная сумма
        for (uint8_t i = 0; i < 9; i++) {           // Считать RAM
            uint8_t data = oneWire_read(DS_PIN);    // Прочитать данные
            sum += data;
            #if (DS_CHECK_CRC == true)
            _ds_crc8_upd(crc, data);                // Обновить значение CRC8
            #endif
            if (i == 0) temp = data;
            else if (i == 1) temp |= (data << 8);
            if (i == 4) res = data;
        }
        if (sum == 0x8F7 || !sum || crc) return 0;  // датчик оффлайн или данные повреждены        
        if (temp != 0x0550) _buf[idx] = temp;       // пропускаем первое чтение (85 градусов)
        if (res != 0x00) _res[idx] = res;           // reading resolution
        return 1;
    }