I2C master does not want to connect to MCP9808

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

Hi,

 

I have an adafruit MCP9808 to read out the temperature for my arduino micro pro.

Because Im going to work with only the Atmega32u4 chip this seemed like a good practice.

to make the adafruit MCP9808 work in AVR I needed to do the followng things:

- turn the adafruitlibary from c++ to c

- replace wire.h with I2C_master

 

Problem:

In the start function of I2c_master there is a check if the command is write or read. 

The value that checks what command it is is twst. if twst is 0x18 its write and if its 0x40 its read. 

But if its neither of these the function returns 1(which is happening) because (and thats what i dont know) twst is 32.

The adress I give as argument is 24 (0x18) which is the adress of the MCP9808 that is defined in the adafruit MCP9808 library

 

uint8_t i2c_start(uint8_t address)
{
	// reset TWI control register
	TWCR = 0;
	// transmit START condition
	TWCR = (1<<TWINT) | (1<<TWSTA) | (1<<TWEN);
	// wait for end of transmission
	while( !(TWCR & (1<<TWINT)) );

	// check if the start condition was successfully transmitted
	if((TWSR & 0xF8) != TW_START)
	{
		return 1;
	}

	// load slave address into data register
	TWDR = address;
	// start transmission of address
	TWCR = (1<<TWINT) | (1<<TWEN);
	// wait for end of transmission
	while( !(TWCR & (1<<TWINT)) );

	// check if the device has acknowledged the READ / WRITE mode
	uint8_t twst = TW_STATUS & 0xF8;
	if ( (twst != TW_MT_SLA_ACK) && (twst != TW_MR_SLA_ACK) )
	{
		return 1;
	}
	return 0;
}
#define TW_STATUS  (TWSR & TW_STATUS_MASK)
#define TWSR _SFR_MEM8(0xB9)
/**
 * \ingroup util_twi
 * \def TW_STATUS_MASK
 * The lower 3 bits of TWSR are reserved on the ATmega163.
 * The 2 LSB carry the prescaler bits on the newer ATmegas.
 */
#define TW_STATUS_MASK  (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|\
    _BV(TWS3))

some handy defs (spread throughout the program)

#define TW_STATUS  (TWSR & TW_STATUS_MASK)
#define TWSR _SFR_MEM8(0xB9)
/**
 * \ingroup util_twi
 * \def TW_STATUS_MASK
 * The lower 3 bits of TWSR are reserved on the ATmega163.
 * The 2 LSB carry the prescaler bits on the newer ATmegas.
 */
#define TW_STATUS_MASK  (_BV(TWS7)|_BV(TWS6)|_BV(TWS5)|_BV(TWS4)|\
    _BV(TWS3))

For the hardware:

32u4 to mcp9808

gnd to gnd

vcc to vcc

pin 2(SDA) to SDA

pin 3(SCL) to SCL

Pin 2 with 2.2kOhm to vcc

pin 3 with 2.2kOhm to vcc

 

I hope someone got some information for me on this topic

 

Thanks in advance

This topic has a solution.
Last Edited: Wed. Jul 18, 2018 - 07:21 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

JPdeRie wrote:
Im going to work with only the Atmega32u4

So why have you posted in the XMega forum?

 

JPdeRie wrote:
The adress I give as argument is 24 (0x18)

Is that the true 7-bit address, or an "8-bit" address (which includes the R/W bit) ?

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

https://www.avrfreaks.net/commen...

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So I2C doenst bit shift the adress and add a r/w bit. So that might be the problem, im checking it right now.

 

#EDIT

yes this was the problem

thank you!

Last Edited: Tue. Jul 17, 2018 - 09:26 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

Great!

 

laugh

 

Now please mark the solution - see Tip #5

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#UPDATE

 

so the bit shifting of the address was indeed needed to work but it wasnt the reason that twst returned 32.

I was putting breakpoints in the start function of I2C_master which caused for unsynchronisation.

 

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

JPdeRie wrote:
So I2C (sic)  doenst bit shift the adress and add a r/w bit.

What are you referring to as "I2C" here?

 

I2C is the name of a two-wire serial bus.

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I2C_master.c*