Microchip MCP342x ADC library
Revision 2:639a5612903f, committed 2016-06-22
- Comitter:
- antoniogonzalez
- Date:
- Wed Jun 22 12:55:48 2016 +0000
- Parent:
- 1:c4da9889ff85
- Child:
- 3:03911aa07029
- Commit message:
- Documentation: Example 2 added
Changed in this revision
mcp342x.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/mcp342x.h Wed Jun 22 12:22:40 2016 +0000 +++ b/mcp342x.h Wed Jun 22 12:55:48 2016 +0000 @@ -40,7 +40,7 @@ * TODO: * - Implement 'conversion mode one-shot' * - * Example: + * Example 1: Minimum functionallity. * @code * #include "mbed.h" * #include "mcp342x.h" @@ -66,6 +66,67 @@ * } * } * @endcode + * + * Example 2: Compare MPC342x with mbed analog input. + * + * In this example a voltage is applied on pin 18 (AnalogOut), and this + * is read by both one of the mbed's analog inputs and by the MCP342x. + * It is useful for checking that the chip is properly connected and + * this library is working as expected, etc. + * @code + * // Connect: + * // - MCP342x CH1- to ground. + * // - MCP342x CH1+ to mbed p18. + * // - mbed ANALOG_IN (defined below) to mbed p18. + * // Then open a terminal. + * #include "mbed.h" + * #include "mcp342x.h" + * + * #define SDA_PIN p28 + * #define SCL_PIN p27 + * #define ANALOG_IN p20 + * #define ANALOG_OUT p18 + * + * I2C i2c(SDA_PIN, SCL_PIN); + * MCP342x mcp_adc(&i2c); + * + * DigitalOut led1(LED1); + * AnalogOut dac(ANALOG_OUT); + * AnalogIn ain(ANALOG_IN); + * + * float mbed_Vin; + * float Vout = 0.1/3.3; + * float mcp_Vin; + * + * int main(){ + * i2c.frequency(400000); + * mcp_adc.set_resolution(MCP342x::RESOLUTION_16); + * + * while(1){ + * // Set the output voltage. + * dac = Vout; + * wait(2); + * + * // Read voltage using both the mbed and the MCP342x. + * led1 = 1; + * mcp_Vin = mcp_adc.read_volts(); + * mbed_Vin = ain.read() * 3.3; + * + * // Display both values. + * printf("\r\nmcp Vin = %.2f\r\n", mcp_Vin); + * printf("mbed Vin = %.2f\r\n", mbed_Vin); + * led1 = 0; + * + * // Increment output voltage by 0.1 V. + * Vout += 0.1/3.3; + * + * // If the output voltage goes above 2.5 V, reset to 0.5 V. + * if (Vout >= 2.5/3.3){ + * Vout = 0.5/3.3; + * } + * } + * } + * @endcode */ class MCP342x {