Help with i2c/twi using Peter Fleury's library

Go To Last Post
4 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Starting a new thread since the new question really isn't the same as the old one.

 

I have an Arduino Pro Mini (Atmega328p, 3v3, 8MHz) connected to an Si5351b clock chip.  I have successfully used an Arduino sketch to communicate with the clock via i2c/twi using the Arduino library.  Everything worked great.

 

Now I am trying to do the same thing without using the Arduino library, instead using the i2c library from Peter Fleury, using the twimaster.c implementation since the 328p has hardware i2c support.   Using Atmel Studio 7 on a Win10 computer.

 

I'm trying for a really simple bit - just send a single register worth of configuration data.  Here's the code:

 

 #define F_CPU 8000000UL
 #include "i2cmaster.h"
 #include <util/delay.h>

 int main(void)
 {
  	DDRB = 0xff;

    i2c_init();                             // initialize I2C library

	for (uint8_t i=0; i < 6; i++)
	{
		PORTB ^= 0xFF;
		_delay_ms(300);
	}

	uint8_t ret;

	PORTB = 0x00;

	ret = i2c_start(0x60+I2C_WRITE);

	if (ret)
	{

		i2c_stop();
		PORTB = 0xFF;

	} else {

		i2c_write(0x03);
		i2c_write(0xFF);
		i2c_stop();

	}

	 for(;;);
 }

 

This is pretty much taken verbatim from the example code provided with the library.  The port writes are just there to blink/light the pin 13 LED for debugging purposes (since I've not tried to get UART to work yet).

 

I hooked up my scope with i2c decoder to the SDL and SCL pins.   See screenshot below.  The sequence of events happens as follows:

 

1) The LED blinks, meaning it's successfully gotten past i2c_init(). 

2) It fires off a single, incorrect data packet - a start to a 0x30 address (not the 0x60 it should be).

3) The LED goes solid on, meaning it got an error value from i2c_start.

 

See screenshot of scope, attached, with the packet shown.

 

I'm not sure what's going on.  One of the things I'm wondering is whether AS7 is setting up the CPU speed correctly.  I successfully used AS7 on an Arduino Uno (5V, 16MHz) before, but this is my first try at the 8 MHz Pro Mini.  I selected the ATMega328p when I created the project, but I'm wondering if I need to tell it, somewhere, to program the fuses for the lower speed.  I am uploading the code via avr-dude, over USB, with the following command: "-CE:\Arduino\hardware\tools\avr\etc\avrdude.conf -v -v -v -v -patmega328p -carduino -PCOM9 -b57600 -D -Uflash:w:"$(ProjectDir)Debug\$(ItemFileName).hex":i "  The upload does work, as the LED reacts correctly.

 

Help much appreciated.

Attachment(s): 

This topic has a solution.
Last Edited: Sun. Jan 14, 2018 - 03:48 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I2c_start is probably a handful of lines of code - what does it say? What happens to the values you pass until they hit the twi data register?

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Have not checked your code but one of the differences, as I recall, is how I2C addresses are constructed. One uses 8 bit addresses that are different for read and write. The other (don't remember which is which) uses a 7 bit address to which you have to add a read/write bit. With the 7 bid addresses, you usually have to shift them left by 1 and append the correct read/write bit. The peripheral device manufacturers are also not consistent in how their addresses are presented.

 

The spec sheet says that the 7 bit address is 0b110000A where "A" is the bit value set by pin A0. So, you are not shifting the address properly. It would need to be 0xC0 + I2C_WRITE.

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You are quite correct!  I think someone (you?) mentioned this idea on the other thread, but it didn't penetrate.  This appears to be an inconsistency between the Arduino Wire library and this library - Wire does the left shift for you, this one doesn't.  Thank you!