Author Topic: Yet another I2C/Axon thread  (Read 1346 times)

0 Members and 1 Guest are viewing this topic.

Offline OperationIvyTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Yet another I2C/Axon thread
« 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 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:

Code: [Select]
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

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: Yet another I2C/Axon thread
« Reply #1 on: March 30, 2009, 08:34:03 PM »
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.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,132
  • Helpful? 108
Re: Yet another I2C/Axon thread
« Reply #2 on: March 31, 2009, 09:12:35 AM »
Have also found this link for ATMega32 which may help you out:- http://www.robot-electronics.co.uk/htm/Atmega32.htm
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline OperationIvyTopic starter

  • Jr. Member
  • **
  • Posts: 15
  • Helpful? 0
Re: Yet another I2C/Axon thread
« Reply #3 on: March 31, 2009, 03:18:57 PM »
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?

Offline Admin

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,657
  • Helpful? 169
    • Society of Robots
Re: Yet another I2C/Axon thread
« Reply #4 on: March 31, 2009, 10:27:35 PM »
Seen this post?
http://www.societyofrobots.com/robotforum/index.php?topic=6970.0

(probably doesn't solve your problem, but might offer clues)

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: Yet another I2C/Axon thread
« Reply #5 on: April 01, 2009, 10:38:18 AM »
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 ?

 


Get Your Ad Here

data_list