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);
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.
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.