Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Master slave I²C

Status
Not open for further replies.

SneaKSz

Member
Hello,

I want to have 1 master ( Pic) and several other slaves ( Pic's) and they communicate over I²C .I've been searching for an example ( in C ) , well I found one :

**broken link removed**

But can someone explain to me how I can find the address of the slave µC and the memory address where I can write data?

in the example the address is A0 and A1 ? I dont know why the address changes , there is only 1 slave.


Thanks in advance

SneakSz
 
Hello,

I want to have 1 master ( Pic) and several other slaves ( Pic's) and they communicate over I²C .I've been searching for an example ( in C ) , well I found one :

**broken link removed**

But can someone explain to me how I can find the address of the slave µC and the memory address where I can write data?

in the example the address is A0 and A1 ? I dont know why the address changes , there is only 1 slave.


Thanks in advance

SneakSz

hi,
The A0 and A1 define a WRITE or READ to the devices, check the d/s for the command string.

EDIT:
Also look here:
http://www.best-microcontroller-projects.com/i2c-tutorial.html
 

Attachments

  • AAesp03.gif
    AAesp03.gif
    61.1 KB · Views: 193
Last edited:
Hello Eric,

thanks for the link, i"ve forgot that the LSB was for R/W. I've learned I²C in school and I was able to make successful projects with I²C.

But I dont know how I can find the address of the slave µC's so I can send data to it.

Thanks!
 
It should be in the part's data sheet. Because there are so few address options, some parts have two different address options that can be selected by tieing an external pin on the IC high or low. This allows some flexibility if there is an address conflict with another part on the bus.
 
Hello Eric,

thanks for the link, i"ve forgot that the LSB was for R/W. I've learned I²C in school and I was able to make successful projects with I²C.

But I dont know how I can find the address of the slave µC's so I can send data to it.

Thanks!

hi,
If the Master I2C PIC is the only 'Talker' to the other PIC's via the I2C bus, then you have to program each PIC to recognise its own unique address.

Some devices have a number printed on then indicating their address, others like the DS1307 have a fixed address of 'Dh', others need pins tied high or low.
 
It should be in the part's data sheet. Because there are so few address options, some parts have two different address options that can be selected by tieing an external pin on the IC high or low. This allows some flexibility if there is an address conflict with another part on the bus.

Hello, I'm searching in the datasheet but I"m unable to find the address. I've found the part where they talk about addressing the µC as slave.

link: https://www.electro-tech-online.com/custompdfs/2010/12/39626b.pdf

page 176 (17.4.3 SLAVE MODE) .

But there is nog 7-bit address. I thought the address would be located there.

Thanks
 
Hello, I'm searching in the datasheet but I"m unable to find the address. I've found the part where they talk about addressing the µC as slave.

link: https://www.electro-tech-online.com/custompdfs/2010/12/39626b-1.pdf

page 176 (17.4.3 SLAVE MODE) .

But there is nog 7-bit address. I thought the address would be located there.

Thanks

Hi,
The program in each PIC on the I2C bus will have to read the full received I2C string and compare the address at the string start with its own address as defined in in its own program.

eg: read the START bit then the address bits being sent on the common bus, it then compares the address received with an address stored in its own memory that you have programmed into the PIC. If the stored address matches the received address it for that PIC


EDIT:
What is the project supposed to do.?
 
Last edited:
hi,
If the Master I2C PIC is the only 'Talker' to the other PIC's via the I2C bus, then you have to program each PIC to recognise its own unique address.

Some devices have a number printed on then indicating their address, others like the DS1307 have a fixed address of 'Dh', others need pins tied high or low.

Well I dont want the master to be the talker only , if the slave detects an error , the master must know what happens.

If i have to program each PIC to have their own ID , so I have to send data to all the slaves on the I²C buses and they have to check if the address is the same as the data from the master ?

But each time I send data , each slave has to go to their interrupt routine and look if they are addressed.

Am I thinking correctly?

EDIT : OK !
Thanks
 
Last edited:
Well I dont want the master to be the talker only , if the slave detects an error , the master must know what happens.

If i have to program each PIC to have their own ID , so I have to send data to all the slaves on the I²C buses and they have to check if the address is the same as the data from the master ?

But each time I send data , each slave has to go to their interrupt routine and look if they are addressed.

Am I thinking correctly?

Thanks

hi,
To allow all the PIC's to have bi-directional communications is going to very difficult.

What sort of data rates are you planning.?
 

Attachments

  • 75016084.pdf
    292.7 KB · Views: 379
Thanks for the PDF Eric,

I"m looking in the datasheet to find out how I can use it as a bidirectional multiplexer , to switch the slaves.
 
The PIC18F4620 should have all the goodies you need to setup you network if I understand what you at trying to do.

I would caution you to spend the time it takes to understand section 17 in the datasheet.

For this PIC the I2C address is set in SSPADD.

I2C can be setup as multi master. I2C Bus: Multi-Master .
PIC datasheet said:
The MSSP module in I2C mode fully implements all
master and slave functions (including general call
support) and provides interrupts on Start and Stop bits
in hardware to determine a free bus (multi-master
function).

The section in red is good new. Each PIC on the I2C bus will know when the bus is busy. In reg SSPCON1 there is even a bit to determine collision: WCOL: Write Collision Detect bit.
 
The PIC18F4620 should have all the goodies you need to setup you network if I understand what you at trying to do.

I would caution you to spend the time it takes to understand section 17 in the datasheet.

For this PIC the I2C address is set in SSPADD.

I2C can be setup as multi master. I2C Bus: Multi-Master .


The section in red is good new. Each PIC on the I2C bus will know when the bus is busy. In reg SSPCON1 there is even a bit to determine collision: WCOL: Write Collision Detect bit.

aha thanks , so I have to program in each slave a unique address in SSPADD , so I can address it from the master. I'll try to implement some code ( C) .
 
Good luck. Microchip has provided significant support for what you want to do in hardware. The downside is that unless you spend the time to understand what they have done you will be beating you head against the wall.

Looks like it could be a fun project. Let us know how it goes. And again RTFM.
 
A microcontroller is able to program in any address it wants to have. The problem come in on simple slave devices where the address has to be hard-wired by IC mask hookup.
 
aha thanks , so I have to program in each slave a unique address in SSPADD , so I can address it from the master. I'll try to implement some code ( C) .

hi,
This pdf may be helpful.
 

Attachments

  • 39340011.pdf
    303 KB · Views: 308
A microcontroller is able to program in any address it wants to have. The problem come in on simple slave devices where the address has to be hard-wired by IC mask hookup.

Yes that is a problem.

What sort of simple devices are you looking at. If they are sensors think of adding a Dallas One Wire net to each PIC. Each one wire part as a unique ID from the factory. Common ones measure temperature and voltage. Some just provide a unique address others are switches.

Yeah it is another bus/net to contend with but it is well documented and worth the effort in my book.
 
Last edited:
Hello,

I came up with a master slave program. It has the purpose that the master sends data over I2C to the slave , the slave will generate a interrupt and fetch the data.



I've well documented the code so you guys can understand what I"ve done .

Hopefully someone can give some feedback.

I told you guys I was using the pic18f4620 , but thats on the easypic5 board. First I want to program two pic18f14k50 (one master , other slave).
I've initialized the various registers that are used to setup this protocol.

The code for master and slave are in the zip !
View attachment I2CMasterSlave.zip

EDIT : updated the code ( SSPADD and interrupt vector)

Thanks in advance !
 

Attachments

  • MasterSlaveI2C.zip
    19.6 KB · Views: 128
Last edited:
I've changed the code a little bit :
Master
Code:
// fosc = 12MHz
// Master mode

  #define SCL TRISBbits.TRISB6 // SCL line
  #define SDA TRISBbits.TRISB4 // SDA line 

// methods
void init(); 
void SendStart();
void SendStop();
void SendByte(unsigned char  data);
void ACKSTAT();
void I2CFAIL();
void SSPFlag();

void Strans(unsigned char W);

void main(void){

init();// setup
// random data
while(1){ // keep sending data
SendStart(); // send S
SendByte(0x13); // address ( write)
ACKSTAT(); // sent?
SendByte(0x42); // Register
ACKSTAT();
SendByte(0x62); // data
ACKSTAT();
SendStop();// send P (stop)

	}
}

void init(){
	ADCON1=0x07;//Configure A/D
	TRISCbits.TRISC0=0;
	LATCbits.LATC0=1; // light up
	SDA=1; // input
	SCL=1;// input
	// MSSP registers 
	SSPCON1=0b00101000; // I²C master mode , SSPEN enable
	SSPSTAT=0b10000000; // Slew Rate control (SMP) 100kHz
	// baud rate 100.000kHz
	SSPADD = 29; 
	//(FOSC / (4 * BAUD)) -1 => Calculates SSPADD Settings
	// Serial communication
	TRISCbits.TRISC6=0;//TX
	TRISCbits.TRISC7=1; //RX
	TXSTA=0b00100000;// enable transmit 
	RCSTA=0b10010000;//enable receive + serial port.
	// fosc/(9600*64) -1 
	SPBRG=18; // 9600 bps

}
// The SSPIF flag is checked to ensure the byte has been sent.
void SendStart(){
	SSPCON2bits.SEN=1;
	SSPFlag();
}

void SendStop(){
	SSPCON2bits.PEN=1; // P activate
	SSPFlag();
}

void SendByte(unsigned char data){
	SSPBUF= data;
	SSPFlag();
}

void I2CFAIL(){
	
}

void ACKSTAT(){
	if ( SSPCON2bits.ACKSTAT==1){ // check ackstat
	I2CFAIL(); // STOP
	}
}
void SSPFlag() {
	while(PIR1bits.SSPIF==0); // wait until sent
	PIR1bits.SSPIF=0;
}

void Strans(unsigned char W){
	while(PIR1bits.TXIF==0); // wait until the last bit is gone
	TXREG=W;
}


Slave Code

Code:
// Slave mode

  #define SCL TRISBbits.TRISB6 // SCL line
  #define SDA TRISBbits.TRISB4 // SDA line 

	unsigned char data;

void InterruptHandlerHigh (void);

// High priority interrupt vector

#pragma code InterruptVectorHigh = 0x08 // comp mode default vector
void InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

//----------------------------------------------------------------------------
// High priority interrupt routine

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh ()
{
	if ( PIR1bits.SSPIF==1){
		if (SSPSTATbits.R == 1){    
		data = SSPBUF; // read data from master
		TRISB=data;
		}
	    if (SSPSTATbits.D == 1){             // Data [not address]
        data = SSPBUF;               // get data
        }
	PIR1bits.SSPIF==0;
	}

}

void init(); 
void main(void){

init();// setup
LATBbits.LATB4=1;
while(1);
}

void init (){
ADCON1=0x07;//Configure A/D
SDA=1; // input
SCL=1;// input
// SDA & SCL as inputs 
TRISB=0;
// SSPIE is located on the High vector of interrupts
// compability mode
PIE1bits.SSPIE=1; // MSSP IE
INTCON = 0b11000000; // GIE and PIE enable

// SSPMSK-> No mask
// 7-Bit Slave mode:
//bit 7-1 ADD<6:0>: 7-bit address
//bit 0 Not used: Unused in this mode. Bit state is a “don’t care.”
SSPADD=0x12; // address
SSPSTAT=0b10000000; // Slew Rate control (SMP) 100kHz
// baud rate 100.000kHz
SSPCON1= 0b00110110; // I2C Slave mode, 7-bit address , SSPEN enable , release clock
}
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top