Alberto
Alberto

Reputation: 1

I2C Communication with sensor CCS Compiler

I need to communicate with a sensor via I2C. The sensor's datasheet https://www.hamamatsu.com/resources/pdf/ssd/s11059-02dt_etc_kpic1082e.pdf shows an example of a communication:

I2C example of datasheet

And my code:

#include <18f2550.h>
#fuses   HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#USE     delay(clock=48000000)
#use i2c(Master,Fast,sda=PIN_B0,scl=PIN_B1)

#include <usb_cdc.h>
void main() {
    i2c_start();
    i2c_write(0x54);
    i2c_write(0x00);
    i2c_write(0x89);
    i2c_write(0x54);
    i2c_write(0x00);
    i2c_write(0x09);
    i2c_stop();

    usb_cdc_init();
    usb_init();
    while(TRUE) {
        delay_ms(10);
        i2c_write(0x54);
        i2c_write(0x03);
        i2c_write(0x55);

        int8 RM = i2c_read(1);
        int8 RL = i2c_read(1);
        int8 GM = i2c_read(1);
        int8 GL = i2c_read(1);
        int8 BM = i2c_read(1);
        int8 BL = i2c_read(1);
        int8 IM = i2c_read(1);
        int8 IL = i2c_read(0);
        i2c_stop();

        usb_task();
        if (usb_enumerated()) {
            printf( usb_cdc_putc,"%d", (RM << 8) | RL );                        
            printf(usb_cdc_putc, "\f ");
        }
    }
}

I'd like to know if my code is allright.

Upvotes: 0

Views: 1516

Answers (1)

user3629249
user3629249

Reputation: 16540

in the posted code, the sequencing of I/O between the microprocessor and the device is not correct

The following link is for a different sensor, but does show the correct I2C sequence

You will need to adjust:

  1. the bus address,
  2. the register address,
  3. the command,
  4. and the number of data bytes

for the chip your using: S11059-02DT/-03DS

Strongly suggest using an enum statement or #define statements to give the fixed values meaningful names to make a reader of your code (including your self at some future date) an easy time understanding what the code is doing

Upvotes: 0

Related Questions