serial programming by interrupt

hi all
i am using arduino nano.
I need a simple code in which i will receive data using RX interrupt. and buffer size is almost 16 bytes.
Thanks in advance

Do you really need this to be interrupt driven? ( I mean handling yourself the serial interrupt rather than using the Serial class)

Have a look at my Yet Another Software Serial

But if you can use the regular Serial libraries I suggest you do so.

...R

means when any data will be received, receive interrupt will be generated. I want to do something very immediate when any data is received.

In communication there is no such thing as immediate. No guarantee that two bytes (e.g. one integer) is transmitted without delay in between the two bytes of the integer.

So please define 'very immediate'. What kind of data (single byte, multiple bytes)? What else is your program doing?

void loop(){
  if(Serial.available()) {
    doSomething();
  }
}

On a 16MHz Arduino, this will have a variable delay up to about 2 microseconds in the rarest worst-case. Is that accurate enough?

void loop(){
  while (1) {
    if(Serial.available()) {
      doSomething();
    }
  }
}

Potentially very slightly better in terms of latency (depends on optimisation)

I guess the OP has more something like this in mind with doSomethingAllTheTime() taking potentially a fair amount of time

void loop(){
  if(Serial.available()) {
    doSomething();
  }
   doSomethingAllTheTime();
}

noman_yousaf:
means when any data will be received, receive interrupt will be generated. I want to do something very immediate when any data is received.

There is no simple way to get an interrupt when a message is received. But when you consider how long it takes for serial data to arrive there is no value to using interrupts.

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data and see how they notify the rest of the program when a message arrives.

The important thing is to design the entire program so no part of it blocks any other part.

...R

thanks all
in above code, there is no any ISR (interrupt service routine). In arduino, is there no any ISR?
I have worked on AVR in assembly so when any data comes from RX, it goes to ISR and do any thing, we need, is that not possible in arduino?

sorry if i am wrong,
I am not to expert or old in arduino so may be it is misunderstanding. As i have told that i have worked on AVR. To save, received data, ISR is used so during other working, serial data is received and saved and not lost, What happened in arduino? Is serial data saved in buffer even arduino is doing other things? if yes then my problem is solved. i am just worried that if arduino is doing something in routine and serial data is received, will that be saveed or lost.

The answer to your question hangs on clarifying what "very immediate" means to you.

Is serial data saved in buffer even arduino is doing other things?

Yes

noman_yousaf:
thanks all
in above code, there is no any ISR (interrupt service routine). In arduino, is there no any ISR?
I have worked on AVR in assembly so when any data comes from RX, it goes to ISR and do any thing, we need, is that not possible in arduino?

Yes of course it is. The Serial library already uses interrupts to put things into the receive buffer. Why do you want to do this yourself? The previous replies are guessing as to your reason.

To save, received data, ISR is used so during other working, serial data is received and saved and not lost, What happened in arduino? Is serial data saved in buffer even arduino is doing other things?

Yes it is. The buffer is 64 bytes from memory, and thus you can be doing other things and not lose anything, providing you check before the 64 bytes are filled up.