DS18B20 test program
original:
https://developer.mbed.org/users/wkinkeldei/code/TempMeasure/file/9e88b2508768/one_wire.cpp
https://developer.mbed.org/users/wkinkeldei/code/TempMeasure/
one_wire.cpp
- Committer:
- va009039
- Date:
- 2015-08-31
- Revision:
- 1:c03fe1e5f435
- Parent:
- 0:c85bb83259cc
File content as of revision 1:c03fe1e5f435:
#include "one_wire.h" /* "borrowed" from snatch59: OneWireCRC and simplified for my needs */ const int timing[] = {6, 64, 60, 10, 9, 55, 0, 480, 70, 410}; #if defined(TARGET_LPC812) static int timing_raw[sizeof(timing)/sizeof(int)]; static void wait_raw_setup_lpc800() { uint32_t us_clk = SystemCoreClock / 1000000; for(int i = 0; i < sizeof(timing)/sizeof(int); i++) { timing_raw[i] = timing[i] * us_clk; } LPC_SYSCON->SYSAHBCLKCTRL |= (1<<10); // enable MRT LPC_SYSCON->PRESETCTRL |= (1<<7); // reset MRT LPC_MRT->CTRL3 |= (1<<1); // one-shot } static void wait_raw_lpc800(uint32_t timeout_raw) { if (timeout_raw != 0) { LPC_MRT->INTVAL3 = timeout_raw | (1UL<<31); // and LOAD LPC_MRT->STAT3 |= 0x01; while(!(LPC_MRT->STAT3 & 1)) { ; } } } #define wait_us wait_raw_lpc800 #define timing timing_raw #else void wait_raw_setup(){} #endif OneWire::OneWire(PinName pin) : port(pin) { wait_raw_setup_lpc800(); } int OneWire::reset() { int result = 0; // sample presence pulse result wait_us(timing[6]); port.output(); port = 0; wait_us(timing[7]); port.input(); wait_us(timing[8]); result = !(port & 0x01); wait_us(timing[9]); return result; } void OneWire::write_bit(int bit) { bit = bit & 0x01; if (bit) { // Write '1' bit port.output(); port = 0; wait_us(timing[0]); port.input(); wait_us(timing[1]); } else { // Write '0' bit port.output(); port = 0; wait_us(timing[2]); port.input(); wait_us(timing[3]); } } int OneWire::read_bit() { int result; port.output(); port = 0; wait_us(timing[0]); port.input(); wait_us(timing[4]); result = port & 0x01; wait_us(timing[5]); return result; } void OneWire::write_byte(int data) { // Loop to write each bit in the byte, LS-bit first for (int loop = 0; loop < 8; loop++) { write_bit(data & 0x01); // shift the data byte for the next bit data >>= 1; } } int OneWire::read_byte() { int result = 0; for (int loop = 0; loop < 8; loop++) { // shift the result to get it ready for the next bit result >>= 1; // if result is one, then set MS bit if (read_bit()) result |= 0x80; } return result; } void OneWire::block(char* data, int data_len) { for (int loop = 0; loop < data_len; loop++) { data[loop] = read_byte(); // original: touchByte(data[loop]); } } void OneWire::prepare_read() { reset(); write_byte(SKIP_ROM); write_byte(CONVERT); // switch on parasite power while waiting. port.output(); port = 1; } int OneWire::read_temperature() { char data[9]; short *raw_temp = (short *) data; int temperature = -99; reset(); write_byte(SKIP_ROM); write_byte(READSCRATCH); block(data, 9); if (data[5] == 255 && data[7] == 16) { temperature = *raw_temp >> 4; } return temperature; } int OneWire::read_temperature_raw(uint8_t data[], size_t size) { short *raw_temp = (short *) data; int temperature = -99; reset(); write_byte(SKIP_ROM); write_byte(READSCRATCH); block((char*)data, size); if (data[5] == 255 && data[7] == 16) { temperature = *raw_temp >> 4; } return temperature; }