Working Menu, additions to be made

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
jackmcgarley
Date:
Wed Aug 24 18:48:25 2022 +0000
Parent:
14:ef54ca605b6f
Commit message:
Working menu, additions to be made;

Changed in this revision

TMP102.lib Show diff for this revision Revisions of this file
TMP102/TMP102.cpp Show annotated file Show diff for this revision Revisions of this file
TMP102/TMP102.h Show annotated file Show diff for this revision Revisions of this file
--- a/TMP102.lib	Wed Aug 24 18:45:06 2022 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-https://os.mbed.com/users/eencae/code/TMP102/#e131a649400b
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102/TMP102.cpp	Wed Aug 24 18:48:25 2022 +0000
@@ -0,0 +1,85 @@
+// library implemtation file
+
+// obviously need to include the header
+#include "TMP102.h"
+
+// we now implement each of the methods listed in the header
+
+// note the TMP102:: at the beginning of the method name
+TMP102::TMP102(PinName sda, PinName scl)
+{
+    // in the constructor, we create the mbed API objects using 'new'
+    i2c_ = new I2C(sda,scl);
+    led_ = new DigitalOut(LED_RED);
+}
+
+void TMP102::init()
+{
+    i2c_->frequency(400000); // set bus speed to 400 kHz
+
+    int ack;  // used to store acknowledgement bit
+    char config_data[2];  // array for data
+    char reg = CONFIG_REG;  // register address
+
+    //////// Read current status of configuration register ///////
+
+    ack = i2c_->write(TMP102_W_ADD,&reg,1);  // send the slave write address and the configuration register address
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+
+    ack = i2c_->read(TMP102_R_ADD,config_data,2);  // read default 2 bytes from configuration register and store in array
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+
+    ///////// Configure the register //////////
+
+    // set conversion rate to 1 Hz - CR1 and CR2 are in the second data byte
+    config_data[1] |= (1 << 6);    // set bit 6
+    config_data[1] &= ~(1 << 7);    // clear bit 7
+
+    //////// Send the configured register value to the slave config register ////////////
+
+    // create data packet
+    char data_packet[3] = {reg,config_data[0],config_data[1]};
+
+    ack = i2c_->write(TMP102_W_ADD,data_packet,3);  // send the data packet to the slave write address
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+
+}
+
+void TMP102::read_temperature()
+{
+    int ack;  // used to store acknowledgement bit
+    char data[2];  // array for data
+    char reg = TEMP_REG;  // temperature register address
+
+    ack = i2c_->write(TMP102_W_ADD,&reg,1);  // send temperature register address
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+
+    ack = i2c_->read(TMP102_R_ADD,data,2);  // read 2 bytes from temperature register and store in array
+    if (ack)
+        error();  // if we don't receive acknowledgement, flash error message
+
+    int temperature = (data[0] << 4) | (data[1] >> 4);
+
+    temperature_ = temperature*0.0625F;
+}
+
+// temperature accessor method
+float TMP102::get_temperature() 
+{
+    read_temperature();
+    return temperature_;    
+}
+
+void TMP102::error()
+{
+    while(1) {  // if error, hang while flashing error message
+        led_->write(0);
+        wait(0.2);
+        led_->write(1);
+        wait(0.2);
+    }
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/TMP102/TMP102.h	Wed Aug 24 18:48:25 2022 +0000
@@ -0,0 +1,73 @@
+// it is good practice to put a comment block on the top, with the name and details of the library
+// you can also include version numbers to keep track of changes/additions
+
+/* TMP102
+
+Simple library to get the temperature data from a TMP102 sensor over I2C.
+
+v 1.0 - initial release
+
+(c) Craig A. Evans, University of Leeds, Jan 2016
+
+*/
+
+// this is called a header guard and prevents a library from being included more than once
+// if it is not already defined, it is defined. If it it already defined, it won't be defined again
+// the header guard is closed at the end of the file
+#ifndef TMP102_H
+#define TMP102_H
+
+// the next thing in a library is usually any required defines
+// addresses for ADD0 connected to GND
+#define TMP102_ADD      0x48
+#define TMP102_R_ADD    0x91
+#define TMP102_W_ADD    0x90
+
+// register addresses
+#define TEMP_REG    0x00
+#define CONFIG_REG  0x01
+#define TLOW_REG   0x02
+#define THIGH_REG    0x03
+
+// we need to include the mbed header (this will also have a header guard to prevent it being included more than once)
+#include "mbed.h"
+
+// a library is actually just a C++ class, we will create an instance of this class (an object) in our main code
+class TMP102
+{
+    // we define the methods that are 'public' i.e. able to be used by the user
+public:
+    
+    // this is a 'constructor' and is used to create the object
+    TMP102(PinName sda, PinName scl);
+    
+    // we will also create an initialisation method
+    void init();
+    
+    // and a method to get the temperature - note the return type
+    float get_temperature();
+    
+    // we also have 'private' methods that can be used in the library itself, but not called directly by the user
+private:
+
+    // called in event of error - flashes LED and hangs 
+    void error();
+    // reads temperature from the sensor
+    void read_temperature();
+    
+
+    // we also do the same for any variables
+public:
+    // don't generally allow direct access to variables, instead use 'accessor' and 'mutator' methods
+    
+private:
+
+    // class data member names often have a trailing underscore to make them easily identifiable
+    I2C* i2c_;
+    DigitalOut* led_;
+    float temperature_;
+
+};
+
+// end of the header guard
+#endif
\ No newline at end of file