8 years, 7 months ago.

I think that I2C Class is not ported to nRF51-DK

When I use the I2C function by the Keil Copiler library, it is working. But when I use the I2C Class, it is not working. When I assign the port to p7(scl) and p30(sda), they are all low states(maybe they are input). When I assigh the port to p6(scl) and p5(sda), they are all low states.

The example code is very simple. But there are no signal at each port.

<<code>>

I2C i2c(p28, p27);

int main() { int address = 0x62; char data[2]; i2c.read(address, data, 2); }

have you used pull up resistors? typically 2K ohm for 400Khz. i2c bus are 'open drain' are you measuring with an Oscilloscope?

posted by Martin Simpson 08 Sep 2015

I find out the problem that I missed the pull up resistors. Thanks alot for your advices.

posted by Kwang Sun Hong 09 Sep 2015

1 Answer

8 years, 7 months ago.

Hello Kwang,

In general you are correct. High level I2C commands (ones that require a data buffer) do not work well with the nRF51822. However, if you re-write your I2C commands using the more simplistic way, I found that those work well. You have to uses i2c.start, i2c.write(data0), i2c.write(data1), i2c.stop and so forth. Example:

//--------------------------------------------------------------------------------------------------------------------------------------//
//generic routine to get temp or humidity from HTU21D. 
//Returns 14 bits of data (anded 0xFFFC) or 0000 if i2c timeout occurs.
//After a read temp or humidity command, HTU21D responds with NACKs until data is ready.
//NOTE: Use non-hold commands

uint16_t htu21d::getData(uint8_t reg) {
    int htu21cnt = 0;           //number of NACKs before ACK or timeout 
    uint16_t htu21data = 0;     //returned data
    int htu21 = 0;              //ACK flag
    _i2c.start();
    htu21 = _i2c.write(HTU21Di2cWRITE);
    _i2c.write(reg);            //read temp, no hold
    _i2c.stop();
    if(htu21 == 0) return 0;    //HTU21T not responding
    do {
        htu21cnt++;
        _i2c.start();
        htu21 = _i2c.write(HTU21Di2cREAD);
        if(htu21 == 1) {
            htu21data = _i2c.read(1) << 8;
            htu21data |= _i2c.read(0) & 0xFC;
            _i2c.stop();
        }
        wait_us(1000);
    } while((htu21cnt < 100) && (htu21 == 0));  //htu21cnt takes 55 to get temp, 16 for humidity (at 1mS loops)
        
    if(htu21 == 0) return 0;    //HTU21D ACK response timed out
    return(htu21data);          //return 14 bit value