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

Parents
  • 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 ;)

Reply
  • 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 ;)

Children
No Data
Related