Satoshi Nihonyanagi / MCP3425AK

Fork of MCP3425 by yasuyuki onodera

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MCP3425.h Source File

MCP3425.h

00001 //**********************
00002 // MCP3425.h for mbed
00003 //
00004 // (C)Copyright 2016 Satoshi Nihonyanagi, All Rights Reserved.
00005 //
00006 // (C)Copyright 2014 All rights reserved by Y.Onodera
00007 // http://einstlab.web.fc2.com
00008 //**********************
00009 
00010 #ifndef MCP3425_H_
00011 #define MCP3425_H_
00012 
00013 #include "mbed.h"
00014 /** MCP3425 class.
00015  *  Used for MCP3425 ADC module from Akizuki-denshi.
00016  *
00017  * Example:
00018  * @code
00019  * #include "mbed.h"
00020  * #include "MCP3425.h"
00021  * 
00022  * I2C i2c(dp13, dp15);
00023  * MCP3425 adc(i2c);
00024  * 
00025  * int main() 
00026  * {
00027  *     adc.set(MCP3425::G1);
00028  *     adc.set(MCP3425::W12);
00029  *     adc.set(MCP3425::CONTINUOUS);
00030  *
00031  *     short c = adc.code();
00032  *     float v = adc.read();
00033  * }
00034  * @endcode
00035  */
00036 
00037 class MCP3425
00038 {
00039 public:    
00040     enum Gain { G1 = 0, G2 = 1, G4 = 2, G8 = 3 };
00041     enum Resolution {W12 = 0, W14 = 1, W16 = 2};
00042     enum Conversion {ONESHOT = 0, CONTINUOUS = 1};
00043 
00044     /** Construct a new instance.*/
00045     MCP3425(I2C& i2c);
00046     
00047     /** Construct a new instance with the specified I2C slave address. 
00048         ( Address of MCP3425 from akizuki-denshi is fixed to 0x0D.)
00049     */
00050     MCP3425(I2C& i2c, int addr);
00051 
00052     /** Specify gain. */
00053     void set(Gain gain);
00054     
00055     /** Specify resolution (sample bits). */
00056     void set(Resolution resolution);
00057     
00058     /** Specify conversion operation mode. */
00059     void set(Conversion conversion);
00060 
00061     /** Return AD code. */
00062     short get();
00063     
00064     /** Return voltage. */
00065     float read();
00066 
00067 private:
00068 
00069     static const int ADDR;
00070     static const float VREF;
00071 
00072     typedef union
00073     {
00074         unsigned short int  Val;
00075         unsigned char v[2];
00076         short S;
00077         struct
00078         {
00079             unsigned char LB;
00080             unsigned char HB;
00081         } byte;
00082     } WORD_VAL;
00083 
00084     typedef union {
00085         unsigned char UC;
00086         struct {
00087             unsigned char G:2;      // 00=1, 01=2, 10=4, 11=8 Gain
00088             unsigned char S:2;      // 00=12bits, 01=14bits, 10=16bits
00089             unsigned char OC:1;     // 0=One-shot, 1=Continuous
00090             unsigned char C:2;      // NA
00091             unsigned char RDY:1;    // wrinting 1=Initiate, reading 0=Ready
00092         } bit;
00093     } CONFIG;
00094 
00095     I2C _i2c;
00096     int _addr;
00097 
00098     CONFIG _cfg;
00099     WORD_VAL _val;
00100 
00101     char _buf[3];
00102 
00103     void _init();
00104     void _write_cfg();
00105     short _get_code();
00106     float _c2v(short code);
00107 };
00108 
00109 #endif /* MCP3425_H_ */
00110 
00111 
00112