Transfer of data using I2C by RX and TX interrupt

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

Hi,

 

How data is going to transfer in ATmega32 using I2C by TX and RX interrupt?

I am trying to transmit some data and again receive that data.After receiving I want to add some more data in that and print that.

 

Thanks

harman

Last Edited: Mon. Nov 14, 2016 - 06:23 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sit down with a nice cup of tea.

 

Think about what do want to do.

Write it down on paper using your native language.

 

Then write your question on the Forum (in English)

e.g. with some example data.

 

David.

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

I want to transmit and receive particular data using master and slave connection in I2C. After receiving that data I want to add more data with that and then print it.

harman

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

Example would be ,slave transmit 8 byte of data to master by TX interuupt and then master receive it by RX interuupt. Then add more data in it and simply prints it.

harman

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

You still do not make much sense.   I2C is nothing more than a communication protocol between "inter-integrated-circuit"s

 

Do you want the Slave to transmit "David" to the Master via the USART ?  i.e. Serial

Do you want the Master to store "David" in an I2C EEPROM e.g. 24C512 ?

 

Or perhaps display "David" on a LCD with I2C adapter.

 

Everything becomes easier to understand if you say "what" I2C device and which AVR.

 

David.

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

Are you saying the mega32 acts as both master and slave then?

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

Simply I want to send 8 bytes of data from slave to master.After receiving data to master(by RX interrupt) i want to add some data into it.and then print the whole result.

Thanks

harman

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

Still not understanding you. So the mega32 is the "master" in this picture? Is the "print" over I2C or do you mean out to LCD or UART or something?

 

But if the mega32 is receiving it sort of suggests it's probably the slave here?

 

Do you have some kind of "system diagram" to show the connections and data flow involved?

 

Finally: why interrupts anyway? Is this because the arrival of bytes is asynchronously driven by a distant master that is coordinating the timing of things?

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

Yes,Using I2C. What I understand that , two nodes are communicating using I2C. Anyone can act as master or slave. One node which is slave sends data to master when TX interrupt comes . And it receives to master when RX interrupt occurs. After I want add another bytes of data into it and simply print final value.

I dont have hardware right now.Just want to simulate.

I think this is now more clear

Thanks

harman

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

I2C on the master seldom needs interrupts, while I2C on the slave usually does involve interrupts, so what is driving this requirement, is this a school project?

 

 

 

FF = PI > S.E.T

 

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

I don't know how I2C is working exactly.but I want to use interrupt in this case.

harman

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

I dont know how I2C is exactly working thats why i am posting this.

harman

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

sp_usa1 wrote:
Anyone can act as master or slave.
Yes but YOU design the system (usually) and YOU choose what it going to be the master and what is the slave. Sometimes that choice is easy. If you add a temperature sensor or a real time clock to a micro and they happen to have I2C interface then there's every chance their operation is fixed as slaves and the CPU is the master and when it wants to read the temperature or set the clock IT drives the operation as master. So it can pick when to do that and know when the other device is likely to respond. In this case the micro does not (usually) need to use interrupts. When it's ready to talk to the temp sensor or the RTC or whatever it opens the dialog and drives the conversation and even if it is on the receiving end of data it will likely have asked the clock "what time is it?" or similar and will know when to expect a response.

 

So in "normal" use interrupts are not usually required.

 

