interface for the temperature sensor chip adt7320, with sw SPI interface

Dependents:   SPItest sscm

Revision:
3:49638acbc5d4
Parent:
2:2b886cea4fcb
Child:
4:ee71e7bca786
diff -r 2b886cea4fcb -r 49638acbc5d4 adt7320.cpp
--- a/adt7320.cpp	Tue Oct 14 17:10:50 2014 +0000
+++ b/adt7320.cpp	Wed Oct 22 10:30:04 2014 +0000
@@ -1,10 +1,20 @@
 #include "adt7320.h"
 
 
+/* ***************************************************
+ * class implementation to read the  adt7320 temperature sensor via SPI 
+ *
+ * W. Beaumont 
+ * (C) 2014 Universiteit Antwerpen
+
+ ************************************************** */
+
+
+
 #define  CS_ACTIVE 1
 #define  CS_DEACTIVE 0
 
-#define ADT7320_SRC_VER "1.13" 
+#define ADT7320_SRC_VER "1.20" 
 
 
 
@@ -14,6 +24,7 @@
 {
     spi=spiinterface;
     cs=chipselect;
+    Tmode=13 ; // 13 bits temperature reading 
     }
 
 void adt7320::set_spi_mode(u8 nrbyte){
@@ -113,8 +124,22 @@
 }    
 
 
-      void adt7320::init1(){}
-       void adt7320::init2(){}
+float adt7320::getTemperature(){
+    u16 Td = get_T(); bool neg=false;
+    if ( Tmode== 13) Td=Td & 0xFFF8;    
+    if ( Td & 0x8000 ) { // negative 
+        neg =true;         
+        Td= ~Td+1;
+    }
+    float temp = (float)Td /128 ;  // same for 16 and 13 bits mode 
+    if( neg     ) temp=-temp;                    
+
+    return temp; 
+}    
+
+
+void adt7320::init1(){}
+void adt7320::init2(){}