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 CCS Technical Support

Using the I2C protocol between two PIC18F4431s

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







Using the I2C protocol between two PIC18F4431s
PostPosted: Fri Jan 25, 2008 9:29 am     Reply with quote

I am fairly amateur at microcontroller programming and definitely lacking in I2C knowledge so I will ask a couple of simple (read: ignorant) questions:

1) Is it possible to use a PIC18F4431 as a slave in the I2C protocol?

2) Are there certain pins which MUST be used for I2C communications?

Now, here is the code I'm using:

Master)
The following code sends the correct signal from the master (I have truncated everything but code immediately relevant).

Code:
setup_oscillator(OSC_8MHZ);
#use i2c(master, sda=PIN_C4, scl=PIN_C5)
setup_power_pwm_pins(PWM_BOTH_ON ,PWM_OFF, PWM_OFF, PWM_OFF);
setup_power_pwm(PWM_CLOCK_DIV_64|PWM_FREE_RUN,1,0,4095,0,1,0);

data_out =122;
address = 0x20

i2c_start();
i2c_write(address);
i2c_write(data_out);
i2c_stop();


***

Slave)
The code compiles and runs all the way through without a problem, however it seems that when it receives the signal from the master it assigns a random value to the data_in variable. What is stranger still is that the random value is not so random: it is always odd and seems to be a few numbers more often than others (255, 121, 253, etc).

Code:
setup_oscillator(OSC_8MHZ);
#use i2c(SLAVE, SDA=PIN_D2, SCL=PIN_D3, address=0x20)
setup_power_pwm_pins(PWM_BOTH_ON ,PWM_BOTH_ON, PWM_BOTH_ON, PWM_BOTH_ON);
setup_power_pwm(PWM_CLOCK_DIV_64|PWM_FREE_RUN,1,0,4095,0,1,0);

i2c_start();
data_in = i2c_read();
i2c_stop();


I then check the data_in value through a LED display.
To recap: What am I doing wrong? Or what am I not doing, that I should be to make my slave understand my master’s signal?

Thank you for your help!
Ttelmah
Guest







PostPosted: Fri Jan 25, 2008 11:18 am     Reply with quote

Look in the examples, at EX_SLAVE.C
The slave device, does not, and cannot generate either I2C start, or stop. These are both done by the _master_ device. The slave needs to simply sit, and wait, either for the I2C interrupt (or polling I2C_DATA_IS_IN).
Yes, to implement a _slave_ device, the hardware I2C pins, really must be used (it would probably be possible to write a slave using other lines, but you would be talking hundreds of lines, of very carefully written and complex code.....). These pins are D3, for the clock, and D2, for the data, which you are using.
Also, you don't mention having the pullup resistors on the lines. These are _essential_.

Best Wishes
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Jan 25, 2008 1:28 pm     Reply with quote

There are boat loads of working code posted in this forum. Look in the 'working code' section and take a look at what other people have used. That will give you an idea of both master and slave code.

Remember, start simple with just the basics necessary. Then, when you're sure you have things talking properly, add more code to get to where you want things to be.

An oscilloscope is extremely handy when trying to see what is going on between each device. You can see if the slave is ACKing back and if it's handling each instruction, from the master, in time.

Ronald
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