i2c - slave address assignment

Status
Not open for further replies.

spartekus

Member level 5
Joined
May 27, 2010
Messages
81
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Location
Duisburg, Germany
Visit site
Activity points
1,918
Hi all,

i have an i2c program(dspic33f), if i write adress of slave directly, it works well, i get the data,

but if i want to change adress with 2 buttons(as u can see on code). i read zero as data.
and i checked I2C1TRN register, it gets the adress which i am sending, but I2C1RCV is zero. where would be problem? any suggestions?


sequence[2] is array element which i sent to device(I2C1TRN).

// if(!bt2 & !bt1)
// sequence[2] = 0x91;
// else if(!bt2 & bt1)
// sequence[2] = 0x93;
// else if(bt2 & !bt1)
// sequence[2] = 0x95;
// else if(bt2 & bt1)
// sequence[2] = 0x9B;

thanks a lot
 
Last edited:

Depending on the definition of bt1 and bt2, the bitwise and operator will probably not work. I assume you meaned to write a logical and.
 

actually i tried both, & and &&, it didnt change anything.
(bt1 und bt2 were as char defined)
if bt1 and bt2 are ints both & and && should work

try it in a simple program, e.g.
Code:
int main()
{
 int bt1=0, bt2=01 , sequence[2]={0};;
 if(!bt2 & !bt1)
 sequence[2] = 0x91;
 else if(!bt2 & bt1)
 sequence[2] = 0x93;
 else if(bt2 & !bt1)
 sequence[2] = 0x95;
 else if(bt2 & bt1)
 sequence[2] = 0x9B;
 printf("%x\n", sequence[2]);
}
the about give sequence[2] as 0x95
change bt1 and bt2 and the result changes as expected

- - - Updated - - -

so is there anyone, who controlled multi slaves with i2c?
any example code maybe?
have you set the baud rate, mode, etc correctly?

I tend to use the functions in Microchip's i2c.h, e.g.
Code:
// write to I2C2 address array wrptr of length and array wrptr1 of length1, if sendStop is 1 send a STOP at end
int writeI2C2array(const int address, const unsigned char *wrptr, int length,const unsigned char *wrptr1, int length1, int sendStop)
{
   /* Baud rate is set for 100 Khz */
   unsigned int i2cbrg=((SYSCLK/100000L)-(SYSCLK/1111111L))-1;

    //This function will initialize the I2C(1) peripheral.
    //First set the I2C(1) BRG Baud Rate.
    I2C2BRG = i2cbrg;//0x004f;

    //Now we will initialise the I2C peripheral for Master Mode, No Slew Rate
    //Control,ACK enable and leave the peripheral switched off.
    I2C2CON = 0x1210;//0x1200;
    I2C2RCV = 0x0000;
    I2C2TRN = 0x0000;

    //Now we can enable the peripheral
    I2C2CON = /*0x8210;*/0x9200;
//printf("\nwrite I2C2 %#x %0x I2C stat %0x \n", address,i2cbrg,I2C2STAT);
   IdleI2C2();
   StartI2C2();			/* send START */
   
   while(I2C2CONbits.SEN );     /* Wait till Start sequence is completed */

   if(MasterWriteI2C2(I2C2address=address) < 0)
		{ printf("MasterWriteI2C2 Write Slave address fail!\n"); /*while(1);*/ return -1;}	/* Write Slave address and set master for transmission */

   if(MasterPutArrayI2C2(wrptr, length) < 0) return -1;	/* Transmit string of data */

   if(MasterPutArrayI2C2(wrptr1, length1) < 0) return -1;/* Transmit string of data */
   if(!sendStop) return 0;
   StopI2C2();			/* send STOP */

   while(I2C2CONbits.PEN);	/* Wait till stop sequence is completed */
   CloseI2C2();
   return 0;
}
have you an oscilloscope? if so do the signals on the I2C bus look correct
 
hey, thank you for your answer,

i had tried bt1 und bt2, they work fine, adress change when i use buttons.

i had changed freq. of microcontroller but i forget to change value of Baud rate generator. i will be able to do try it on monday. i continiue other programmers code, and he didnt use microchip functions.

thanks again.

- - - Updated - - -

about microchip functions,
MasterputI2C function is needed only, when our slave reads data from slave right? if we only want to read data from slave, we dont need MasterputI2C function?

ist CloseI2C() function necessary?
 
Last edited:

this is my read function
Code:
// read from I2C2 device address
// initially send tx_data (length tx_length), restart and read rx_length bytes into rs_data
int readI2C2(int address, unsigned char *tx_data, int tx_length, unsigned char * rx_data, int rx_length)
{
   //int i;
   unsigned int i2c_data_wait= 11152;
   //printf("\n\rread I2C2 %#x ", address);
   if(writeI2C2(address,tx_data, tx_length, 0) == -1) return -1;;	// send address and register number etc
   RestartI2C2();     							// send RESTART ready for a read!
   while(I2C2CONbits.RSEN );						// wait for restart complete 
   if(MasterWriteI2C2(address+1)<0)return -1;	/* Write Slave address and set master for read */   
   if(MastergetsI2C2(rx_length, rx_data, i2c_data_wait)!=0)return -1;	// read data from slave
   // uart2_puts("\n\rread  ");
   // for(i=0;i<rx_length;i++) { printHex(rx_data[i],uart2_putchar); uart2_putchar(' ');}
   StopI2C2();									/* send stop */  
   while(I2C2CONbits.PEN);	/* Wait till stop sequence is completed */
   CloseI2C2();			// close I2C2 device
   return 0;
}
 

Hi Horace,
thanks for example code, could you tell me, how did u calculate the number 11152?

is it necessary to close i2c with closei2c function, coz i want to read data continiously?

thanks

- - - Updated - - -

there is sth new problem.
i can assign different adresses to slave and read data about 1 minute. after that, i cant assign new adress or i cant read new data. what would you suggest me to try?
thanks
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…