zcashion
Joined: 30 Jul 2010 Posts: 1
|
PIC18F4550 I2C Slave transmits garbage |
Posted: Fri Jul 30, 2010 6:04 pm |
|
|
Hello.
I am developing a circuit in which I have two PIC18F4550's communicating via I2C. I have successful communication from master to slave but when I read from the slave I always receive the same thing, decimal 106.
In my slave code, even if I comment out the I2C_write command, I STILL receive the 106 from the slave. I also attached an LED as an indicator that the slave code is operating properly. The LED indicates that the correct section of code operates when I ask for data from the slave.
The craziest part is that I have written very similar slave code for the PIC16F88 and it works perfectly in both directions with the 4550 master.
Can anybody PLEASE help me shed some light on this issue? I have spent many hours on it now and I'm stumped...and frustrated.
Thanks!
Slave Code:
Code: |
#include <18F4550.h>
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, USBDIV, PLL5, CPUDIV3, VREGEN, PUT
#use delay (clock=24000000)
#use i2c(Slave,sda=PIN_B0,scl=PIN_B1,address=0xB4,force_hw)
// pin defines
#define RED_LED PIN_B2
BYTE adress, buffer[0x10];
#INT_SSP
void SSP_isr()
{
BYTE incoming, state, clear;
state = i2c_isr_state();
if (state <= 0x80){ //The code comes here when the master is sending data
incoming = i2c_read();
if (state ==1){
adress = incoming;
output_high(RED_LED);
}
if (state == 2){
buffer[adress] = incoming;
}
}
if (state == 0x80){ //This is where the code comes when the master asks for data
i2c_write(buffer[adress]);
//output_high(RED_LED);
}
}
|
Master Code:
Code: |
#include <18F4550.h>
#fuses HSPLL, NOWDT, NOPROTECT, NOLVP, USBDIV, PLL5, CPUDIV3, VREGEN, PUT
#use delay (clock=24000000)
#use i2c(Master,sda=PIN_B0,scl=PIN_B1)
void Write_Slave(int Slave_Address, int Buffer_Address, Write_Byte)
{
i2c_start();
i2c_write(Slave_Address); // Device address
i2c_write(Buffer_Address); // address of the buffer that you are writing to 'buffer[address]'
i2c_write(Write_Byte); //data sent to the buffer(address) above
i2c_stop();
}
void Read_Slave(int Slave_Address, int Buffer_Address)
{
i2c_start();
i2c_write(Slave_Address);
i2c_write(Buffer_Address);
//i2c_stop();
delay_us(1);
i2c_start();
i2c_write(Slave_Address+1);
delay_us(2);
data = i2c_read(0);
i2c_stop();
} |
|
|