I2C Master Slave

Status
Not open for further replies.

SneaKSz

Member
Hello all,

I made a board with 2 18F14K50's pics, I am able to create pulses on the output of the master , but the slave doesn't recognize this pulses.

---------------- -------------------- -----------------
master SDA - ------ - SDA SDO - -------- SDI DAC
SCL - ------ - SCL/SCK ----- -- --- -------- SCK
--------------- -------------------- ---------------

This is the setup , sending data from the master to the slave , and then the slave would take over the SCL/SCK line ( master I2C is disabled) , and the data to the DAC. Also mark I have 4 slaves with 4 DAC's.


Master code :
Code:
	Delay1KTCYx(3);
	SendStart(); // send S
	SendByte(0x08); // Address
	ACKSTAT(); // successful send?
	SendByte(0xFF); // Address
        ACKSTAT();
	SendStop();// send P (stop)	
	SSPCON1bits.SSPEN=0;
	Delay1KTCYx(3);
        while(1);

This code works , just sending the address and then the data.

This is the slave code :
Code:
	unsigned char counter,i,dummy;
	unsigned char data[10];
	unsigned char data2;
	unsigned char dummy;
	unsigned char i=0;
        unsigned char junk;


void main(void){

init();// setup
while(1){
	if ( PIR1bits.SSPIF){ // dont use the interrupt yet
   //recieve data from master
		data[i]=SSPBUF;//clear BF flag
		i++;
		SSPCON1bits.CKP=1;
         	SPIInit();
                LATBbits.LATB5=0; // /CS low
		Delay1KTCYx(3);
		SendByteSPI(0x10);	// high nibble DAC is zero .
		SendByteSPI(data2[i]);// send the value of master
		Delay1KTCYx(3);
		LATBbits.LATB5=1; // /CS HIGH
		Delay1KTCYx(3);
		init(); // I2C

	PIR1bits.SSPIF=0;
}
}

} // end main	
void init(){
	ADCON1=0x07;//Configure A/D
	SDA=1; // input
	SCK=1;// input
	// SDA & SCL as inputs 
	// TRISB=0;
	// SSPIE is located on the High vector of interrupts
	// compability mode
 //   INTCONbits.GIE = 1;
//    INTCONbits.PEIE = 1;
//   PIE1bits.SSPIE = 1;                      // enable SSP interrupts
    PIR1bits.SSPIF = 0;

	SSPADD=0x08; // address
	SSPSTAT=0b10000000; // Slew Rate control (SMP) 100kHz
	// baud rate 100.000kHz
	SSPCON1= 0b00110110; // I2C Slave mode, 7-bit address , SSPEN enable , release clock
	//SSPCON2bits.SEN  = 0;
  // Disable Address 0x0 General Call
    SSPCON2bits.GCEN = 0;
}

void SPIInit(){
TRISBbits.TRISB5=0; // /CS chip select 
LATBbits.LATB5=1; 

SDO=0; // As output ( datasheet)15.2.3 ENABLING SPI I/O
SCK=0;// SCK (Master mode) must have corresponding TRIS bit cleared
SDA=1;// = SDA

SSPSTAT=0b01000000; // middle
SSPCON1= 0x22; // SSPEN,SPI Master mode, clock = FOSC/64

}

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

void SSPFlag() {
while(SSPSTATbits.BF==0); // wait until sent buffer is empty?

junk=SSPBUF;
}

The SPI code also works , the slave just not seems to get the data. I cant see my fault. Could someone please help me with this bug?

Thx
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…