This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

I2c protocol.

Dear Sir,

i am using pca10001.

Actually i tried to read and write the data from EEPROM having address 0x50 though i2cbus.

i made po.0 and p0.1 as output pin then clk and sda pin respectively.

i done every thing. but once i start NWF_TWI0->STARTTX=1;

i am not geeting ack from slave.

infact ERRORSRC=2 and EVENTS_ERROR=1.

Is it needed to make data pin again as input to get ack from slave.

and is needed to send slave address or just put in ADDRESS register is sufficient. Kindly suggest me.

if you want i will post my code also.

Regards Balaji

  • Hi, you can have a look at the twi_hw_master.c example in the sdk folder. I've just used that functions to communicate with a Microchip EEPROM (according to I2C address i imagine it's the same are using you, maybe just the size is different). Follow the snippets of code to write EEPROM:

    
    uint8_t addw = 0xa0;
    uint8_t dataW[3] = {0x5d, 0x00, 0xfb};
    if(twi_master_transfer(addw, dataW, 3, true))
    	simple_uart_putstring((const uint8_t *)" \n\rOK\n\r");
    else
    	simple_uart_putstring((const uint8_t *)" \n\rERROR\n\r");
    
    

    And to read the EEPROM:

    
    uint8_t addr = 0xa1;
    uint8_t dataR[2] = {0x5d, 0x00};
    //	Writing device address and register
    twi_master_transfer(addw, dataW, 2, true);
    //	Writing device address and reading data
    twi_master_transfer(addr, dataR, 1, true);
    
    

    A little explanation, if the EEPROM address is 0x50, you have to left shift one position and add 1 to read bytes, and simply left shift to read, so 0x50 become 0xA0 to write and 0xA1 to read from the EEPROM. Register address is two bytes long (if the EEPROM is bigger than 65KB, look at the datasheet of your model), in my examples i am writing the value 0xFB to the register 0x5D00, then i read the value from the same register. If you go in debug mode and watch the dataR first element, you should find the correct value after reading... i hope it helps! Ops, i forgot to say... for that example SCL and SDA are the pins 24 and 25 of pca10001 ;)

  • hi, i tried your approach above shown, but i was not able to read the data from the sensor, here in my case i am integrating sensor instead of EEPROM, i tried both hw and sw TWI

    ERRORSRC=2 and EVENTS_ERROR=1.

    me too got the same while debugging, what might be the problem....

  • Tried with Keeping delay of max 400us. after every write and read.

    and also check with both debugging mode and by single run.

    if it does not work,check you are passing correct device id or not.

    Regards Balaji

  • I did that one, i am giving 1 sec delay b/w write and read, while debugging, the PSELSCL = 0X00000018, PSELSDA = 0X00000019

    showing the pin numbers as 18 and 19, but my actual pins i am using are -

    #define TWI_MASTER_CONFIG_CLOCK_PIN_NUMBER (24U) #define TWI_MASTER_CONFIG_DATA_PIN_NUMBER (25U)

    24 and 25, why it is showing like that ?

    even i tried to connect to the 18 and 19 pins but not getting any thing!

  • i tried with accelerometer sensor. when i give delay its working fine.

    if you send code it will be easy to find fault.

    Regards Balaji

Related