MCP3021

Dependents:   Telliskivi2_2014

Files at this revision

API Documentation at this revision

Comitter:
Reiko
Date:
Tue Sep 02 15:34:15 2014 +0000
Commit message:
Initial commit

Changed in this revision

MCP3021.cpp Show annotated file Show diff for this revision Revisions of this file
MCP3021.h Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 423652b49d07 MCP3021.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3021.cpp	Tue Sep 02 15:34:15 2014 +0000
@@ -0,0 +1,45 @@
+#include "MCP3021.h"
+ 
+//Create instance
+MCP3021::MCP3021(PinName sda, PinName scl, float supplyVoltage) : i2c(sda, scl), _supplyVoltage(supplyVoltage) {
+}
+ 
+//destroy instance
+MCP3021::~MCP3021() {
+}
+ 
+float MCP3021::read() {
+ 
+//You cannot write to an MCP3021, it has no writable registers.
+//MCP3021 also requires an ACKnowledge between each byte sent, before it will send the next byte. So we need to be a bit manual with how we talk to it.
+//It also needs an (NOT) ACKnowledge after the second byte or it will keep sending bytes (continuous sampling)
+//
+//From the datasheet.
+//
+//I2C.START
+//Send 8 bit device/ part address to open conversation.   (See .h file for part explanation)
+//read a byte (with ACK)
+//read a byte (with NAK)
+//I2C.STOP
+ 
+ 
+  //  char data[2];
+ 
+    i2c.start();
+    int acknowledged = i2c.write(MCP3021_CONVERSE); //send a byte to start the conversation. It should be acknowledged.
+    _data[0] = i2c.read(1);  //read a byte. acknowledge when we have it.
+    _data[1] = i2c.read(0);  //read the second byte. (n)acknowledge when we have it to stop the flow.
+    i2c.stop();
+ 
+    //convert to 12 bit.
+    short res;
+    int _10_bit_var; // 2 bytes
+    char _4_bit_MSnibble = _data[0]; // 1 byte, example 0000 1000
+    char _6_bit_LSByte = _data[1];   // 1 byte, example 1111 0000
+ 
+    _10_bit_var = ((0x0F & _4_bit_MSnibble) << 6) | _6_bit_LSByte >> 2;   //example 100011110000
+    res = _10_bit_var;
+ 
+    return  (_supplyVoltage / 1024) * res;
+ 
+}
\ No newline at end of file
diff -r 000000000000 -r 423652b49d07 MCP3021.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3021.h	Tue Sep 02 15:34:15 2014 +0000
@@ -0,0 +1,41 @@
+#ifndef MCP3021_H
+ 
+#define MCP3021_H
+ 
+#include "mbed.h"
+ 
+#define    MCP3021_CONVERSE 0x9B //10011011 NOTE IT ENDS IN 1, this is the READ ADDRESS. This is all this device does.
+                                 //It opens a conversation via this specific READ address
+ 
+//Library for the MCP3021 12 BIT ADC.
+ 
+class MCP3021
+{
+public:
+ 
+  /*
+  Creates instance
+  Connect module using I2C port pins sda and scl. The output is referenced to the supply voltage which can be
+  2.7v to 5.0v. The read will return the correct voltage, if you supply the correct supplyVoltage when instantiating.
+  */
+  MCP3021(PinName sda, PinName scl, float supplyVoltage);
+  
+  /*
+  Destroys instance.
+  */ 
+  ~MCP3021();
+  
+  /*
+  Reads the analog register of the MCP3021 and converts it to a useable value. (a voltage) 
+  */
+  float read();
+  
+private:
+  
+  I2C i2c;
+  float _supplyVoltage;
+  char _data[2];
+ 
+};
+ 
+#endif
\ No newline at end of file