Interrupt driven serial communication

Hi friends.........i just want to check how to use arduino mega for interrupt based serial communication but i dont know why it is not working i have attached the code below please let me know whats the problem....thanks

#include <avr/io.h>
#include <avr/interrupt.h>

void setup()
{
pinMode(13, OUTPUT);

noInterrupts(); // disable all interrupts

UBRR0H = 0x00;
UBRR0L = 207;

UCSR0B = 0x98; // Serial interrupt
UCSR0C = 0x06; // Serial interrupt

interrupts(); // enable all interrupts
}

void loop()
{

}

ISR(USART0_RXC) // timer compare interrupt service routine
{
noInterrupts();

char inChar = (char)Serial.read();

char inChar = UDR0;
delay(5);

Serial.print(inChar);
delay(20);

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)
delay(25); // wait for a second
digitalWrite(13, LOW); // turn the LED off by making the voltage LOW
delay(25);

interrupts();
}

This is from a serial event example code.

inputString and stringComplete are declared as globals

void serialEvent() {
  while (Serial.available()) {
    get the new byte:
    char inChar = (char)Serial.read();
    // add it to the inputString:
    inputString += inChar;
    // if the incoming character is a newline, set a flag
    // so the main loop can do something about it:
    // if (inChar == '\n') {
    stringComplete = true;
    //}

    
  }
}

In an ISR, interrupts are already disable, so the noInterrupts() call is useless.

Re-enabling interrupts in an ISR is not something that is typically done. Where it is done, the person writing the code has a COMPLETE understanding of interrupt handling, interrupt priorities, and of the possible problems that will arise.

That seems to exclude you, so you should not be enabling interrupts in the ISR.

Of course, you only do it at the end, when they would be re-enabled anyway, so doing it is pointless.

While interrupts are disabled you use two functions that rely on interrupts, so that code is never going to work.

Interrupts are meant for things that must be dealt with immediately, like serial data arriving, the clock ticking, etc.

There is nothing about sending serial data that needs to happen IMMEDIATELY, when sending serial data is ssslllooowww. Sending the data a few nanoseconds later is not going to matter.

So, what is it, EXACTLY, that you are trying to accomplish? Do not repeat the lame "check how to use arduino mega for interrupt based serial communication" statement, since sending and receiving serial data is already interrupt based.

The Mega already uses interrupts to receive serial data and put it in the Serial Input Buffer. There seems little point in making the system more complicated. The code in Serial Input Basics can empty the Serial Input Buffer faster than data arrives and does not need interrupts to do so.

The function serialEvent() is not an ISR regardless of its unfortunate name. There is more about it in my link.

...R

The problem is i want to use GPS and GSM when a sms arrives on the GSM i need the GPS co-ordinates to be send by sms again but the problem is i dont want to check the serial port for data again and again thats y i want to use interrupts for that im not very good at arduino so im really confused what to do.

aamir5253:
but the problem is i dont want to check the serial port for data again and again

Why ever not?

It's not like the extra checks will cost you money.

Computers are great at doing things again and again and ag ....

And the system in Serial Input Basics uses a variable to flag when new data becomes available.

...R

Namaste,

Hope below code help you!

char value;

void setup()
{
pinMode(13, OUTPUT); // configuring pin 13 as output
UBRR0 = 103; // for configuring baud rate of 9600bps
UCSR0C |= (1 << UCSZ01) | (1 << UCSZ00);
// Use 8-bit character sizes
UCSR0B |= (1 << RXEN0) | (1 << TXEN0) | (1 << RXCIE0);
// Turn on the transmission, reception, and Receive interrupt
interrupts(); // enable global interrupt
}

void loop()
{
if (value=='1'){
digitalWrite(13,HIGH);}

if (value=='2'){
digitalWrite(13,HIGH);}

}

//interrupt routine for serial communication
ISR(USART_RX_vect)
{
value=UDR0; // read the received data byte in temp
}

Try the following sketch in a MEGA-UART1 + BT + Phone setup (Fig-1).

volatile char x;
volatile bool flag = false;
void setup()
{
  Serial.begin(9600);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  //--configure UART1 of MEGA: Bd = 9600; RXRDY interrupt----
  UCSR1B = 0b10011000;  //
  UCSR1C = 0b00000110;  //
  //-Bd: 9600-------------
  UBRR1L = 0x67;
  UBRR1H = 0x00;
  //--global int enable--
  bitSet(SREG, 7);
}

void loop()
{
  if (flag == true)
  {
    Serial.print(x);
    flag = false;
  }
}

ISR(USART1_RX_vect)
{
  //digitalWrite(13, HIGH); //debugging code
  x = UDR1;    //recive from phone via BT
  UDR1 = x;    //sending to phone via BT
  flag = true;
}

hc5Mega.png
Figure-1:

hc5Mega.png

This Thread was sleeping happily for 4 years. Leave it to its slumbers.

...R