I2C-Master write to Slave

Thread Starter

snowbell

Joined Sep 14, 2009
3
Hi, I am trying to use I2C to send 0x00 or 0x01 from master to slave and the slave will light an LED if it receives 0x01, and not light the LED if it receives 0x00. I tested the code and the LED doesn't light up at all. I'm using PIC 18F4620 and CCS compiler. Anyone knows the reason why? The master and slave codes are as follows:

Master:
Rich (BB code):
#define SLAVE1_WRT_ADDR   0x12 //LSB=0 
#define SLAVE1_READ_ADDR  0x13 //LSB=1 
 
//==================================== 
void main() 
{ 
int8 cmd; 
 
while(1) 
  { 
   if(input(PIN_C0)) 
      cmd = 0x01; 
    else 
      cmd = 0x00; 
    
   i2c_start(); 
   i2c_write(SLAVE1_WRT_ADDR); 
   i2c_write(cmd); 
   i2c_stop(); 
 
   //printf("read %X \n\r", data); 
 
  } 
 
}
Slave:
Rich (BB code):
int8 data; 
 
 
#INT_SSP 
void ssp_interrupt() 
{ 
   int8 incoming, state; 
    
   state = i2c_isr_state(); 
       
   if(state < 0x80)     // Master is sending data 
   { 
      incoming = i2c_read(); 
      data = incoming; 
   } 
    
   if(state >= 0x80)   // Master is requesting data from slave 
   { 
      i2c_write(data); 
   } 
 
} 
 
 
//====================================== 
void main () 
{ 
 
   enable_interrupts(INT_SSP); 
   enable_interrupts(GLOBAL); 
   //set_tris_c(0x01); 
       
   while(1) 
   { 
      if (data == 0x01) 
         output_high(PIN_C0); 
      if (data == 0x00) 
         output_low(PIN_C0); 
   } 
 
}
 

sebgus

Joined Feb 5, 2010
6
In the master you have defined the write-adress as 0x12, but you haven't defined anything in the slave. Haven't worked anything with the CCS compiler but in the SSPADD-register you define the slave-adress.

I programmed two microcontrollers a while a go where the first would send a value from the internal A/D-converter over I2C to the slave which would use the PWM as a D/A-converter to show the voltage on MCU #1s A/D-converter.

Here is the source code for those (poorly commented, but I hope you understand the flow):

Master:

Rich (BB code):
#include <pic18.h>
#include <delay.c>

void Init_I2C(void);
void I2C_Write(char SlaveAdress, char Data);
unsigned char ad8bit(unsigned char kanal);
unsigned char i,j,data;

void main(){
	TRISA=0x01;
	TRISB=0x00;
	Init_I2C();
	while(1)
	{
		RB4=1;  // Turn on a LED on RB4
		data = ad8bit(0);
		I2C_Write(0x40,data); // Send to 0x40 and data as value
		DelayMs(100);
		RB4=0;  // Turn off a LED on RB4 (just to indicate that it passes this loop)
		DelayMs(100);
	}
}
	
unsigned char ad8bit(unsigned char kanal){ // A/D-converter
	kanal<<=3;
	ADCON0=kanal | 0xC1;
	DelayUs(10);
	GODONE=1;
	while(GODONE);
	return ADRESH;
}

void Init_I2C(void){ // Initialize I2C
		TRISC=0xFF;
		SSPCON1=0x28;
		SSPADD=0x28;
}

void I2C_Write(char SlaveAdress, char Data){ //Write-sequence
	SEN=1;
	for(j=0;j<20;j++); 	//Short delay
	SSPBUF=SlaveAdress;
	for(j=0;j<100;j++); 	//Short delay
	SSPBUF=Data;
	for(j=0;j<100;j++); 	//Short delay
	PEN=1;
	for(j=0;j<50;j++); 	//Short delay
}
Slave:
Rich (BB code):
#include <pic18.h>
#include <delay.c>

void Init_I2C(void);
unsigned char I2C_Read2(void);

unsigned char data;

void main(){
	Init_I2C();
	PR2=0xFF;
	TMR2ON=1;
	CCP1CON=0x0C;
	while(1)
	{
		data=SSPBUF; // Get the value from the buffer
		CCPR1L=data; // PWM it out!
	}
}		

void Init_I2C(void){ // Initialize I2C
		TRISC=0xFB;
		SSPCON1=0x36;
		SSPADD=0x40;
}
This code was used on two PIC18F458 with the HI-Tech PICC18 compiler.

Hope you can solve it!
 
Last edited:
Top