HI, everyone i have met some problem during the interfacing between master and slave connection. i m using PIC C compiler (CCS C compiler) to write the codes. my project is to make the communication between two MCU (PIC16F877A) by using I2C.
the following are the code for master:
#include "C:\Users\Tan Chee How\Desktop\testing I2C\master.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#USE I2C (MASTER, SDA=Device_SDA, SCL=Device_SCL)
int x;
BYTE Data;
void main()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
WHILE(TRUE)
{
if(INPUT(PIN_D1))
OUTPUT_HIGH(PIN_B6);
delay_ms(500);
OUTPUT_LOW(PIN_B6);
delay_ms(500);
OUTPUT_HIGH(PIN_B7);
delay_ms(500);
OUTPUT_LOW(PIN_B7);
for(x=0; x<10; x++)
{
i2c_start();
i2c_write(0xA0);
i2c_write(x);
i2c_write(10-x);
i2c_stop();
delay_ms(100);
i2c_start();
i2c_write(0xA1);
Data=i2c_read(0);
i2c_stop();
delay_ms(100);
}
}
}
and the slave codes:
#include "C:\Users\Tan Chee How\Desktop\testing I2C\slave.h"
#define Device_SDA PIN_C3
#define Device_SCL PIN_C4
#use i2c (slave, SDA= Device_SDA, SCL=Device_SCL,address=0xA0, FORCE_HW)
#INT_SSP
BYTE address, buffer[0x10];
void ssp_interupt ()
{
BYTE incoming, state;
state = i2c_isr_state();
if(state < 0x80)
{
incoming = i2c_read();
if(state == 1)
address = incoming;
if(state == 2)
buffer[address] = incoming;
}
if(state == 0x80)
{
i2c_write(buffer[address]);
}
}
void main ()
{
SET_TRIS_C(0XFF);
SET_TRIS_B(0X00);
SET_TRIS_D(0X00);
OUTPUT_B(0X00);
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
while (TRUE) {}
}
therefore, it doesn't work while downloaded into the MCU and test it in hardware. so, can anyone help me see whether which part of my programming having problem or mistake and give me the reply as soon as possible.
my hardware part is jz two MCU connected wif the pin that reserve for I2C and simple power up MCU connection. besides, in order to show the I2C commincation, i added in the LED light for port B and push button for port D1. when i press the push button of master MCU, the slave LED suppose will be lightning up.
please help me to figure out what's the problem .... thx you very much !!