View previous topic :: View next topic |
Author |
Message |
scanan
Joined: 13 Aug 2004 Posts: 59 Location: Turkey
|
[SOLVED] about TCA9535 |
Posted: Wed Oct 05, 2016 8:20 am |
|
|
Hi,
I have connected a TCA9535 to my pic18F6310
using ccs 4.135.
Every setup of my i2c is ok. I can read from my eeprom etc.
I think I have initiating problem of TCA9535.
When I read the input of the TCA9535 I get all 1, what could be the problem?
My code is as follows:
Code: |
void main(void)
{
char a;
init_prog();
sleep();
IOpin.modulepower=0;
enable_interrupts(INT_TIMER1);
//------------------------------------------------------------------------------
for(;;)
{
// set port0 as input
i2c_start();
i2c_write(0x40);
i2c_write(0x06);
i2c_write(0xFF);
i2c_stop();
// set port1 as input
i2c_start();
i2c_write(0x40);
i2c_write(0x07);
i2c_write(0xFF);
i2c_stop();
// set port0 as no polarity
i2c_start();
i2c_write(0x40);
i2c_write(0x04);
i2c_write(0x00);
i2c_stop();
// set port1 as no polarity
i2c_start();
i2c_write(0x40);
i2c_write(0x05);
i2c_write(0x00);
do{
// read port0
i2c_start();
i2c_write(0x41);
i2c_write(0x00);
i2c_start();
rfdata1=i2c_read(1);
i2c_stop();
//read port1
i2c_start();
i2c_write(0x41);
i2c_write(0x01);
i2c_start();
rfdata2=i2c_read(1);
i2c_stop();
printf("rfdata1=%u\r\n",rfdata1);
printf("rfdata2=%u\r\n",rfdata2);
}while(1==1);
}
|
_________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional.
Last edited by scanan on Wed Oct 05, 2016 8:54 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Wed Oct 05, 2016 8:50 am |
|
|
Look at Figure 29. You are not handling the read correctly.
In I2C, the standard sequence for read, is:
Send start
Send write address of chip
Send register address you want to talk to.
Send restart
Send read address of chip
Then you start actually reading.
This is what figure 29 shows.
Code: |
// read port0
i2c_start();
i2c_write(0x40); //I2C write address of chip
i2c_write(0x00); //write the register address
i2c_start(); //now the restart
i2c_write(0x41); //only now select the 'read' I2C device
rfdata1=i2c_read(1);
i2c_stop();
|
You are trying to write the register address to a device set to read. With I2C, as soon as the address byte is transferred with the R/W bit set 'read', the bus switches round. |
|
|
scanan
Joined: 13 Aug 2004 Posts: 59 Location: Turkey
|
|
Posted: Wed Oct 05, 2016 8:54 am |
|
|
many thanks works fluently _________________ Dr Suleyman CANAN
R&D Electronic Engineer
https://suleymancanan.wordpress.com
Do whatever you do with amateur spirit -
But always feel professional. |
|
|
|