Microchip MCP342x ADC library
Diff: mcp342x.h
- Revision:
- 1:c4da9889ff85
- Parent:
- 0:7dbf7356da6b
- Child:
- 2:639a5612903f
--- a/mcp342x.h Wed Jun 15 10:37:24 2016 +0000 +++ b/mcp342x.h Wed Jun 22 12:22:40 2016 +0000 @@ -21,6 +21,8 @@ #include "mbed.h" +#define COMMAND_N_BYTES 5 + /** * @brief Microchip analog-to-digital converter MCP3422/3/4 * @@ -53,13 +55,13 @@ * * while(1){ * // Channel 2 was selected above. Read data from this channel. - * long chan2_val = mcp_adc.read(); + * float chan2_val = mcp_adc.read_volts(); * * // Select now channel 1 and read it. * mcp_adc.set_channel(MCP342x::CHANNEL_1); - * long chan1_val = mcp_adc.read(); + * float chan1_val = mcp_adc.read_volts(); * - * printf("CH1 %i CH2 %i\r\n", chan1_val, chan2_val); + * printf("CH1 %.3f; CH2 %.3f\r\n", chan1_val, chan2_val); * wait(5); * } * } @@ -160,36 +162,43 @@ void set_pga(mcp342x_pga_t pga); /** - * Read the ADC value. The value will be that read from the channel - * set previously (`set_channel`) + * Read the ADC value. The value will be that read from whatever + * channel was set previously (`set_channel`). * * @return Analog measurement in raw data (integer) */ - long read(); + uint32_t read(); /** - * Read the ADC value in volts. The value will be that read from the - * channel set previously (`set_channel`). + * Read the ADC value in volts. The value will be that read from + * whatever channel was set previously (`set_channel`). * * The data are coded in two's complements format, and the final * voltage is also a function of the resolution and gain settings. - * This function follows the equations presented in the device's - * datasheet (Microchip DS22088C), Section 4.9. + * This function follows the equations presented in Section 4.9 of + * the device's datasheet (Microchip DS22088C). * * @return Analog measurement in volts (float) */ float read_volts(); + //void start_conversion(void); For one-shot mode. + private: - uint8_t _address; + // Variables and functions for I2C communication. I2C *_i2c; - uint8_t _resolution; - uint8_t _pga; - char _configuration[1]; + uint8_t _address; + char _i2c_command[COMMAND_N_BYTES]; + uint8_t _configuration; static const uint8_t _device_code = 0b1101; // Hardcoded in factory void _write_configuration(); + + // Variables needed for converting digital output codes to real + // values (i.e. voltage). + mcp342x_resolution_t _resolution; + uint8_t _pga; + float _lsb; + uint32_t _max_code; }; -//void mcp342x_start_conversion(void); For one-shot mode. - #endif