A cut-down version of https://os.mbed.com/users/Sissors/code/DS1820/ tweaked for use with the STM32F103. It is all generic Mbed operations though, so should be usable anywhere. Non-essential functions have been removed, as this is intended for use within a tutorial.
Dependents: STM32F103C8T6_DS18B20 stm32f103c8t6-ds18b20
Fork of DS1820 by
Diff: DS1820.cpp
- Revision:
- 17:045f96704cc6
- Parent:
- 16:d490e11c466d
- Child:
- 18:3a24aedc1e7c
- Child:
- 19:b9d69bad8185
diff -r d490e11c466d -r 045f96704cc6 DS1820.cpp --- a/DS1820.cpp Thu Jan 11 04:37:42 2018 +0000 +++ b/DS1820.cpp Fri Jan 12 00:38:01 2018 +0000 @@ -1,10 +1,7 @@ #include "DS1820.h" -DigitalOut tracer(PA_6); - static inline void onewire_input(DigitalInOut &pin) { pin.input(); - pin.mode(PullUp); } static inline void onewire_output(DigitalInOut &pin) { @@ -60,18 +57,15 @@ void DS1820::onewire_bit_out (bool bit_data) { onewire_output(_datapin); wait_us(1); // DXP modified from 5 - if (bit_data) { - onewire_input(_datapin); // bring data line high - } else { - wait_us(57); // keep data line low - onewire_input(_datapin); + if (!bit_data) { + wait_us(57); // keep data line low } - wait_us(55); + onewire_input(_datapin); + wait_us(50); } void DS1820::onewire_byte_out(char data) { // output data character (least sig bit first). - int n; - for (n=0; n<8; n++) { + for (int n=0; n<8; n++) { onewire_bit_out(data & 0x01); data = data >> 1; // now the next bit is in the least sig bit position. } @@ -276,30 +270,17 @@ int DS1820::convertTemperature(bool wait, devices device) { // Convert temperature into scratchpad RAM for all devices at once - int delay_time = 750; // Default delay time - char resolution; + int delay_time = 850; // Default delay time if (device==all_devices) skip_ROM(); // Skip ROM command, will convert for ALL devices else { match_ROM(); - if ((FAMILY_CODE == FAMILY_CODE_DS18B20 ) || (FAMILY_CODE == FAMILY_CODE_DS1822 )) { - resolution = RAM[4] & 0x60; - if (resolution == 0x00) // 9 bits - delay_time = 94; - if (resolution == 0x20) // 10 bits - delay_time = 188; - if (resolution == 0x40) // 11 bits. Note 12bits uses the 750ms default - delay_time = 375; - } } - tracer = 0; - onewire_byte_out( 0x44); // perform temperature conversion - tracer = 1; + onewire_byte_out(0x44); // perform temperature conversion if (wait) { wait_ms(delay_time); - delay_time = 0; } return delay_time; } @@ -314,8 +295,9 @@ for(i=0;i<9;i++) { RAM[i] = onewire_byte_in(); } -// if (!RAM_checksum_error()) -// crcerr = 1; + printf("Scratchpad=%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x:%02x\r\n", + RAM[8], RAM[7], RAM[6], RAM[5], RAM[4], RAM[3], RAM[2], RAM[1], RAM[0]); + printf("Checksum is %s\r\n", RAM_checksum_error() ? "bad" : "good"); } bool DS1820::setResolution(unsigned int resolution) {