![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
PICLeo
Joined: 14 Dec 2005 Posts: 11 Location: South Africa
|
I2C with 18F242 |
Posted: Tue Jan 17, 2006 7:07 am |
|
|
HI
Today I tried to get a simple connection of two 18F242 started.
I just wanted to send a value from the master to the slave PIC and back. After that I send the value via RS232 to the PC to see if it works of not.
Unfortunately I do not have any kind of simulation tool where I can check the program. So trial and error.
What kind of setups do I need to edjust?
Aren't there some examble codes available?
Here the simple programs:
SLAVE:
Code: |
#device adc=8
#use delay(clock=3276800)
#fuses NOWDT,WDT128,XT, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, PUT, STVREN, NODEBUG, LVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0xa)
|
Code: |
include "RCcarI2CSlave.h"
#include <stdlib.h>
/* RS-CAR CONTROL PROGRAMM Motor PIC
*/
int16 data;
#int_SSP
SSP_isr()
{
Output_high(PIN_B4);
i2c_start();
data = i2c_read();
i2c_write(data);
i2c_stop();
Output_low(PIN_B4);
}
void main()
{
enable_interrupts(GLOBAL);
enable_interrupts(INT_SSP);
setup_spi (SPI_SLAVE);
while(1)
{
}
}
|
MASTER:
Code: |
#include <18F242.h>
#device adc=8
#use delay(clock=3276800)
#fuses NOWDT,WDT128,XT, NOPROTECT, NOOSCSEN, BROWNOUT, BORV20, PUT, STVREN, NODEBUG, LVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, NOCPB, NOEBTR, NOEBTRB
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3)
|
subroutine:
Code: |
void ICE()
{
test = 99;
i2c_start();
i2c_write(0xa0); // Device address
i2c_write(test); // Data to device
i2c_start(); // Restart
i2c_write(0xa1); // to change data direction
data=i2c_read(0); // Now read from slave
i2c_stop();
printf("%ld", data);
}
|
Bye |
|
![](templates/subSilver/images/spacer.gif) |
rnielsen
Joined: 23 Sep 2003 Posts: 852 Location: Utah
|
|
Posted: Tue Jan 17, 2006 12:08 pm |
|
|
There are a few problems here...
First,
Quote: | #use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,address=0xa) |
The address is actually 0x0a whereas your Master is addressing 0xa0.
Second,
Slaves do not generate a Start() or Stop(). Only the Master generates these signals.
Third,
Your Slave code is trying to receive And send data every time the ISR is entered. The ISR will be entered when the Slave is addressed by the Master and it will try to send nothing. You need to have some code that will test the condition of the ISR to know what needs to be done.
I, personally, don't like to use the built in functions for I2C communications. Rather, I like to use the registers directly. See the post here, http://www.ccsinfo.com/forum/viewtopic.php?t=24095&highlight=i2c
Ronald
Now that I've typed all of this stuff in, what in the heck was it I said? ![Wink](images/smiles/icon_wink.gif) |
|
![](templates/subSilver/images/spacer.gif) |
PICLeo
Joined: 14 Dec 2005 Posts: 11 Location: South Africa
|
|
Posted: Wed Jan 18, 2006 3:06 am |
|
|
Hi
I thought the zero or one behind the slave-address indicates if the master wants to write or send. ?!
I tried it like that and the interrupt on the slave PIC occured.
I deleted the start and stop lines within the slave code.
But I am still not able to send data.
Bye |
|
![](templates/subSilver/images/spacer.gif) |
PICLeo
Joined: 14 Dec 2005 Posts: 11 Location: South Africa
|
|
Posted: Wed Jan 18, 2006 8:30 am |
|
|
Hi
I changed my program now and I do get the right data at the slave PIC (3). I checked it with Hyper Terminal.
I am still busy with sending the data back. Will take a while.
Something curious:
I takes maybe 5-7 seconds till the slave PIC starts working. After that it gets the data correctly. But why this break at the startup?
I didn't use a delay in any way.
I DON'T UNDERSTAND?
Here the code SLAVE:
Code: | BYTE data;
#int_SSP
SSP_isr()
{
if(i2c_read() == 1)
{
data = i2c_read();
}
if(i2c_read() == 2)
{
i2c_write(0x45);
}
printf("%X", data);
}
|
Subrouting MASTER:
Code: | void ICE()
{
output_high(PIN_B3);
i2c_start();
i2c_write(0xa0);
i2c_write(1);
i2c_write(3);
i2c_start();
i2c_write(0xa1);
i2c_write(2);
data = i2c_read();
i2c_stop();
printf("%d,%X", data,data);
sendready = 0;
output_low(PIN_B3);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|