CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

I2C Between Two 18F4431

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Carl Eldredge



Joined: 07 Dec 2007
Posts: 2
Location: Rolla, MO

View user's profile Send private message Visit poster's website

I2C Between Two 18F4431
PostPosted: Fri Dec 07, 2007 12:07 pm     Reply with quote

Hi,
I'm a neophyte in programming. Could someone please look at this code and tell me if they see anything wrong. I can't get it to work properly. A few times I was able to write to the slave, but it would never respond.

Thanks a lot.

Code for the Master:

#include <18F4431>
#fuses HS,NOPROTECT,BROWNOUT,NOWDT,BORV42
#use delay(clock=10M)
#use i2c(Master, sda=PIN_C4, scl=PIN_C5, SLOW=100000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int ack;
//BYTE SSPADD = 24; // 100khz

////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void receive(int number)
{
if(!ack==0) // Acked?
{
printf(" # %x ERROR!!\n\r",number); // If not: ERROR
}
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////

void read_write(void) // Write data and the read it.
{
BYTE data;

printf("Starting to write...\n\r");
delay_ms(1000);

i2c_start();
ack = i2c_write(0xa0); // Control byte (write)
receive(1);
ack = i2c_write(0x00); // Address
receive(2);
ack = i2c_write(0x42); // Write Data
receive(3);
i2c_stop(); // End


printf("If there were no errors, the write was successful.\n\r");
printf("Short delay...\n\r");

delay_ms(1000); // Allow 94 ms extra for safety

///////////////////////////READING//////////////////////////////////////////////

printf("Read is beginning...\n\r");
delay_ms(1000);
i2c_start();
ack=i2c_write(0xa0);
receive(1);
ack=i2c_write(0x00);
receive(2);
i2c_start();
ack=i2c_write(0xa1);
receive(3);
data = i2c_read(0);
i2c_stop();
printf("Read complete\n\r");

printf("Data: /%c/ \n\r",data);


}
///////////////////////////////////////////////////////////////////////////////
//===================================
void main()
{
while(true) // Loop forever
{
read_write();
printf("Wait for 10 seconds\n\r");
delay_ms(10000);
}

}



Code for Slave:

#include <18F4431.h>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, BORV42, PUT, NOLVP
#use delay(clock=4M)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0xa0, SLOW=100000, FORCE_HW)

BYTE address, buffer[0x10];
//BYTE SSPADD = 0x09; // Set baud rate to 100khz


///////////////////////////////////////////////////////////////////////////////

#INT_SSP
void ssp_interupt()
{
BYTE incoming, state;

output_high(PIN_B2); // Turn on LED 3

state = i2c_isr_state();

if(state <0x80> 0x80)
output_high(PIN_B3); // Turn on LED 4
}

////////////////////////////////////////////////////////////////////////////////


void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

set_tris_c(0b11111111);

while(1) // Forever
{
if(buffer[address]==0x42) // If I2C worked
{
output_high(PIN_B1); // Flash LED 2
delay_ms(100);
output_low(PIN_B1);
delay_ms(100);


output_low(PIN_B0); // Turn off LED 1
}

else
{
output_high(PIN_B0); // Turn on LED 1

}

}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Dec 07, 2007 2:03 pm     Reply with quote

1. The slave should be running at 8 MHz minimum.
2. You access the i2c receive buffer in main(), but you don't set
the index variable to a specific value.
Quote:

void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);

set_tris_c(0b11111111);

while(1) // Forever
{
if(buffer[address]==0x42) // If I2C worked
{

Also, edit your original post and fix the slave routine code.
Carl Eldredge



Joined: 07 Dec 2007
Posts: 2
Location: Rolla, MO

View user's profile Send private message Visit poster's website

PostPosted: Fri Dec 07, 2007 4:27 pm     Reply with quote

Thanks. I actually got it working by lowering the baud rate. To speed it up, I'll get another crystal.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group