Society of Robots - Robot Forum
Software => Software => Topic started by: OperationIvy on March 30, 2009, 01:42:39 PM
-
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 (http://www.robot-electronics.co.uk/htm/Atmega32.htm) and here (http://www.scu.edu/engineering/cse/ugrad/upload/COEN-2006-PROJECT-10.pdf)) 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
-
Have you tried the I2C in the Axon lib. Its basically the same as the AVRlib version except for one or two lines that FIX the correct register definitions for the Axon ATMega640.
-
Have also found this link for ATMega32 which may help you out:- http://www.robot-electronics.co.uk/htm/Atmega32.htm (http://www.robot-electronics.co.uk/htm/Atmega32.htm)
-
I tried using the SoR i2c.c/i2c.h file, it didn't change anything. My code still hangs up after the i2cReceiveByte() function.
I've seen that example, I even linked it in my first post.
Anyone actually have this working?
-
Seen this post?
http://www.societyofrobots.com/robotforum/index.php?topic=6970.0 (http://www.societyofrobots.com/robotforum/index.php?topic=6970.0)
(probably doesn't solve your problem, but might offer clues)
-
I had trouble with I2C once on PIC's where it was sending data but not receiving it. The problem was that the I2C pins should be set as input pins from the start. (the built in libraries never did this). maybe its the same for atmega's ?