Skip to content

Commit

Permalink
Major refactoring of main code - Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
nadavmatalon committed Oct 3, 2016
1 parent 669a566 commit c8cb8ff
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 60 deletions.
17 changes: 7 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ As noted above, whichever library you intend to use for this purpose __must be a

The MCP9802 is designed to measure temperature btween -55°C to 125°C (-67°F to 257°F). Measurments below or above this range will return the minimum or maximum measurable value. Concurently, the ability to custom set the HYSTERESIS or LIMIT values has been limited to this range in software (even though logically, these values would need to be al least slightly lower or higher with respect to the actual measurable temperature). Hence, attempts to set these registers with a new value which doesn't fall within the said range will simply do nothing, leaving the current value as is.

3) __Shutdwon & Conversion Mode__
3) __Standby & Conversion Mode__

The first bit of the configuration byte controls the device mode of operation, namely: ON, in which the device operates in 'CONTINUOUS' mode, or OFF - or more precisely HYBERNATE (as I2C communication remains active), in which the device operates in 'SINGLE-SHOT' nmode. As such, setting the 'CONVERSION MODE' of the device to 'CONTINUOUS' will effectively ensure that it is 'ON', while setting it to 'SINGLE-SHOT' mode will turn it OFF (or, rather, put it in hybernate mode).
The first bit of the configuration byte controls the device mode of operation, namely: ON, in which the device operates in 'CONTINUOUS' mode, or OFF - or more precisely STANDBY (as I2C communication remains active) - in which the device operates in 'SINGLE-SHOT' nmode. As such, setting the 'CONVERSION MODE' of the device to 'CONTINUOUS' will effectively ensure that it is 'ON', while setting it to 'SINGLE-SHOT' mode will turn it into STANDBY mode.

4) __Hysteresis & Limit Registers Resolution__

Expand All @@ -72,26 +72,22 @@ The libraty offers the option of getting/setting all termperature values (Abmien

As the MCP9802 was designed to work in a degrees Celsuis scheme, all Fahrenheit values obtained (or custom set by the user) can only represent as close approximations as possible with relation to the Celsius values generated or stored by the device. This limitation is perhaps most noticable when setting the LIMIT or HYSTERESIS registers to custom Fahrenheit values, as a double operation needs to take place, namely: conversion of this figure to the equivalent Celsius value and then rounding that value to the nearest 0.5 degree Celisus (the latter stems from the 9-BIT size of the HYSTERESIS & LIMIT registers as noted above).

7) __Alert Functionality__
7) __Alert Output Signal__

The MCP9802's Alert functionality is based on an 'open collector' architecture which means it requires a pull-up resistor in order to work (this is true for both Alert Types, i.e. 'ACTIVE-LOW' and 'ACTIVE-HIGH). For the purposes of this testing sketch, the Atmega's (weak) internal pull-up resistor is used and so the only connection needed in this context is between the MCP9802's ALERT pin and the Arduino's Digital Pin D2. However, for any real-life use of the device, it is highly recommended to implement a suitable external pull-up resistor (typically 10K) hooked-up betweem the ALERT pin and VCC.

__8. Alert Output__

The MCP9802's Alert functionality is based on an 'open collector' architecture which means it requires a pull-up resistor in order to work (this is true for both Alert Types, i.e. 'ACTIVE-LOW' and 'ACTIVE-HIGH). For the purposes of this testing sketch, the Atmega's (weak) internal pull-up resistor is used and so the only connection needed in this context is between the MCP9802's ALERT pin and the Arduino's Digital Pin D2. However, for any real-life use of the device, it is highly recommended to implement a suitable external pull-up resistor (typically 10K) hooked-up betweem the ALERT pin and VCC.

__9. Device Information String__
8) __Device Information String__

