Преглед изворни кода

added OneWire functionality, started with DS1820 Lib. Missing the Temperatur conversion from read and crc data validation

Sebastian пре 7 година
родитељ
комит
8884745ad7
4 измењених фајлова са 247 додато и 0 уклоњено
  1. 63 0
      CoffeeCode/DS18B20.cpp
  2. 18 0
      CoffeeCode/DS18B20.h
  3. 141 0
      CoffeeCode/OneWire.cpp
  4. 25 0
      CoffeeCode/OneWire.h

+ 63 - 0
CoffeeCode/DS18B20.cpp

@@ -0,0 +1,63 @@
+/*
+ * DS18B20.cpp
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+
+
+#define READ_SCRATCH	0xBE
+#define WRITE_SCRATCH	0x4E
+#define READ_ROM	0x33
+#define SKIP_ROM	0xCC
+#define CONVERT_T	0x44
+
+
+#include "DS18B20.h"
+
+/*
+ * | -- 8 BIT CRC -- | -- 48 BIT SERIAL NUMBER -- | -- 8 BIT FAMILY CODE (28h) -- |
+ */
+uint8_t ROM[8] = {0,0,0,0,0,0,0,0};
+double deviceID;
+
+/**
+ * initialize the Temperatur Sensor
+ * This lib is written for only ONE device on the 1-Wire Bus
+ */
+
+void DS18B20_init(void){
+	if(OW_reset()){
+		logger_error("Unable to reset 1-Wire Bus, no Device present %s\n", strerror(errno));
+		return;
+	}
+	DS18B20_readRom();
+	deviceID = (double (ROM[6]) | ROM[5] << 8 | ROM[5] << 16 | ROM[5] << 24 | ROM[5] << 32 | ROM[5] << 40);
+	if(ROM[7] == 0x28){
+		logger(V_HAL, "Found Temperatur Sensor on 1-Wire Bus\n");
+		logger(V_HAL, "Device ID: %12X", deviceID);
+	}
+}
+
+void DS18B20_readRom(void){
+	OW_writeByte(READ_ROM);
+	uint8_t i;
+	for(i = 8; i >= 0; i--){
+		ROM[i] = OW_readByte();
+	}
+}
+
+uint16_t DS18B20_readTemp (void){
+	OW_writeByte(CONVERT_T);
+	while(!OW_readBit()){
+		//wait to finish conversion
+		//TODO don't wait forever
+	}
+	OW_writeByte(READ_SCRATCH);
+	uint8_t scratchPad[9];
+	uint8_t i;
+	for(i = 0; i < 9; i++){
+		scratchPad[i] = OW_readByte();
+	}
+}

+ 18 - 0
CoffeeCode/DS18B20.h

@@ -0,0 +1,18 @@
+/*
+ * DS18B20.h
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#ifndef DS18B20_H_
+#define DS18B20_H_
+
+
+#include <inttypes.h>
+#include "OneWire.h"
+
+
+void DS18B20_readRom(void);
+
+#endif /* DS18B20_H_ */

+ 141 - 0
CoffeeCode/OneWire.cpp

