|
|
View previous topic :: View next topic |
Author |
Message |
erik006
Joined: 05 Jan 2008 Posts: 10
|
I2C troubles - 2 PICs |
Posted: Sat Mar 01, 2008 11:02 pm |
|
|
I'm trying to get two PICs the communicate over i2c. For now, I'm using 2 18F2431 PICs to test out the i2c routines.
The trouble is: it's just not working properly. Although at first, using the EX_SLAVE example from CCS, it would work semi-decently. (it would sometimes send back what I sent it).
I'm using master code suggested by PCM programmer, and slave code from the EX_SLAVE example.
Master code:
Code: |
int8 data;
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data); |
One PIC is running on an internal oscillator (8MHz) and since it's on a PCB it's sort of tough to change that right now. For the other PIC i've tried using the internal osc, and crystal with the following values: 8Mhz, 10Mhz, 12Mhz,25Mhz. All with the proper delay values.
I've tried using 2.2K & 370 Ohms pull up resistors. I've tried using force_hw, & SLOW options.
I tried turning leds on in the interrupt routine of the slave & master, which seemed to work. Somehow though, I don't receive the proper character.
Compiler version: 4.057.
Any ideas?
Thanks,
Erik |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Mar 01, 2008 11:34 pm |
|
|
1. Post the full master program. I mean show the #include, #fuses, and
#use statements. Show the main(), and the braces, etc.
2. Post the #include, #fuses, and #use statements of the Ex_Slave.c
program. In other words, post only the part of Ex_Slave.c that you
modified, which will be those first few lines.
3. Do you have a ground connection between the two boards ?
4. Are both boards running at 5v ?
5. When you use 2.2K pull-ups, and send the letter 'B', what do
you get back ? If it's some strange character, then change the %c
into a %x in the printf statement. Then post the value that you get back.
(Don't test it with 370 ohm pullups. That's way too low). |
|
|
erik006
Joined: 05 Jan 2008 Posts: 10
|
|
Posted: Sun Mar 02, 2008 8:09 am |
|
|
Here's the master code:
Code: | #include <18f2431.h>
#fuses HS, NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#use i2c(MASTER, SDA=PIN_C4, SCL=PIN_C5)
#use RS232(BAUD =57600, XMIT=pin_c6, RCV=pin_c7,)
void main()
{
// Write the letter 'B' to the slave board.
int8 data;
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_write('B');
i2c_stop();
// Read from the slave board and display the data.
i2c_start();
i2c_write(0xA0);
i2c_write(0x00);
i2c_start();
i2c_write(0xA1);
data = i2c_read(0);
i2c_stop();
printf("read %c \n\r", data);
while(TRUE){}
} |
Slave fuses:
Code: | #elif defined(__PCH__)
#include <18F2431.h>
#fuses RC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0xa0)
|
The master board distributes 5V power to the slave board, so the grounds are connected. At first I got a strange character back, when I changed it, I started getting an 'ff' back. When I changed it back, I still got an 'ff' back.
Hope that helps,
Erik |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Mar 02, 2008 11:24 am |
|
|
Quote: | I started getting an 'ff' back |
FF is what you get when the slave is dead. When you just have pull-ups
on the bus and there's no slave, you will get FF back.
Quote: |
#include <18F2431.h>
#fuses RC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0xa0)
|
The RC_IO fuse is for an external oscillator circuit that consists of
a resistor and a capacitor. I don't think you have those, so your
PIC is probably not even running. To use the internal oscillator,
you should use the INTRC_IO fuse. |
|
|
erik006
Joined: 05 Jan 2008 Posts: 10
|
yeah... |
Posted: Sun Mar 02, 2008 2:49 pm |
|
|
Thanks PCM programmer, that fixed it. Thanks for letting me know about the "ff" deal also, that'll definitely help me spot problems in the future.
Erik |
|
|
|
|
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
|