The fun starts when you have two micros you want to connect together. You then have the choice to make as to which will be the master and which will be the slave. As above the master generally drives the timing of things. So the slave needs to be ready to respond to the master at any moment (assuming it won't be getting some other clue that the master is about to get in touch).

 

But from what I've gleaned so far I don't think (correct me if I'm wrong ) you are connecting two micros. It sounds to me like you have and AVR master some kind of I2C slave (temp., RTC, EEPROM, accelerometer, DAC, ADC, whatever) and from time to time the master AVR needs to interrogate the slave to get some data from it and then print that as output on an LCD or a UART or up a USB cable to a PC or something like that?

 

If so then isn't this just "normal" use of I2C with the MCU as a master and some external device as a slave? So I don't really see where there's a requirement for RX interrupts in this? Or is the master AVR so busy doing other stuff that it cannot spend a little time now and again instigating a dialog with the slave device? So it needs to trigger the slave to start sending data, then get on with the other stuff, and just use RX-interrupts to pick up each byte as they arrive?

 

You are delivering the details here in dribs and drabs. Is it all very secret or something? Can you not just fully describe what it is you want to achieve? As I said in #8, a picture is worth a 1,000 words. Perhaps you can just sketch out on the back of a fag packet the system interconnects and intended dataflow so we all have a better picture of what is in the system and how it's intended to work.

 

The real art of engineering is detailed specification up front. So you probably already have documents describing what the ultimate goal here is anyway? Perhaps share those?

 

EDIT:

I don't know how I2C is working exactly.but I want to use interrupt in this case.

You posted that while I was typing. That statement seems oxymoronic to me. If you don't know I2C works how can you possibly know if interrupts are the best way to approach it? Or have you just heard that "interrupts are real cool!" and you need to squeeze their use in at any opportunity.

 

Or fess up - is this a school assignment and you have been told you must use interrupts in there somewhere whatever the cost? ;-)

 

(course tutors have a habit of setting projects that have no bearing on how things are actually done in the real world!).

Last Edited: Mon. Nov 14, 2016 - 02:48 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think, No school give such assignments.Why are you posting it again and again?

harman

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

sp_usa1 wrote:
Why are you posting it again and again?

What "again and again"? What are you talking about?

 

Do you want advice about I2C system design or not?

 

Or are posters here just wasting their time?

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

Let me clear the things,

I just want want to communicate two mega32 using I2C. Anyone can act as master. and just want to transfer data between them and print it from one end.

Now tell me there is need of interrupt or not? I read that on google,so just put that one.

harman

Last Edited: Mon. Nov 14, 2016 - 03:09 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't have hardware with me right now.Just want to try it on simulator

harman

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

Why in the name of all that is holy has it taken until post #16 to say "two mega32"?

 

Yes, in that case, the slave may well need to use interrupts unless it can dedicate all it's time to polling the receive state or there is some other mechanism by which the master might warn it that a transaction is imminent (Which itself might involve an interrupt).

 

So what is the data flow here? The master sends some data to the slave, the slave "processes" it in some way and then passes it back to the master who then "outputs" it? Or is the data reading sensors or something and the master accesses it to read this every now and again?

 

Don't suppose you have a system diagram showing components and dataflow do you?

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

RX interrupt generally refers to a USART interrupt on an AVR.

 

I2C interrupts are generally referred to as TWI interrupts.

 

You can connect two Mega32 chips to the same I2C bus.    One will be Master and the other a Slave.

It gets fairly tricky if you want them to change roles on the fly.    This is called I2C Multi Master.   i.e. several Masters on the same bus.

It is easy if one always starts as Master and the two MCUs have a special procedure to change roles.

 

As I said earlier.    Sit down and work out what you want to do.

 

An I2C Master is incredibly easy (with polling).    A bit tricky with interrupts.

An I2C Slave almost always uses interrupts.     Very difficult to understand.    Especially if you have no idea how you want it to behave.

 

I always suggest that you start learning to: be a Master with a hardware Slave e.g. 24Cxxx EEPROM

Then learn: Master with interrupts

Then implement a Slave to emulate the 24Cxxx (or whatever hardware chip you chose)

 

At all stages you have a working project to compare with.

 

David.

Last Edited: Mon. Nov 14, 2016 - 03:22 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

first of all thanks for understanding.

Yes data flow is like: master sends some data (eg:100) to slave. then slave just get that data and send it by adding 10.after receiving at master again add 10 to it,and finally print that.

I think this require interrupt logic also

 

harman

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

david.prentice wrote:

...This is called I2C Multi Master...

 

Somewhere in the back of my mind is a post-it note that says that "AVR multi-master mode is broken." on some chips.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

first of all thanks for understanding.

Yes data flow is like: master sends some data (eg:100) to slave. then slave just get that data and send it by adding 10.after receiving at master again add 10 to it,and finally print that.

I think this require interrupt logic also

 

harman

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

No, interrupt logic is not required. Sometimes, use of interrupt simplifies things, sometimes not.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

sp_usa1 wrote:

I think this require interrupt logic also

 

Why? If the chips are only doing what you describe then they can just poll.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Hello, 

 

  You can transfer data into the Mega32 by using I2C and  TX/RX interrupts.  But they are completely different systems for exchanging data between ICs.

 

  I2C uses two lines to connect to many peripheral ICs.   These lines are either asserted (by being pulled low) or released (allowed to float to high using a pull-up resistor).  The Mega32 describes this method as TWI (two wire interface).

 

  TX and RX interrupts use the serial port or USART of the Mega32.  In this case one line is driven low at precise intervals in order to send data in a serial stream that will read the data at precise intervals.

 

 Please read the many tutorials on the web about computer communications in order to get more information. 

 

 By the way, I would believe that you are a student because many schools use the Mega32 as a training tool.  But this IC is being replaced in the outside world by the Mega328P and the XMega series.  If you are a student then you probably must use the assigned Mega32 as the CPU platform.  Study the Peter Fleury libraries available here because a mere mortal can get them to work with the Mega32...eventually.

 

  But if you are not a student, and you are not forced to use the Mega32 as a learning tool in a classroom, then switch now to an Arduino UNO or Nano system.  They are -MUCH EASIER- to learn on and to get any small learning application operating.  The Nano can do everything the mega32 can, often using identical code.  The Arduino has a built-in library of extensions to the C language code-words that are designed to EASILY handle most AVR interfacing on the IC level.   Plus the Arduino Nano is much cheaper than any other mega-based or xmega-based PCB module AVR system.  It costs $3 on eBay for a mega328P CPU, a USB interface (that actually works reliably, which is nothing to snicker at), voltage regs, resistors, caps, LEDs, connectors, break-out VIAS,... everything you need...

Last Edited: Mon. Nov 14, 2016 - 06:02 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Can anyone give rough idea about code?

harman

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

Please give me interrupt logic in the code.

harman

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

Please NOTE: This forum is for Studio bugs only and NOT for general questions, I'll move the thread to the Mega and Tiny forum.

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

What language? If C which one?

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

C

harman

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

sp_usa1 wrote:

Can anyone give rough idea about code?

 

What research have you done? What code have you tried?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

I have  tried to initialize TWIBR TWSR TWDR registers of I2C.But I dont know how RX and TX interrupt is to initialize.

harman

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

Have you read Atmel's application notes?

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

sp_usa1,

 

I looked into I2C some time ago and this was my sequence of learning:

 

Google about the basics of I2C and get a grasp of how it works.

Read the datasheet for the devices I want to use and see how they implement I2C

Research some code examples on the internet to get a grasp on how it's done in practise.

Try coding myself and if I get stuck with my code, THEN come here.

 

This is not the first time I've seen guys that are not doing the groundwork themselves simply asking for code. I don't think that's fair on the helpful members here. 

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

Artandsparks wrote:

Research some code examples on the internet to get a grasp on how it's done in practise.

 

Except that 90% of the code you find online is just plain wrong. A classic example of this is how much rubbish code is out there which fails to follow the correct initialisation sequence for LCD displays.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

When working with I2C it's good to have an understanding of the I2C concepts devised by Philips (then NXP, now someone else) in the early 1980s, the latest specs (that I know of) http://www.nxp.com/documents/use... (fixed link)

Then, as Mr Prentice always advises, use a proven and respected I2C library.

 

 

John Samperi

Ampertronics Pty. Ltd.

https://www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

Last Edited: Mon. Nov 14, 2016 - 07:59 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think that the OP is more than a bit confused.

 

Interrupts are one-purpose functions. A USART interrupt CANNOT be used to respond to a TWI event. An SPI interrupt CANNOT be used to respond to a timer event. Each interrupt is paired with exactly ONE event (though Pin Change interrupts might seem to violate this). Thus, Rx and Tx interrupts are for serial USART operations, only. If you are going to talk about interrupts, PLEASE look up the correct name.

 

Hint: For AVR TWI, there is exactly ONE TWI interrupt, which is called simply "TWI" on the Mega48/88/168/328.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

Last Edited: Mon. Nov 14, 2016 - 11:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
What language? If C which one?
sp_usa1 wrote:
C
Which leaves the question of "which one?" unanswered frown

 

I asked for a specific reason. If it happens to be avr-gcc then you can use this...

 

http://homepage.hispeed.ch/peter...

http://homepage.hispeed.ch/peter...

 

BTW I cannot help thinking people are reading too much into your thread title confusion. Presumably the "RX and TX interrupt" you are talking about is actually TWINT_vect ?

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

A web search on "AVR i2c master code" gives me AVR315 as the first hit which is for an interrupt driven I2C master with flow charts for the whole kit and caboodle.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

I2c doesnot uses Rx and tx it uses SDA and SCK.

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

You appear to be 3 years too late.