Go Down

Topic: serial.available() from polling to interrupt (Read 2 times) previous topic - next topic

aureliusss

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?

Graynomad

Look up serialEvent,

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

or roll your own ISP for the serial port.

_____
Rob
Rob Gray aka the GRAYnomad www.robgray.com

aureliusss

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

Graynomad

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
Rob Gray aka the GRAYnomad www.robgray.com

aureliusss


Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

aureliusss

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?

aureliusss

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

Graynomad

#8
Nov 22, 2013, 01:40 am Last Edit: Nov 22, 2013, 01:44 am by Graynomad Reason: 1
It looks like void serialEvent() is weakly defined so you can add your own function

Code: [Select]
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



Rob Gray aka the GRAYnomad www.robgray.com

aureliusss

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?

Graynomad

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

______
Rob
Rob Gray aka the GRAYnomad www.robgray.com

aureliusss

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

Go Up