Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
7 years, 2 months ago.
MCP3421 I2C communication
Dear all,
I am trying to communicate using the NRF51822 with the MCP3421A1T using the I2C bus.
My setup is the following:
SDA_PIN is 9 SCL_PIN is 10
the pull-up resistors are 3kΩ.
My code looks is this:
include the mbed library with this snippet
#include "mbed.h" #define SDA_PIN p9 #define SCL_PIN p10 I2C i2c(SDA_PIN, SCL_PIN); const char addr = 0xD2;// 7bit address of the ADC (for this particular one it is 0x69) on the I2C bus shifted by 1 bit, with the R/W bit set to 0 double readADC(){ char cmd[3]; cmd[0] = 0x88; i2c.start(); int t = i2c.write(addr); int k = i2c.write(cmd[0]); i2c.read( addr, cmd, 3); i2c.stop(); if(k == 0){ long t = cmd[0] << 8 | cmd[1]; if (t >= 32768) t = 65536l - t; double v = (double) t * 2.048/32768.0; return v; }else{ return 10.0; } }
What I get is that there is no acknowledgement from the I2C bus and what is more, even if I change the address of the device I always get the same results, which means that there something not working on this code.
What I want to do is to cpnfigure the register byte of the MCP3421 as 1001000 and then perform a reading after a one-shot measurement.
Does anybody have a clue on this ?
Thank you very much.