Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 0:4b472be1b25f, committed 2016-01-22
- Comitter:
- irsanjul
- Date:
- Fri Jan 22 08:44:44 2016 +0000
- Commit message:
- This is my library for IC ADC MPC3424
Changed in this revision
| MCP3424.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MCP3424.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r 4b472be1b25f MCP3424.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3424.cpp Fri Jan 22 08:44:44 2016 +0000
@@ -0,0 +1,145 @@
+#include "MCP3424.h"
+
+extern Serial pc;
+
+MCP3424::MCP3424(PinName sda, PinName scl) : mcp3424_i2c(sda, scl)
+{
+ mcp3424_i2c.frequency(MCP3424_FREQ);
+}
+
+MCP3424::~MCP3424(){
+
+}
+
+bool MCP3424::Config(uint8_t channel, uint8_t resolution, bool mode, uint8_t PGA)
+{
+ bool ok = false;
+ char addr = 0x00;
+ if(mcp3424_i2c.write(MCP3424_ADDR, &addr, 1) != 0)
+ return 0;
+
+ _PGA = PGA;
+ _mode = mode;
+
+ if(resolution != 12 && resolution != 14 && resolution != 16 && resolution != 18)
+ {
+ _resolution = 12;
+ }
+ else _resolution = resolution;
+
+ _cfgbyte = 0;
+ _cfgbyte = _cfgbyte<<2;
+ _cfgbyte |= (channel-1);
+ _cfgbyte = _cfgbyte<<1;
+ _cfgbyte |= _mode;
+ _cfgbyte = _cfgbyte<<2;
+ _cfgbyte |= int((_resolution-12)/2);
+ _cfgbyte = _cfgbyte<<2;
+ _cfgbyte |= 0x00;
+
+ char _send = (char)_cfgbyte;
+
+ if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) ==0)
+ {
+ ok = true;
+ }
+ else
+ {
+ ok = false;
+ }
+ return ok;
+}
+
+bool MCP3424::NewConversion()
+{
+ char _send = (char)(_cfgbyte |= 128);
+ if(mcp3424_i2c.write(MCP3424_ADDR, &_send, 1) != 0)
+ return 0;
+ else return 1;
+}
+
+bool MCP3424::IsConversionFinished()
+{
+ if(_resolution!=18)
+ {
+ _RequestedByte = 3;
+ }
+ else _RequestedByte = 4;
+
+
+ if(mcp3424_i2c.read(MCP3424_ADDR, _recvBuff, _RequestedByte)!= 0) return 0;
+
+ for(_i = 0; _i < _RequestedByte; _i++)
+ {
+ _Buffer[_i] = _recvBuff[_i];
+ }
+
+ _testvariable = _Buffer[_RequestedByte-1]>>7;
+
+ return _testvariable;
+}
+
+
+long MCP3424::Measure()
+{
+ _resultat=0;
+
+ while(IsConversionFinished()==1);
+
+ switch (_resolution)
+ {
+ case 12:{
+ _Buffer[0]&=0b00001111;
+ _resultat = _Buffer[0]*256 + _Buffer[1];
+
+ if(_resultat>2048-1)
+ {
+ _resultat=_resultat-4096-1;
+ }
+
+ _resultat = _resultat*1000/_PGA;
+
+ break;
+ }
+ case 14:{
+ _Buffer[0]&=0b00111111;
+ _resultat = _Buffer[0]*256 + _Buffer[1];
+
+ if(_resultat>8192-1)
+ {
+ _resultat=_resultat-16384-1;
+ }
+
+ _resultat = _resultat*250/_PGA;
+
+ break;
+ }
+ case 16:{
+ _resultat = _Buffer[0]*256 + _Buffer[1];
+
+ if(_resultat>32768-1)
+ {
+ _resultat=_resultat-65536-1;
+ }
+
+ _resultat = _resultat*62.5/_PGA;
+
+ break;
+ }
+ case 18:{
+ _Buffer[0]&=0b00000011;
+ _resultat = _Buffer[0]*512 + _Buffer[1]*256 + _Buffer[2];
+
+ if(_resultat>131072-1)
+ {
+ _resultat=_resultat-262144-1;
+ }
+
+ _resultat = _resultat*15.625/_PGA;
+
+ break;
+ }
+ }
+ return _resultat;
+
+}
\ No newline at end of file
diff -r 000000000000 -r 4b472be1b25f MCP3424.h
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/MCP3424.h Fri Jan 22 08:44:44 2016 +0000
@@ -0,0 +1,45 @@
+/*
+ This Library for ADC MCP3424.
+ I modif from ibrary at https://github.com/battosai30/MCP3424
+ I wish it's working good.
+*/
+
+#ifndef MCP3424_H
+#define MCP3424_H
+
+#include "mbed.h"
+//#include <math.h>
+
+#define MCP3424_ADDR 0xD0
+#define MCP3424_FREQ 100000
+
+class MCP3424 {
+
+public:
+
+ MCP3424(PinName sda, PinName scl);
+ ~MCP3424();
+ bool Config(uint8_t channel,uint8_t resolution,bool mode,uint8_t PGA);
+ bool NewConversion();
+ bool IsConversionFinished();
+ long Measure();
+
+private:
+
+ I2C mcp3424_i2c;
+
+ uint8_t _adresse;
+ long _resultat;
+ uint8_t _resolution;
+ bool _mode;
+ uint8_t _i;
+ uint8_t _testvariable;
+ uint8_t _cfgbyte;
+ uint8_t _PGA;
+ uint8_t _RequestedByte;
+ uint8_t _Buffer[4];
+ char _recvBuff[8];
+ long _res;
+};
+
+#endif
\ No newline at end of file