Hello,
I'm trying to figure out I2C for the devantech compass, using the Axon. I'm not too worried about the compass but I really need to learn I2C for the sake of other components on my project.
I'm using the i2c.c/i2c.h from AVRlib, but the documentation is pretty sparse. I've been looking at a couple examples on the web (
here and
here) and I think I've cobbled something together that SHOULD work.
I'm pretty sure it's not a hardware issue, but we're using external pullup resistors instead of messing with the internal pullups on the Axon. Could this be a potential sticking point? Anyway, here is my code:
i2cInit();
i2cSetBitrate(100); //have tried all kinds of values here
cbi(TWCR, TWIE); //disable interupt
i2cSendStart(); //start condition
lcd_clrscr();
lcd_puts("1");
delay_us(200);
i2cWaitForComplete();
i2cSendByte(0xC0); //device address with r/w bit low
lcd_clrscr();
lcd_puts("2");
delay_us(200);
i2cWaitForComplete();
i2cSendByte(0x01); //send data - register address
lcd_clrscr();
lcd_puts("3");
delay_us(200);
i2cWaitForComplete();
i2cSendStart(); //repeated start condition
lcd_clrscr();
lcd_puts("4");
delay_us(200);
i2cWaitForComplete();
i2cSendByte(0xC1); //device address with r/w bit high
lcd_clrscr();
lcd_puts("5");
delay_us(200);
i2cWaitForComplete();
i2cReceiveByte(FALSE);
i2cWaitForComplete();
compass_analog = i2cGetReceivedByte();
i2cSendStop();
lcd_clrscr();
lcd_puts(itoa(compass_analog, buffer, 10));
The numbers I'm writing to the LCD are for debugging purposes, so I can see where it hangs up. The code above hangs up on number 5, which seems to indicate that the problem is with the i2cReceiveByte() function. If I remove the i2cWaitForComplete() immediately following i2cReceiveByte, compass_analog always comes back with the value 193. Weird, huh?
Now I dont think anyone on here has actually managed to get I2C working on the Axon, but I'm hoping someone sees something in my code that would stop it from working. Failing that, maybe this will help someone else get theirs working.. in which case, please tell me what you did :p