Test code for LM75AIM

Dependencies:   LM75A mbed-src

Files at this revision

API Documentation at this revision

Comitter:
edodm85
Date:
Wed Jun 27 15:45:09 2012 +0000
Child:
1:7d868212c8c1
Commit message:
Rev 1

Changed in this revision

LM75A/.lib Show annotated file Show diff for this revision Revisions of this file
LM75A/LM75A.cpp Show annotated file Show diff for this revision Revisions of this file
LM75A/LM75A.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.lib Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75A/.lib	Wed Jun 27 15:45:09 2012 +0000
@@ -0,0 +1,1 @@
+ 
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75A/LM75A.cpp	Wed Jun 27 15:45:09 2012 +0000
@@ -0,0 +1,78 @@
+/*
+ *  Autor: Edoardo De Marchi                            
+ *  Date: 04/01/12                                        
+ *  Version: 0.1
+ *  File: LM75.cpp
+ */
+
+#include "LM75A.h"
+
+#define TEMP_REG_ADDR 0x00          // Temperature address
+#define CONFIG_REG_ADDR 0x01        // configuration register
+Serial pc2(USBTX, USBRX);
+
+// costructor
+LM75A::LM75A(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr){
+
+}
+
+
+// destructor
+LM75A::~LM75A(){
+
+}
+
+
+float LM75A::read(){
+
+  const char tempRegAddr = TEMP_REG_ADDR;
+
+  m_i2c.write(m_addr, &tempRegAddr, 1);      // Pointer to the temperature register
+ 
+  char cmd[2] = {0,0};
+  m_i2c.read(m_addr, cmd, 2);        // read temperature register
+  
+  unsigned short val = ((cmd[0] << 8) + cmd[1]) >> 7;     //val = (cmd[ 1 ] << 1) | ( cmd[ 0 ] >> 7 ) ;
+  
+  pc2.printf("reg: ");
+  pc2.printf("%d - %d \n", cmd[1], cmd[0]);         // debug
+  
+  float temp = (float) ((float)val * 0.5);  
+  
+  return temp;
+}
+
+
+char LM75A::read_reg(char addr){
+
+
+    char data[1] = {0};
+    char ret = addr;
+    m_i2c.write(m_addr, &ret, 1);    
+    wait_us(10);         
+    m_i2c.read(m_addr, data, 1);              // Read register content
+    wait_us(20);
+    pc2.printf("reg: %#x \n\r", data[0]);         // debug
+    
+    return ret;
+
+}
+
+
+
+/** Write to specified MMA7660FC register
+*
+* @param char addr: the internal registeraddress of the MMA7660FC
+* @param char data: write data to selected Register
+*/
+void LM75A::write_reg(char addr, char data){
+
+    char data2[2] = {0, 0};
+    
+    data2[0] = addr;
+    data2[1] = data;
+    
+    m_i2c.write(m_addr, data2, 2);             
+    wait_us(50);
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LM75A/LM75A.h	Wed Jun 27 15:45:09 2012 +0000
@@ -0,0 +1,45 @@
+/*
+ *  Autor: Edoardo De Marchi                            
+ *  Date: 04/01/12                                        
+ *  Version: 0.1
+ *  File: LM75.h
+ */
+
+//#ifndef LM75A_H
+//#define LM75A_H
+#pragma once 
+ 
+#include "mbed.h"
+ 
+ 
+/* Library for the LM75A temperature sensor.
+The TLM75A is an I2C digital temperature sensor in a small SOP-8 package, 
+with a 0.5C resolution and 2C accuracy.
+*/
+ 
+class LM75A{        // Creates an instance of the class.
+    public:
+        
+          // Connect module at I2C address addr using I2C port pins sda and scl.
+      LM75A(PinName sda, PinName scl, int addr);
+    
+    
+          // Destroys instance.
+      ~LM75A();
+    
+          // Reads the current temperature.
+      float read();
+      
+      
+      char read_reg(char addr);
+      
+      
+      void write_reg(char addr, char data);
+      
+    
+    private:
+      I2C m_i2c;
+      int m_addr;   
+};
+ 
+// #endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Jun 27 15:45:09 2012 +0000
@@ -0,0 +1,35 @@
+/*
+ * Author: Edoardo De Marchi
+ * Date: 16-05-2011
+ * Notes: Read temperature from LM75AIM
+*/
+
+
+#include "mbed.h"
+#include "LM75A.h"
+
+
+Serial pc(USBTX, USBRX);
+LM75A temp(p28, p27, 0x90);    //SDA, SCL, ADDRESS
+
+
+int main(){
+int n = 0;
+
+  while(1){
+    float var = temp.read();
+    pc.printf("The temp is: %4.2f degree Celsius\n\r", var);
+    temp.read_reg(0x01);
+    if(n == 3){
+        temp.write_reg(0x01, 0x03);
+        n = 0;
+    }else{
+         temp.write_reg(0x01, 0x00);
+    }
+    n++;
+    wait(2);
+  }    
+  
+
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.lib	Wed Jun 27 15:45:09 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/projects/libraries/svn/mbed/trunk@43
\ No newline at end of file