hi aLL
well, here an Arduino lover and somehow newbie
i’m trying to read a Devantech Digital Compass CMPS03 via i2c and i can’t get it to work …
i got it to work really easyly with the PWM read pulse … but i would like to use the i2c interface …
i’ve the 1.5 K resistors as pullUps on the SDA and SCL …
i tried :
-
Arduino 006 with Wire.h library … with which pins it should work ? has anybody got to work Arduino in an i2C interface using the Wire.h lib ?? Which pins to be connected [are they Analog in 4 and 5 ??]
-
coding myself i2c functions as the Phillips i2C protocol specs … no result …
-
wrapping many other i2c c functions i found and i got no positive results …
Could anybody please make somelight on how to use i2c with Arduino ??
I post here my last code in case anybody knows about it …
any info on i2c or CMPS03 interfacing would be great …
//////////////////////////
/////////////////////////////////////////////////////////////////////
byte PIN_SDA = 7;
byte PIN_SCL = 6;
byte pinLed = 13;
int MSa = 1000;
byte b = 0;
/////////////////////////////////////////////////////////////////////
void setup()
{
Serial.begin(115200);
// inicialitzacions
pinMode(PIN_SDA,OUTPUT);
pinMode(PIN_SCL,OUTPUT);
//pinMode(pinLed,OUTPUT);
SDA(1); //KK
SCL(1); //KK
//delay(20000);
}
/////////////////////////////////////////////////////////////////////
void loop()
{
Serial.println("//////////////////////////////// inici Loop()");
i2c_start();
i2c_tx(0xC0);
i2c_tx(0x01);
i2c_start();
i2c_tx(0xC1);
b = i2c_rx(1);
i2c_stop();
Serial.print("_LOOP__read ");
Serial.println((int)b);
delay(500);
}
//////////////////////////////////////////////////////
void SDA(int i)
{
pinMode(PIN_SDA,OUTPUT);
i2c_delay();
if(i){
digitalWrite(PIN_SDA,HIGH);
Serial.print("… “);
Serial.println(i);}
else {
digitalWrite(PIN_SDA,LOW);
Serial.print(”… ");
Serial.println(i);}
}
//////////////////////////////////////////////////////
void SCL(int i)
{
pinMode(PIN_SCL,OUTPUT);
i2c_delay();
if(i){
digitalWrite(PIN_SCL,HIGH);
Serial.print("========= “);
Serial.println(i);}
else {
digitalWrite(PIN_SCL,LOW);
Serial.print(”== ");
Serial.println(i);}
i2c_delay();
}
////////////////////////////////////////////////////////
void i2c_delay()
{
delayMicroseconds(MSa);
}
//////////////////////////////////////////////////////////////////
void i2c_start()
{
Serial.println("____i2c_START");
SCL(1);
i2c_delay();
SDA(1);
i2c_delay();
SDA(0);
i2c_delay();
//* recomanat en algunes docs
//SDA(1);
i2c_delay();
}
//////////////////////////////////////////////////////////////////
void i2c_stop()
{
Serial.println("____i2c_STOP");
SCL(1);
i2c_delay();
SDA(0);
i2c_delay();
SDA(1);
i2c_delay();
}
//////////////////////////////////////////////////////////////////
int SCL_IN()
{
pinMode(PIN_SCL,INPUT);
i2c_delay();
int n = digitalRead(PIN_SCL);
i2c_delay();
return(n);
}
//////////////////////////////////////////////////////////////////
int SDA_IN()
{
pinMode(PIN_SDA,INPUT);
i2c_delay();
int i = digitalRead(PIN_SDA);
i2c_delay();
Serial.print("____lleyendo en SDA_IN =");
Serial.println(i);
return(i);
}
///////////////////////////////////////////////////////////////////
// Transmissio de un byte per i2C
byte i2c_tx(byte d)
{
Serial.print("________TRANSMIX de byte ");
Serial.println((int)d);
byte x;
byte b=1;
//SCL(0);
for(x=8;x>0;x–)
{
if(d & 0x80){
SDA(1);}
else{
SDA(0);}
SCL(0);
i2c_delay();
i2c_delay();
SCL(1);
i2c_delay();
SCL(0);
i2c_delay();
d=d<<1;
}//endFor
// comencem el ACK
SDA(1); // deixem lliure la SDA
SCL(0);
i2c_delay();
i2c_delay();
SCL(1);
i2c_delay();
i2c_delay();
int ack = SDA_IN();
if (ack) {Serial.println("___NACK");}
else {Serial.println("___ACK OK");}
return(0);
}//endi2c_tx
///////////////////////////////////////////////////////////////////
byte i2c_rx(byte ack)
{
Serial.println("________RECEPCION de byte ");
byte x=0;
byte d=0;
SDA(1);
for(int x=0;x<8;x++){
SCL(1);
i2c_delay();
d = SDA_IN();
if(d)
d|=1;
d<<=1;
digitalWrite(pinLed,d);
SCL(0);
i2c_delay();
}//endFor
//if(ack)
//{
// SDA(0);
//}
//else
//{
// SDA(1);
//}
return(d);
}//endi2c_rx
what is happening to me is that i never got an ACKnowledge from the CMPS03 when adressing it to 0xc0 … ?¿