Forráskód Böngészése

Fixed 1-Wire temperature sensor, reset was missing prior to every command

Philipp Hinz 7 éve
szülő
commit
36eac8db8e
4 módosított fájl, 25 hozzáadás és 11 törlés
  1. 21 7
      CoffeeCode/DS18B20.cpp
  2. 1 1
      CoffeeCode/OneWire.cpp
  3. 1 1
      CoffeeCode/buildno
  4. 2 2
      CoffeeCode/main.cpp

+ 21 - 7
CoffeeCode/DS18B20.cpp

@@ -23,7 +23,7 @@ uint8_t rom[8] = {0,0,0,0,0,0,0,0};
 double deviceID;
 
 /**
- * initialize the Temperatur Sensor
+ * initialize the Temperature Sensor
  * This lib is written for only ONE device on the 1-Wire Bus
  */
 
@@ -37,7 +37,7 @@ void DS18B20_init(void){
 	DS18B20_readRom();
 	deviceID = double (rom[1]);// | 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, "Found temperature sensor on 1-Wire Bus\n");
 		logger(V_HAL, "Device ID: %12X\n", deviceID);
 	}
 }
@@ -51,24 +51,38 @@ void DS18B20_readRom(void){
 }
 
 /**
- * Reads the Temperatur of the DS18B20 Sensor
+ * Transmits a command to the sensor
+ * @param cmd Command byte
+ */
+
+void DS18B20_cmd(uint8_t cmd) {
+	if(OW_reset()){
+		logger_error("Unable to reset 1-Wire Bus, no device present %s\n", strerror(errno));
+	}
+	OW_writeByte(SKIP_ROM);
+	OW_writeByte(cmd);
+}
+
+/**
+ * Reads the Temperature of the DS18B20 Sensor
  * It rounds the value read from the Sensor rounded to the next integer
  */
+
 int8_t DS18B20_readTemp (void){
-	OW_writeByte(SKIP_ROM);
-	OW_writeByte(CONVERT_T);
+	DS18B20_cmd(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;
 	int8_t temp;
+
+	DS18B20_cmd(READ_SCRATCH);
 	for(i = 0; i < 9; i++){
 		scratchPad[i] = OW_readByte();
 	}
-	//Information about temperatur is stored in byte[0] and byte[1]
+	//Information about temperature is stored in byte[0] and byte[1]
 	temp = scratchPad[0] >> 4;
 	temp |= (scratchPad[1] << 4) & 0xF0;
 	return temp;

+ 1 - 1
CoffeeCode/OneWire.cpp

@@ -106,7 +106,7 @@ void OW_writeBit(uint8_t bit) {
 }
 
 /**
- * Reads one bit fromt he 1-Wire Bus and returns it
+ * Reads one bit from the 1-Wire Bus and returns it
  */
 
 uint8_t OW_readBit(void) {

+ 1 - 1
CoffeeCode/buildno

@@ -1 +1 @@
-197
+211

+ 2 - 2
CoffeeCode/main.cpp

@@ -146,7 +146,7 @@ int main(int argc, char *argv[]) {
 
 	for (int i = 0; i < 5; i++) {//TODO this can be simplified!
 		if (i == THREAD_MAIN)
-			rc = pthread_create(&thread[i], NULL, mainLoop, (void *) i);
+			rc = pthread_create(&thread[i], &attr, mainLoop, (void *) i);
 		else if (i == THREAD_STRIPE)
 			rc = pthread_create(&thread[i], NULL, stripeThread, (void *) i);
 		else if (i == THREAD_DISPLAY)
@@ -243,7 +243,7 @@ void *mainLoop(void *threadid) {
 	while (1){
 		sleep(4);
 		int8_t temp = DS18B20_readTemp();
-		logger(V_BASIC, "Temperatur: %d\n", temp);
+		logger(V_BASIC, "Temperature: %d\n", temp);
 	}
 
 	logger(V_BASIC, "Thread goes Sleeping..\n");