Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Q. I am new to embedded system.I have gone through so many datasheets and Now I am starting to learn I2c communication using two controllers (dspic30f2010). How can I know the slave's address which I'll pass from master master one.Anyone please help me. If possible give some more idea about it.
Posted

1 solution

The slave address is specified in the data sheet of the device. Some devices (mainly memory like EEPROM) have address pins to select sub-addresses. If you want to address an EEPROM the address is 1010xxxR/W where 1010 is the pre-defined base address for EEPROMs, xxx corresponds to the three sub-address pins and R/W is the read/write bit specifying the direction. Assuming the address pins are connected to GND, the EEPROM read address is 0xA1 and the write address is 0xA0.

If you are using another microcontroller as slave, you can choose the address from any not reserved and not used by other devices connected to the same bus. Reserved addresses mainly have the 4 upper bits set or cleared (1111xxxx and 0000xxxx). So using any other combination is safe.
 
Share this answer
 
Comments
CPallini 5-Nov-13 8:54am    
5.
Herambashree 6-Nov-13 5:27am    
Dear Jochen Arndt,
Thaks for reply to my question
I am using dsPIC 30F2010 as a slave device.would you please give me detail about reserve address and the how do I know slave address (any example). Is there any sub address pins for it?
May I have to do any register bit configuration for I2CCON AND I2CSTAT and others. If you have idea about it please give detail please.

Thax
Heram
Jochen Arndt 6-Nov-13 5:47am    
With a micro controller as I2C slave, the address is specified by writing it to a register.

See section 16.2 and the following ones in the 30F2010 data sheet. You will find all necessary information there: Valid 7-bit addresses (without R/W bit) are from 0x08 to 0x77. So choose one and write it to the I2CADD register. Because the R/W bit is not part of this address, the master must use the selected value shifted left by one and set the R/W bit when reading.

If you choose the IC2ADD value 0x08 for example, the master must use the adresses 0x10 / 0x11 for write / read.
Herambashree 6-Nov-13 6:14am    
Thaks Jochen Arndt

I shall try and reply you.
Herambashree 6-Nov-13 7:52am    
This is my code here

for slave controller
/////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////
//AUTHOR:ANOOP //
//COMPANY:UNIPAR //
//LOCATION:BANGALORE //
//DSPIC 30F2010 //
//PROJECT:SINEWAVE INVERTER USING DSPIC //
//////////////////////////////////////////////////////////////////////////////////

#define SCL _RF3 // I2C bus
#define SDA _RF2 /*
#define SCL_IN _TRISF3 //
#define SDA_IN _TRISF2 //*/
//#define SCL _TRISF3 //
//#define SDA _TRISF2 //
////Mains sub time duration is 344 Micro sec 2.91 KHz////

#include <p30f2010.h>
#include "init.h"
//#include "DataEEPROM.h"
#include "p30fxxxx.h"
#include <stdio.h>
#include <stdlib.h>

_FOSC(CSW_FSCM_OFF & XT_PLL16);
//_FGS(CODE_PROT_ON);

unsigned char f=0,bit1=0,read_value=0,read=0,count=0,i,data=0;
unsigned int addrs,var,var2=0;
unsigned char a[16] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
unsigned char b[16] = {1,0,1,0,0,0,1,0,1,0,1,0,0,0,1,0};
struct
{
unsigned addrs :1;
}adr;

void read_write();
void read_data();
void communication();
unsigned char I2CRead();

int main(void)
{


PORTEbits.RE0=0;
_TRISE0 = 0;
PORTFbits.RF2=1;
PORTFbits.RF3=1;
_TRISF2 = 1;
_TRISF3 = 1;


///////// void _write_eedata_word(int Mydata2,int Mydata1 );///////
addrs=0X0F;
adr.addrs=0;
var=0;
{
I2CCON =0XA1C0;
I2CSTAT =0X0699;
I2CBRG =0X007;
I2CADD =0X08;
I2CRCV =0X0000;
I2CTRN =0X00FF;


}
while(1){



//while(!SCL);while(SCL);
if((SDA==0) && (SCL ==0)){

while(count<10) {
if(SCL)
count++;
//while(!SCL);
}
data=I2CRead();
if(data >3)_RE0 =1;
}

/*{
for(i=0;i<16;i++){
while(!SCL);
a[i] = SDA;
}
for(i=0;i<16;i++){
if(a[i]==b[i])
count++;
}
if(count>=11){
//_RE0 =1;count=0;
}
}
if(I2CRCV ==0XA2)_RE0 =1;*/
/*_TRISF2 = 1;
_TRISF3 = 1;
if((SDA==0) && (SCL ==0)){
communication();


_TRISF2 = 1;
_TRISF3 = 1;
}*/

}
return 0;
}



unsigned char I2CRead(){
unsigned char i, Data=0;
for(i=0;i<8;i++){
SCL = 0;
SCL = 1;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
}
SCL = 0;
SDA = 1;
return Data;
}


/////////////////////////////////////////////////////
for master controller
////////////////////////



//#include <htc.h>
#define SCL _RF3 // I2C bus
#define SDA _RF2 //

#include <p30f2010.h>


#include "p30fxxxx.h"
#include <stdio.h>
#include <stdlib.h>
unsigned long data1;
_FOSC(CSW_FSCM_OFF & XT_PLL16);
//_FGS(CODE_PROT_ON);
int a;

void I2CInit(){
SDA = 1;
SCL = 1;
}

void I2CStart(){
SCL = 1;
SDA = 0;
SCL = 0;
}

void I2CRestart(){
SDA = 1;
SCL = 1;
SDA = 0;
}

void I2CStop(){
SDA = 0;
SCL = 1;
SDA = 1;
}

void I2CAck(){
SDA = 0;
SCL = 1;
SCL = 0;
}

void I2CNak(){
SDA = 1;
SCL = 1;
SCL = 0;
}

unsigned char I2CSend(unsigned char Data){
unsigned char i, ack_bit;
for(i=0;i<8;i++){
SCL = 0;
if ((Data & 0x80) == 0)
SDA = 0;
else
SDA = 1;
SCL = 1;
Data<<=1;
}
SCL = 0;
ack_bit = SDA;
SCL = 1;
SCL = 0;
return !ack_bit;
}

unsigned char I2CRead(){
unsigned char i, Data=0;
for(i=0;i<8;i++){
SCL = 0;
SCL = 1;
if(SDA)
Data |=1;
if(i<7)
Data<<=1;
}
SCL = 0;
SDA = 1;
return Data;
}

int main(void)
{
PORTEbits.RE0=0;
_TRISE0 = 0;

{
I2CCON =0XB15F;
I2CSTAT =0X46BD;
I2CBRG =0X007;
I2CADD =0XA3;
I2CRCV =0X0000;
I2CTRN =0X00FF;


}
I2CInit();

while(1){
I2CStart();
I2CSend(0xFE);
I2CSend(0xA2
I2CStop();



}
return 0;
}




////////////////////////////////////////////////
I am not getting output here. Is there any mistake here? I am checking the voltage in the RE0 pin of slave controller.

thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900