1
int I2C_Master_ReadReg(unsigned char dev_addr, unsigned char reg_addr, unsigned char count, unsigned char *pData, void (* Sub)(void)) {
    uint8_t cnt;   
    start();    
    sda(dev_addr);
    
    if (!(dev_addr == 0x10)) {
        cnt = 0;
    }
    
    if (count > 0x05) {
        cnt = 0;
    }
   
    if (clk() == 1) {
        I2C_Ack_Error(1);
        return -1;
    }   
    sda(reg_addr + 0);
    if (clk() == 1) {
        I2C_Ack_Error(2);
        return -2;
    }       
    start();
    sda(dev_addr + 1);
    if (clk() == 1) {
        I2C_Ack_Error(3);
        return -3;
    }  
    for (cnt = 0; cnt < count; ++cnt) {  
        *(pData + cnt) = read(); 
        //printf("D: %x\n\r", read());
        if (clk() == 0) {
            I2C_Ack_Error(4);
            return -4;
        }   
        stop();        
    }
    return 0;
}
the busybee
  • 10,755
  • 3
  • 13
  • 30
NoUdemy
  • 11
  • 1
  • Please elaborate a bit more on the error you see, and where in your code you think you have a problem. -- You are stopping after the first read byte (and any additional byte). Is this by purpose? -- BTW, select a serious code style and use it. For now, I have made your source readable. – the busybee Sep 06 '22 at 08:53
  • I am using VEML7700 light sensor to measure lux value and want to talk with my MCU PIC18F46K42. As my MCU is already setup with other sensors and they all are communicating through I2C very well. But, with light sensor it only write the address not the reading address because it does not get stop condition on read. – NoUdemy Sep 07 '22 at 00:41
  • Please take the [tour] to learn how this site works, this is not a forum. If you have additional information, [edit] your question and add it. -- I still do not understand, _which_ of the numerous error exits are done. You might want to provide a [mre]. -- BTW, the I²C stop is done by the PIC, not the sensor. Do you mean "ACK"? On the last byte the sensor does not acknowledge... – the busybee Sep 07 '22 at 06:11

0 Answers0