It is now possible to extend the MCP9802 Library to include a function for generating a pritable device information string showing all the relevant details about the devices current Configuration, Limit & Hysteresis settings. As the additional functionality comes at the cost of increased memory usage, it was implemented as an optional add-on rather than added directly to the core MCP9802 Library. See the [MCP9802_Info](https://github.com/nadavmatalon/MCP9802/blob/master/examples/MCP9802_Info/MCP9802_Info.ino) example sketch for detailed explanation and an actual usage demo.

__10. Device I2C Communications String__
9) __Device I2C Communications String__

It is now possible to also extend the MCP9802 Library to include a function for generating a pritable I2C Communications string showing the result of each I2C transaction in a human-friendly way, something that may be useful, for example, during debugging sessions. As the additional functionality comes at the cost of increased memory usage, it was implemented as an optional add-on rather than added directly to the core MCP9802 Library. See the [MCP9802_I2C_Status](https://github.com/nadavmatalon/MCP9802/blob/master/examples/MCP9802_I2C_Status/MCP9802_I2C_Status.ino) example sketch for detailed explanation and an actual usage demo.


## I2C ADDRESSES

Each ADS1110 has 1 of 8 possible I2C addresses (factory hardwired & recognized by its specific part number & top marking on the package itself):
Each MCP9802 has 1 of 8 possible I2C addresses (factory hardwired & recognized by its specific part number & top marking on the package itself):

| PART NO. | BIN | HEX | DEC | MARKING |
|-----------------|----------|------|-----|---------|
Expand Down Expand Up @@ -296,6 +292,7 @@ Please report any issues/bugs/suggestions at the [Issues](https://github.com/nad

- __CORE LIBRARY__: Create interger-math methods for getting/setting Temp/Hyst/Limit
- __DEVICE INFORMATION STRING__: Replace use String class with string class or other alternative (?)
- __COMMUNICATION STATUS RESULT STRING__: Replace use String class with string class or other alternative (?)

## LICENSE

Expand Down
13 changes: 6 additions & 7 deletions examples/MCP9802_I2C_Status/MCP9802_I2C_Status.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,22 @@
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/

#include <MCP9802.h>
#include "MCP9802.h"
#include "utility/MCP9802ComStr.h"

const int MCP9802_ADDR = 0x48; // I2C address of the MCP9802 (Change as needed)

MCP9802 MCP9802(MCP9802_ADDR);
MCP9802 mcp9802(MCP9802_ADDR);

void setup() {
Serial.begin(9600);
Wire.begin();
while(!Serial);
Serial.print(F("\nMCP9802 TEMPERATURE SENSOR\n"));
Serial.print(F("\nCurrent Temp Reading: "));
Serial.print(MCP9802.getTempC(), 1);
Serial.print(F("C\n"));
Serial.print(F("\nI2C Communications Status: "));
Serial.print(MCP9802ComStr(MCP9802));
Serial.print(F("\nCurrent Temp Reading:\t"));
Serial.print(mcp9802.getTemp(), 1);
Serial.print(F("C\n\nI2C Communications Status: "));
Serial.print(MCP9802ComStr(mcp9802));
Serial.print(F("\n\n"));
}

Expand Down
6 changes: 3 additions & 3 deletions examples/MCP9802_Test/MCP9802_Test.ino
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
in default mode).
INPORTANT: This library uses the 'WSWire' library (https://github.com/steamfire/WSWireLib/tree/master/Library/WSWire) for I2C communication with
the ADS1110, so it is NECESSARY to have it installed prior to using the current libraty. Alternatively, if you wish to use the 'Wire' library
the MCP9802, so it is NECESSARY to have it installed prior to using the current libraty. Alternatively, if you wish to use the 'Wire' library
(https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/libraries/Wire) - or any other I2C library for that matter - simply change
the following line the the 'MCP9802.h' file:
#include <WSWire.h>
Expand Down Expand Up @@ -66,10 +66,10 @@
PARTICULAR PURPOSE. See the GNU General Public License for more details.
*/

#include <MCP9802.h>
#include "MCP9802.h"

const int MCP9802_ADDR = 0x48; // DEC: 72 - I2C address of the MCP9802 (Change as needed)
const byte PIN_D2 = 2; // Arduino Digital Pin PIN 2 (connected to the MCP9802's ALERT Pin)
const byte PIN_D2 = 2; // Arduino Digital Pin PIN 2 (connected to the MCP9802 ALERT Pin)
float temp, limit, hyst; // Containers for register data

typedef enum:byte {
Expand Down
69 changes: 33 additions & 36 deletions examples/MCP9802_Usage/MCP9802_Usage.ino
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
1) I2C COMMUNICATION LIBRARY
This library uses the 'WSWire' library (https://github.com/steamfire/WSWireLib/tree/master/Library/WSWire) for I2C communication with
the ADS1110, so it is NECESSARY to have it installed prior to using the current libraty. Alternatively, if you wish to use the 'Wire' library
the MCP9802, so it is NECESSARY to have it installed prior to using the current libraty. Alternatively, if you wish to use the 'Wire' library
(https://github.com/arduino/Arduino/tree/master/hardware/arduino/avr/libraries/Wire) - or any other I2C library for that matter - simply change
the following line the the 'MCP9802.h' file:
Expand All @@ -27,50 +27,47 @@
#include <Wire.h> // or to whatever I2C library name you are using.
As noted above, whichever library you intend to use for this purpose must be alredy installed for the ADS1110 library to work.
As noted above, whichever library you intend to use for this purpose must be alredy installed for the MCP9802 library to work.
2) DEVICE TEMPERATURE RANGE
The MCP9802 is designed to measure temperature btween -55°C to 125°C (-67°F to 257°F). Measurments below or above this range will return
the minimum or maximum measurable value. Concurently, the ability to custom set the HYSTERESIS or LIMIT values has been limited to this
range in software (even though logically, these values would need to be al least slightly lower or higher with respect to the actual
measurable temperature).
The MCP9802 is designed to measure temperature btween -55°C to 125°C (-67°F to 257°F). Measurments below or above this range will return the minimum
or maximum measurable value. Concurently, the ability to custom set the HYSTERESIS or LIMIT values has been limited to this range in software
(even though logically, these values would need to be al least slightly lower or higher with respect to the actual measurable temperature).
Hence, attempts to set these registers with a new value which doesn't fall within the said range will simply do nothing, leaving the current value as is.
3) SHUTDOWN & CONVERSION MODE
The first bit of the configuration byte controls the device mode of operation, namely: ON, in which the device operates in 'CONTINUOUS' mode,
or OFF - or more precisely HYBERNATE (as I2C communication remains active), in which the device operates in 'SINGLE-SHOT' nmode. As such, setting
the 'CONVERSION MODE' of the device to 'CONTINUOUS' will effectively ensure that it is 'ON', while setting it to 'SINGLE-SHOT' mode will turn it OFF
(or more accurately, put it in hybernate mode).
3) STANDBY & CONVERSION MODE
4) HYSTERESIS & LIMIT REGISTERS RESOLUTION
The Temperature register has a setteble range of 9 to 12-BIT (0.5 to 0.0625 degrees Celsius respectively). However, both the LIMIT and HYSTERESIS
registers only have a 9-BIT fixed resolution. This means these registers can only be set with a maximum accuracy of 0.5 degrees Celsius.
Hence, while the relevant functions (i.e. setHyst() and setLimit() ) will happily accept any float value within the premmitted parameter range (-55°C to 125°C)
for either of these two registers, this float value will be automatically rounded to the nearest 0.5C.
The first bit of the configuration byte controls the device mode of operation, namely: ON, in which the device operates in 'CONTINUOUS' mode, or OFF -
or more precisely STANDBY (as I2C communication remains active) - in which the device operates in 'SINGLE-SHOT' nmode. As such, setting the 'CONVERSION MODE'
of the device to 'CONTINUOUS' will effectively ensure that it is 'ON', while setting it to 'SINGLE-SHOT' mode will turn it into STANDBY mode.
5) DEGREES CELSIUS & FAHRENHEIT
The libraty offers the option of getting/setting all termperature values (Abmient [read-only], Limit [read-write] and/or Hysteresis [read-write])
in either degrees Celsuis or Fahrenheit (the default is degrees Celsius).
4) HYSTERESIS & LIMIT REGISTERS RESOLUTION
6) DEGREES FAHRENHEIT ACCURACY LIMITATIONS
As the MCP9802 was designed primerily to work in a degrees Celsuis scheme, all Fahrenheit values obtained (or custom set by the user)
can only represent as close approximations as possible with relation to the Celsius values generated or stored by the device.
This limitation is perhaps most noticable when setting the LIMIT or HYSTERESIS registers to custom Fahrenheit values, as a double
operation needs to take place, namely: conversion of this figure to the equivalent Celsius value and then rounding that value to
the nearest 0.5 degree Celisus (the latter stems from the 9-BIT size of the HYSTERESIS & LIMIT registers as noted above).
The Temperature register has a setteble range of 9 to 12-BIT (0.5 to 0.0625 degrees Celsius respectively). However, both the HYSTERESIS and LIMIT registers
only have a 9-BIT fixed resolution. This means these registers can only be set with a maximum accuracy of 0.5 degrees Celsius. Hence, while the relevant
functions (i.e. setHyst() and setLimit() ) will happily accept any float value within the premmitted parameter range (-55°C to 125°C) for either of these
two registers, this float value will be automatically rounded to the nearest 0.5C.
7) ALERT FUNCTIONALITY
The MCP9802's Alert functionality is based on an 'open collector' architecture which means it requires a pull-up resistor in order to work
(this is true for both Alert Types, i.e. 'ACTIVE-LOW' and 'ACTIVE-HIGH). For the purposes of this testing sketch, the Atmega's (weak) internal
pull-up resistor is used and so the only connection needed in this context is between the MCP9802's ALERT pin and the Arduino's Digital Pin D2.
However, for any real-life use of the device, it is highly recommended to implement a suitable external pull-up resistor (typically 10K) hooked-up
betweem the ALERT pin and VCC.
5) DEGREES CELSIUS & FAHRENHEIT
The libraty offers the option of getting/setting all termperature values (Abmient [read-only], Limit [read-write] and/or Hysteresis [read-write]) in
either degrees Celsuis or Fahrenheit (the default is degrees Celsius).
6) DEGREES FAHRENHEIT ACCURACY LIMITATION
As the MCP9802 was designed to work in a degrees Celsuis scheme, all Fahrenheit values obtained (or custom set by the user) can only represent as close
approximations as possible with relation to the Celsius values generated or stored by the device. This limitation is perhaps most noticable when setting
the HYSTERESIS or LIMIT registers to custom Fahrenheit values, as a double operation needs to take place, namely: conversion of this figure to the equivalent
Celsius value and then rounding that value to the nearest 0.5 degree Celisus (the latter stems from the 9-BIT size of the HYSTERESIS & LIMIT registers
as noted above).
7) ALERT OUTPUT SIGNAL
The MCP9802's Alert output signal is based on an 'open collector' architecture which means it requires a pull-up resistor in order to work (this is true
for both Alert Types, i.e. 'ACTIVE-LOW' and 'ACTIVE-HIGH). For the purposes of this testing sketch, the Atmega's (weak) internal pull-up resistor is used
and so the only connection needed in this context is between the MCP9802's ALERT pin and the Arduino's Digital Pin D2. However, for any real-life use of
the device, it is highly recommended to implement a suitable external pull-up resistor (typically 10K) hooked-up betweem the ALERT pin and VCC.
WIRING DIAGRAM
--------------
Expand Down
4 changes: 0 additions & 4 deletions utility/MCP9802ComStr.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,3 @@ String MCP9802ComStr(const MCP9802& devParams) {
}

#endif

// Check possibility of returning string (lower case - add \null in the end) for both functions

// Check possibility of using 'stream' instead of Serial.print in sketch?

0 comments on commit c8cb8ff

Please sign in to comment.