@@ -0,0 +1,141 @@
+/*
+ * OneWire.cpp
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#include "OneWire.h"
+/*
+ * timings in us from
+ * https://www.maximintegrated.com/en/app-notes/index.mvp/id/126
+ */
+uint8_t OWtiming[] = { 6, 64, 60, 10, 9, 55, 0, 480, 70, 410 };
+
+/**
+ * Initializes the 1-Wire communication
+ */
+
+void OneWire_init() {
+	pinMode(OW_PIN, OUTPUT);
+	pullUpDnControl(OW_PIN, PUD_UP);
+	//digitalWrite(SHIFT_G, HIGH);
+}
+
+/**
+ * Writes one byte on the 1-Wire Bus
+ */
+
+void OW_writeByte(uint8_t byte) {
+	uint8_t loop;
+	// Loop to write each bit in the byte, LS-bit first
+	for (loop = 0; loop < 8; loop++) {
+		OW_writeBit(byte & 0x01);
+		// shift the data byte for the next bit
+		byte >>= 1;
+	}
+}
+
+/**
+ * Reads a full byte from the 1-Wire Bus
+ */
+
+uint8_t OW_readByte(void) {
+	uint8_t loop, result = 0;
+	for (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 (OW_readBit())
+			result |= 0x80;
+	}
+	return result;
+}
+
+/**
+ * Simultaneous write and read from the 1-Wire Bus
+ * OW_touchByte(0xFF) is equivalent to OW_readByte()
+ * and OW_touchByte(data) is equivalent to OW_writeByte(data)
+ */
+
+uint8_t OW_touchByte(uint8_t data) {
+	uint8_t loop, result = 0;
+	for (loop = 0; loop < 8; loop++) {
+		// shift the result to get it ready for the next bit
+		result >>= 1;
+
+		// If sending a '1' then read a bit else write a '0'
+		if (data & 0x01) {
+			if (OW_readBit())
+				result |= 0x80;
+		} else
+			OW_writeBit(0);
+
+		// shift the data byte for the next bit
+		data >>= 1;
+	}
+	return result;
+}
+
+/**
+ * function which sends and receives a block of data on the 1-Wire Bus
+ */
+
+void OW_block(uint8_t *data, uint8_t data_len) {
+	int loop;
+	for (loop = 0; loop < data_len; loop++) {
+		OW_touchByte(data[loop]);
+	}
+}
+
+/**
+ * Writes one bit on the 1-Wire Bus
+ */
+
+void OW_writeBit(uint8_t bit) {
+	if (bit) { //1
+		digitalWrite(OW_PIN, LOW);
+		delayMicroseconds(OWtiming[0]);
+		digitalWrite(OW_PIN, HIGH);
+		delayMicroseconds(OWtiming[1]);
+	} else {
+		digitalWrite(OW_PIN, LOW);
+		delayMicroseconds(OWtiming[2]);
+		digitalWrite(OW_PIN, HIGH);
+		delayMicroseconds(OWtiming[3]);
+	}
+}
+
+/**
+ * Reads one bit fromt he 1-Wire Bus and returns it
+ */
+
+uint8_t OW_readBit(void) {
+	digitalWrite(OW_PIN, LOW);
+	delayMicroseconds(OWtiming[0]);
+	digitalWrite(OW_PIN, HIGH);
+	delayMicroseconds(OWtiming[4]);
+	pinMode(OW_PIN, INPUT);
+	uint8_t value = digitalRead(OW_PIN);
+	delayMicroseconds(OWtiming[5]);
+	pinMode(OW_PIN, OUTPUT);
+	return value;
+}
+
+/**
+ * Performs a Reset on the 1-Wire Bus and returns either 1 or 0:
+ * 1: if no device is present and 0 if device(s) are present
+ */
+
+uint8_t OW_reset() {
+	delayMicroseconds(OWtiming[6]);
+	digitalWrite(OW_PIN, LOW);
+	delayMicroseconds(OWtiming[7]);
+	digitalWrite(OW_PIN, HIGH);
+	delayMicroseconds(OWtiming[8]);
+	pinMode(OW_PIN, INPUT);
+	uint8_t value = digitalRead(OW_PIN);
+	delayMicroseconds(OWtiming[9]);
+	pinMode(OW_PIN, OUTPUT);
+	return value;
+}

+ 25 - 0
CoffeeCode/OneWire.h

@@ -0,0 +1,25 @@
+/*
+ * OneWire.h
+ *
+ *  Created on: Feb 28, 2018
+ *      Author: sebastian
+ */
+
+#ifndef ONEWIRE_H_
+#define ONEWIRE_H_
+
+#include <wiringPi.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <inttypes.h>
+#include <unistd.h>
+
+#include "global.h"
+#include "logger.h"
+
+
+#define OW_PIN	5
+
+
+#endif /* ONEWIRE_H_ */