I2C Problems

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

I am trying to develop a simple temperature indicator for thermocouple or RTD input using a 16-bit delta sigma ADC interfaced with ATmega16 using I2C protocol. Also I'm using 7-segment display.

Now, I have written code in C language for getting ADC count for some input voltage. I have made use of interrupts for I2C communication.  

But the problem is that I'm unable to read data from ADC in infinite while loop in main function.

I don't get any response from the slave(ADC).

 

I've attached code for reference.

Please suggest any modifications/corrections.

Attachment(s): 

Gaurav Gawade

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

gauravg20 wrote:
I don't get any response from the slave(ADC).

 

The "problem" may be in many areas, such as:

1) H/W no pull up resistors (or wrong pull up resistors used)

2) wrong I2C address, thus no response from slave

3) using un-proven I2C functions

4) ignoring return values from I2C functions

 

Please post a schematic or picture of your setup.

Why not use proven I2C library to at least prove your h/w works

Tell us what test equipment you have access too that may help you troubleshoot your h/w and or s/w issues.

Tell us what you have done so far to trouble shoot the problem.

 

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

Thanks Jim for your immediate response.

 

The "problem" may be in many areas, such as:

1) H/W no pull up resistors (or wrong pull up resistors used)

2) wrong I2C address, thus no response from slave

3) using un-proven I2C functions

4) ignoring return values from I2C functions

First, I have used both pull-up resistors on SLC & SDA lines.

Second, the slave address is correct because when we execute the same code without an infinite loop it is executed. i.e. you get response from the slave.

Third, I haven't used any unproved I2C functions. I'm trying to make use of simple functions for using I2C module.

Finally, there are no return values from I2C functions.

 

 

Right now, I don't have any schematic or picture of my circuit.

Nor do I have any test equipment for testing the circuit except a multi meter.

I have tested this code without making use of infinite loop. It works but only once. I want it to work infinitely.

Please suggest any modifications/corrections.

 

Gaurav Gawade

Last Edited: Mon. Aug 7, 2017 - 01:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

gauravg20 wrote:
Third, I haven't used any unproved I2C functions.

I'm trying to make use of simple functions for using I2C module.

Finally, there are no return values from I2C functions.

 

And there is the problem...

 

Most I2C devices use a sequence similar to this:

 

ret = i2c_start(address);
if (ret == ack){
    data1 = i2c_readAck();
    data2 = i2c_readNak();
}
i2c_stop();

When reading multi-byte data from the device, the last byte must be NACK'd to tell the device to release the bus so the master can do i2c_stop!

 

Please let us know what ADC device your talking to, then we may be able to give specific advise.

Interrupts are not needed for simple i2c master code, but return status values are a must.

 

Jim

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

ADC: MCP3426

Gaurav Gawade

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

Follow Jm's advice.

 

1.  Use proven library e.g. Fleury

2.  Use conventional source code.  e.g. in C files

3.  Use return values e.g. in Jim's sequence.

 

Whichever library you choose,   you must follow their documentation.   e.g. which function arguments,  what return values mean

 

When your program is working 100% with conventional libraries,  you can experiment with your own square wheels.

 

David.

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

gauravg20 wrote:
ADC: MCP3426

So the datasheet is here: http://ww1.microchip.com/downloa...

 

ki0bk wrote:
When reading multi-byte data from the device, the last byte must be NACK'd to tell the device to release the bus

which is clearly shown in fig 5-4.

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

david.prentice wrote:
Follow Jm's advice.

+1

 

And, of course, follow the specific instructions given in the datasheet.

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

Though interrupts is not recommend for single slave on I2C bus, but suppose if I want it then what should I do?

Please suggest.

Gaurav Gawade

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

You should get the basics working first!

 

 

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: 1

It is much easier to use a polled Master. Get that working 100%.
Then study how the library code works.
Then replace it with an interrupt driven Master if you want.
.
By all means write your own I2C code but make sure you understand how the proven code works first.
.
David.

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

gauravg20 wrote:

Though interrupts is not recommend for single slave on I2C bus, but suppose if I want it then what should I do?

Please suggest.

 

See AVR ap note 315, i2c master for interrupt driven TWI

a complete interrupt driver for TWI, source code and application notes, google AVR315

 

Jim

 

 

 

(Possum Lodge oath) Quando omni flunkus, moritati.

"I thought growing old would take longer"

 

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

This is about th 20th time I advise this in the last month or zo:

Go get a logic analyser.

Assuming that the logic levels on the I2C bus are valid (pullups) it will tell you pretty fast if anything fishy is goin on on the I2C bus.

Or it wil confirm that I2C is OK and you can focus on the next best thing.

 

Look a bit into "sigrok"

Unfortunately I'm a bit in limbo about what hardware to advise.

Until recently I've been advising to search aliexpress for "24m 8ch".

But the latest batch of 10 came with a CY7C68013A-128AXC instead of the expected CY7C68013A-56... and the don't work entirely as expected.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR's: http://www.hoevendesign.com

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

Paulvdh wrote:
This is about th 20th time I advise this in the last month or zo: Go get a logic analyser.

 

You do not need one if you use a proven library as David says, such as Peter Fleury's as the library will return an error if there is a problem.  This issue with I2C comes up often enough and the problem is usually an error in the code somewhere.  Reading the return from each function will tell you where things went wrong.

 

JIm

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

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

Ok. Thanks everyone.

Gaurav Gawade