Electronics4you
Member
Hi there
I need some help to understand and build this I²C hello world project.
I have two PIC16F819 with I²C hardware on pin 7 and 10 (right?)
I want them to communicate and send two bytes from the slave to the master. I want to know if my code is right and how to identify the right slave from the master. How to select the right slave if 10 slaves are connected? Do I need to send an adress and make the right slave respond to that - please show an example code.
Here are my small codes (mikroC is used, hope you'll understand)
The schematic is attached
I need some help to understand and build this I²C hello world project.
I have two PIC16F819 with I²C hardware on pin 7 and 10 (right?)
I want them to communicate and send two bytes from the slave to the master. I want to know if my code is right and how to identify the right slave from the master. How to select the right slave if 10 slaves are connected? Do I need to send an adress and make the right slave respond to that - please show an example code.
Here are my small codes (mikroC is used, hope you'll understand)
The schematic is attached
Code:
//SLAVE
int data_array[2] = {0b10110010,0b11110100};
void main() {
TRISB = 0x00;
PORTB = 0x00;
I2C_Init(100000); // Clock frequency
I2C_Start(); // I2C start signal
//????? Send an address
I2C_Wr(data_array[1]); // Write one byte to master
I2C_Wr(data_array[2]); // Write next byte to master
I2C_Stop();
}
Code:
//MASTER
int data_array[2] = {0,0};
void main() {
TRISB = 0xFF;
PORTB = 0x00;
I2C_Init(100000); // Clock frequency
I2C_Start(); // I2C start signal
//????? To which slave
I2C_Rd(data_array[1]); // Read one byte from slave
I2C_Rd(data_array[2]); // Read next byte from slave
I2C_Stop();
}