Microchip MCP342x ADC library

Files at this revision

API Documentation at this revision

Comitter:
SomeRandomBloke
Date:
Thu Sep 24 19:09:51 2020 +0000
Parent:
6:5c60f0b0b1c1
Commit message:
First push

Changed in this revision

mcp342x.cpp Show annotated file Show diff for this revision Revisions of this file
mcp342x.h Show annotated file Show diff for this revision Revisions of this file
--- a/mcp342x.cpp	Thu Oct 04 10:02:12 2018 +0000
+++ b/mcp342x.cpp	Thu Sep 24 19:09:51 2020 +0000
@@ -1,7 +1,11 @@
 #include "mcp342x.h"
 
+//#define DEBUG 1
 #define LEN_ONE_BYTE 1
+
+#ifdef DEBUG
 extern Serial pc;
+#endif
 
 MCP342x::MCP342x(I2C *i2c, uint8_t device_address)
 {
@@ -104,21 +108,25 @@
 
 void MCP342x::_write_configuration()
 {
-//    pc.printf("MCP3424 config %0X\r\n", _configuration );
+#ifdef DEBUG
+    pc.printf("MCP3424 config %0X\r\n", _configuration );
+#endif
     _i2c_command[0] = _configuration;
+    _i2c->frequency( MCP342X_FREQ );
     _i2c->write(_address, _i2c_command, LEN_ONE_BYTE);
 }
 
 void MCP342x::_startRead()
 {
     _i2c_command[0] = _configuration | 0x80;
+    _i2c->frequency( MCP342X_FREQ );
     _i2c->write(_address, _i2c_command, LEN_ONE_BYTE);
 }
 
 uint32_t MCP342x::read()
 {
     uint32_t adc_value = 0;
-    uint8_t readConfig = 0;
+//    uint8_t readConfig = 0;
     int byteNum = 3;
     
     if( (_configuration & 0x10) == 0 )
@@ -129,11 +137,16 @@
         byteNum = 4;
     }
 
+    _i2c->frequency( MCP342X_FREQ );
+
     _i2c->read(_address, _i2c_command, byteNum);
 
     while( (_i2c_command[byteNum-1] & 0x80) == 0x80 ) {
-       wait_ms(10);
-       // pc.printf("MCP3424 config %0X %0X %0X %0X\r\n", _i2c_command[0],_i2c_command[1],_i2c_command[2],_i2c_command[3] );
+       wait_us(1000);
+//       wait_ms(10);
+#ifdef DEBUG
+       pc.printf("MCP3424 config %0X %0X %0X %0X\r\n", _i2c_command[0],_i2c_command[1],_i2c_command[2],_i2c_command[3] );
+#endif
         _i2c->read(_address, _i2c_command, byteNum);
     }
 
--- a/mcp342x.h	Thu Oct 04 10:02:12 2018 +0000
+++ b/mcp342x.h	Thu Sep 24 19:09:51 2020 +0000
@@ -22,6 +22,7 @@
 #include "mbed.h"
 
 #define COMMAND_N_BYTES 5
+#define MCP342X_FREQ 100000
 
 /**
  * @brief Microchip analog-to-digital converter MCP3422/3/4