hi,
i'm trying to use the I2C bus between 2 same dspic30f4011 controllers.
i'm using 1.8 Kohms pull-up resistors, one resistor on SDA and one resistor on SCL.
i'm supplying both dspic's with external power (5v).
here is the code for both master and slave, there is only 1 line different..
#include "p30f4011.h"
#include "libpic30.h"
#include "i2c.h"
#define BUT PORTBbits.RB8
#define LED PORTBbits.RB0
int main(void)
{
ADPCFG = 0xFFFF;
unsigned int I2CConfig, I2C_BRG;
TRISFbits.TRISF2 = 1; // SDA
TRISFbits.TRISF3 = 1; // SCL
TRISBbits.TRISB0 = 0; //Output for led
TRISBbits.TRISB8 = 1; //but
//Configure the I2C Bus//
I2CConfig = I2C_ON & //Enable the I2C Bus
I2C_IDLE_CON & I2C_CLK_HLD & I2C_IPMI_DIS &
I2C_7BIT_ADD & //Use a 7 Bit Slave Address
I2C_SLW_DIS & //Slew Rate disabled (enbled only for "fast mode" 400kHz)
I2C_SM_DIS & I2C_GCALL_DIS & I2C_STR_DIS &
I2C_NACK & //Send NACK during acknowledge
I2C_ACK_DIS & I2C_RCV_DIS & I2C_STOP_DIS & I2C_RESTART_DIS & I2C_START_DIS; //All seq. disabled
I2C_BRG = 0xB5; //0xB5 = 181 => Fscl=100kHz=Boud Rate, PGD=250ns, Fcy=20MHz/ 0x1F = 31 =>Fscl=400kHz
/*SLAVE ONLY*/// I2CADD = 0x08; // the slave address is 0x08, with r/w bit=0, we get 0x10
OpenI2C(I2CConfig , I2C_BRG); //Open the I2C Bus
LED=0;
IdleI2C();
StartI2C();
while(I2CCONbits.SEN); // Wait till Start sequence is completed
if (MasterWriteI2C(0x10) == 0) // the slave address is 0x08, with r/w bit=0, we get 0x10
{
LED=1;
__delay32(2500000); // =0.5 sec
LED=0;
__delay32(2500000); // =0.5 sec
LED=1;
__delay32(2500000); // =0.5 sec
LED=0;
}
StopI2C();
}
what happens is that the leds start blinking all the time, not only twise like it should.
where is my problem ?
i'm trying to use the I2C bus between 2 same dspic30f4011 controllers.
i'm using 1.8 Kohms pull-up resistors, one resistor on SDA and one resistor on SCL.
i'm supplying both dspic's with external power (5v).
here is the code for both master and slave, there is only 1 line different..
#include "p30f4011.h"
#include "libpic30.h"
#include "i2c.h"
#define BUT PORTBbits.RB8
#define LED PORTBbits.RB0
int main(void)
{
ADPCFG = 0xFFFF;
unsigned int I2CConfig, I2C_BRG;
TRISFbits.TRISF2 = 1; // SDA
TRISFbits.TRISF3 = 1; // SCL
TRISBbits.TRISB0 = 0; //Output for led
TRISBbits.TRISB8 = 1; //but
//Configure the I2C Bus//
I2CConfig = I2C_ON & //Enable the I2C Bus
I2C_IDLE_CON & I2C_CLK_HLD & I2C_IPMI_DIS &
I2C_7BIT_ADD & //Use a 7 Bit Slave Address
I2C_SLW_DIS & //Slew Rate disabled (enbled only for "fast mode" 400kHz)
I2C_SM_DIS & I2C_GCALL_DIS & I2C_STR_DIS &
I2C_NACK & //Send NACK during acknowledge
I2C_ACK_DIS & I2C_RCV_DIS & I2C_STOP_DIS & I2C_RESTART_DIS & I2C_START_DIS; //All seq. disabled
I2C_BRG = 0xB5; //0xB5 = 181 => Fscl=100kHz=Boud Rate, PGD=250ns, Fcy=20MHz/ 0x1F = 31 =>Fscl=400kHz
/*SLAVE ONLY*/// I2CADD = 0x08; // the slave address is 0x08, with r/w bit=0, we get 0x10
OpenI2C(I2CConfig , I2C_BRG); //Open the I2C Bus
LED=0;
IdleI2C();
StartI2C();
while(I2CCONbits.SEN); // Wait till Start sequence is completed
if (MasterWriteI2C(0x10) == 0) // the slave address is 0x08, with r/w bit=0, we get 0x10
{
LED=1;
__delay32(2500000); // =0.5 sec
LED=0;
__delay32(2500000); // =0.5 sec
LED=1;
__delay32(2500000); // =0.5 sec
LED=0;
}
StopI2C();
}
what happens is that the leds start blinking all the time, not only twise like it should.
where is my problem ?