Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Pages: [1]   Go Down
Author Topic: serial.available() from polling to interrupt  (Read 2404 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hi everyone,
i'm working on a chat between my smartphone and arduino due. i'm using a bluetooth that communicates with arduino through the serial port.

in the loop function i've always used if(Serial.available) polling permanently the processor. how can i move from this polling procedure to an interrupt based function?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 131
Posts: 8690
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Look up serialEvent,

http://arduino.cc/de/Reference/SerialEvent

or roll your own ISP for the serial port.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

serialEvent is checked after every loop, i need to know if i can trigger an interrupt when data arrive on serial port
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 131
Posts: 8690
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That already happens in the Arduino core functions, if you want to grab that interrupt you may have to modify the ISR in HardwareSerial.cpp.

Another option may be to call serialEvent yourself, I don't think there's any reason to wait for the main loop to do it.

_____
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you please give me a code example?
do tou think something like this could work http://stackoverflow.com/questions/10201590/arduino-serial-interrupts
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 131
Posts: 8690
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

I don't have any example code I'm afraid, but that Stackoverflow thread seems about right, good place to start anyway.

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

what DO you think  about connecting RX serial pin to a KNOWN pin, let's say pin P. and than i should attach an interrupt on pin P?
Logged

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

'and How do i call a serial event in the loop()?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 131
Posts: 8690
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

It looks like void serialEvent() is weakly defined so you can add your own function

Code:
void serialEvent() {
   char c;
// what you want to do, for example
   if (Serial.available())  {
      c = Serial.read());
}

EDIT: Two minutes looking and I found this tute

http://arduino.cc/en/Tutorial/SerialEvent

You should learn to do research as well as coding.

Quote
about connecting RX serial pin to a KNOWN pin, let's say pin P. and than i should attach an interrupt on pin P?
That should work, you may also be able to use a pin-change interrupt on the Rx pin as well, but running Rx to one of the standard interrupt pins would be cleaner I think.

______
Rob



« Last Edit: November 21, 2013, 07:44:08 pm by Graynomad » Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

none of the above solutions has worked till now. thus i was thinking of editing USART/UARTClass cpp file, including a flag in the interrupt code of the usart that will help me wake up chip. what do you think about?
Logged

nr Bundaberg, Australia
Offline Offline
Tesla Member
***
Karma: 131
Posts: 8690
Scattered showers my arse -- Noah, 2348BC.
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

So you tried tying the Rx to an interrupt pin and that didn't work?

______
Rob
Logged

Rob Gray aka the GRAYnomad www.robgray.com

Offline Offline
Full Member
***
Karma: 1
Posts: 160
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

No, i couldn't becouse i'm not currently working on an Arduino DUE but on a SAM3X based board, therefor i can't make hardware changes, but i'll try that solution asap
Logged

Pages: [1]   Go Up
Arduino Forum upgrade scheduled for Monday, October 20th, 11am-4pm (CEST). Sorry for the inconvenience!
Jump to: