Author Topic: Arduino interrupts.  (Read 19948 times)

0 Members and 1 Guest are viewing this topic.

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 813
  • Country: au
Arduino interrupts.
« on: September 12, 2011, 09:50:03 am »
Is there a hardware interrupt for serial rx?

I know PIC chips have such an interrupt but i have not seen any ref regarding it for Arduinos.

Regards
Testing one two three...
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: Arduino interrupts.
« Reply #1 on: September 12, 2011, 10:33:35 am »
AVRs have 3 serial interrupts, (RX Complete, TX Complete and Data Register empty) , so the hardware does.
It would be pretty stupid if the Arduino platform didn't use them so they must be there somewhere.
I'm not sure what Arduino call them though, (sorry, i'm not an Arduino expert).

Ya can't really compare a PIC with an Arduino.
The Arduino platform is an extra layer on top of an ATmega AVR designed to simplify things and make it easier for people with little programming ability to write code. As a result some things have been hidden away.
 
I find people seem to be forgetting that an AVR is a great MCU by itself without any Arduino layer on top of it. :)
« Last Edit: September 12, 2011, 10:58:03 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline johnboxall

  • Supporter
  • ****
  • Posts: 524
  • Country: au
  • Problems are solved by taking action.
    • Tronixlabs Australia
Re: Arduino interrupts.
« Reply #2 on: September 12, 2011, 10:52:27 am »

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 813
  • Country: au
Re: Arduino interrupts.
« Reply #3 on: September 13, 2011, 04:53:19 am »
Quote
http://www.arduino.cc/en/Reference/Interrupts

I have looked over all the Arduino docs. No mention of Serial Interrupts at all. I guess it must have to be done manually.
Anyone know of a way to set these up within Arduino?

Regards
Testing one two three...
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9608
  • Country: us
Re: Arduino interrupts.
« Reply #4 on: September 13, 2011, 05:11:50 am »
I think the expectation would be to use the Arduino serial communications library rather than handling the serial comms directly.

If that is not suitable, I suppose you would have to write raw AVR code as a function/library of your own and call it from your Arduino code.

I don't know much about Arduino and have just recently been curious about its capabilities. I can tell you that comparing a raw AVR program with a typical Arduino program is like night and day. Larry Wall wrote that three virtues of a programmer are Laziness, Impatience and Hubris. Arduino programming certainly supports the first two of those.

Presuming you can't (or don't want to) use the high level Arduino facilities then you could always use the Arduino hardware platform and program the AVR directly at the hardware level.
I'm not an EE--what am I doing here?
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 813
  • Country: au
Re: Arduino interrupts.
« Reply #5 on: September 13, 2011, 05:37:43 am »
I am rapidly heading toward getting a C compiler for AVR. There has to be an open source project for it somewhere. I am thinking i really need to get over my allergy to 'C' an learn to use it.

Regards
Testing one two three...
 

Offline Bored@Work

  • Super Contributor
  • ***
  • Posts: 3932
  • Country: 00
Re: Arduino interrupts.
« Reply #6 on: September 13, 2011, 06:17:47 am »
I am rapidly heading toward getting a C compiler for AVR. There has to be an open source project for it somewhere.

WinAvr for Windows:

A GCC-based AVR compiler with additional tools, compiled for Windows. A little bit old, but recommended. Comes with a Windows version of avrdude (see below).

AVR Toolchain:

A GCC-based AVR compiler for Windows with barely enough to compile something. New, full of bugs. A pile of shit.

"Bingo's scripts for avr-gcc":

Script and instructions to build a good GCC version for AVR on Linux. Recommended. You need to get programming software called avrdude, too. That one is recommended, too (might be included in recent Bingo scripts).

Your Linux distribution's pre-packaged avr-gcc:

Independent of the Linux distribution typically old, with missing patches. Not recommended.

ObDev CrossPack:

AVR GCC for Mac, and some tools. No experience with it.
I delete PMs unread. If you have something to say, say it in public.
For all else: Profile->[Modify Profile]Buddies/Ignore List->Edit Ignore List
 

Offline IanB

  • Super Contributor
  • ***
  • Posts: 9608
  • Country: us
Re: Arduino interrupts.
« Reply #7 on: September 13, 2011, 06:40:21 am »
I am rapidly heading toward getting a C compiler for AVR. There has to be an open source project for it somewhere. I am thinking i really need to get over my allergy to 'C' an learn to use it.
You didn't really say why you need to get at the hardware interrupt directly. You find the existing serial comms support in Arduino to be unsuitable?

Also, AVR Studio is free. Does your C compiler need to be open source, as long as it works?
I'm not an EE--what am I doing here?
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 2004
  • Country: us
Re: Arduino interrupts.
« Reply #8 on: September 13, 2011, 08:16:23 am »
I am not 100% sure, but you should be able to continue using the arduino environment if you want to.  The arduino compiler is basically a C compiler with some extra preprocessing steps to cut down on red tape and a bunch of pre-packaged libraries.  If you don't use the serial library you should be able to write low-level code that mucks with the serial interrupt and sets up your own serial handler.  That would allow you to easily continue using the arduino libraries for everything else.

You may even be able to use the serial library to set up the baud rate and everything, then hijack the receive interrupt and take over from there.

On the other hand, C shouldn't be too scary if you are already comfortable with arduino.

Quote
You didn't really say why you need to get at the hardware interrupt directly. You find the existing serial comms support in Arduino to be unsuitable?

Interrupt driven communications can be quite a handy way to implement a terminal interface if you want to be doing something else at the same time.  As near as I can tell that isn't supported by the arduino libraries. 
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 3103
  • Country: us
Re: Arduino interrupts.
« Reply #9 on: September 13, 2011, 08:50:12 am »
The Arduino libraries have always used interrupts for the receive data path; there's a 128byte FIFO buffer implemented by the ISR code.  In the 1.0 software, interrupts are also used for the transmit path (and the buffer gets split between rx and tx to avoid using too much RAM.)
 

alm

  • Guest
Re: Arduino interrupts.
« Reply #10 on: September 13, 2011, 09:13:11 am »
Also, AVR Studio is free. Does your C compiler need to be open source, as long as it works?
What free, closed-source compiler did you have in mind?
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: Arduino interrupts.
« Reply #11 on: September 13, 2011, 09:51:43 am »
If you install winAVR and then install AVRStudio you can select the winAVRgcc compiler for your AVR Studio projects.
I find this works really well.
It saves you from having to know about makefiles.

Greek letter 'Psi' (not Pounds per Square Inch)
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 2004
  • Country: us
Re: Arduino interrupts.
« Reply #12 on: September 13, 2011, 10:44:03 am »
The Arduino libraries have always used interrupts for the receive data path; there's a 128byte FIFO buffer implemented by the ISR code.  In the 1.0 software, interrupts are also used for the transmit path (and the buffer gets split between rx and tx to avoid using too much RAM.)

Right, but the user code doesn't have access to the interrupt signal.  You still have to poll the rx buffer to get the actual data.  What is often useful is to have the rx interrupt fill a buffer until you have a complete line, handle directly commands that can or must be executed within interrupt context, then signal to the main loop for anything else.  It can also be used for synchronization: if you want to synchronize some data acquisition code with some other process you can send a serial byte trigger.  Obviously using the change detection interrupt is even better in this instance, but if you already have serial communication available it may be good enough.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: Arduino interrupts.
« Reply #13 on: September 13, 2011, 11:27:11 am »
What is often useful is to have the rx interrupt fill a buffer until you have a complete line, handle directly commands that can or must be executed within interrupt context, then signal to the main loop for anything else.

That's probably expecting a bit much. The Arduino was originally developed so people studying for a bachelor of arts could build/program electronic stuff for their art projects.

Granted, it's grown a huge amount since then, but it's still about simplicity rather than efficiency.
« Last Edit: September 13, 2011, 11:29:44 am by Psi »
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 813
  • Country: au
Re: Arduino interrupts.
« Reply #14 on: September 13, 2011, 01:38:46 pm »
It seems strange to me to have interrupts for pins but then not Serial, i use Serial interrupts for most of my serial io handling as it saves time within the main loop.

Regards
Testing one two three...
 

Online ejeffrey

  • Super Contributor
  • ***
  • Posts: 2004
  • Country: us
Re: Arduino interrupts.
« Reply #15 on: September 13, 2011, 01:46:59 pm »
That's probably expecting a bit much. The Arduino was originally developed so people studying for a bachelor of arts could build/program electronic stuff for their art projects.

Granted, it's grown a huge amount since then, but it's still about simplicity rather than efficiency.

I am not complaining at all.  The arduino platform is great because you can almost always start off simple, then drop down to the lower level as needed.  That is really a huge advantage over any microcontroller system based on byte code or a high level language.  Because you are really writing in C, you can always use the low level stuff when you need it.  I was merely trying to explain why someone might want to install a serial port interrupt handler even though the arduino uses interrupt driven serial IO already.
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 3103
  • Country: us
Re: Arduino interrupts.
« Reply #16 on: September 13, 2011, 04:35:56 pm »
Arduino 1.0 adds a SerialEvent handler called before each invocation of loop(), which gives you a sort of pseudo-interrupt capability.  I'm not sure how that will work out.

Quote
What is often useful is to have the rx interrupt fill a buffer until you have a complete line, handle directly commands that can or must be executed within interrupt context, then signal to the main loop for anything else.
eww.  trends in modern operating systems are toward the other direction, with the ISR doing nothing but signaling that a microtask needs to wake up and deal with data (the ISR wouldn't even read from the UART.)  Personally, I think that's going way too far, but actually parsing commands in the ISR is too far in the other direction.  ISRs should be deterministic and relatively consistent in the time they take to execute.  You can implement a state machine in your ISR to parse a SLIP or PPP packet, but having it suddenly take much more time because a newline came in is a bad idea.
 

Offline Psi

  • Super Contributor
  • ***
  • Posts: 7411
  • Country: nz
Re: Arduino interrupts.
« Reply #17 on: September 14, 2011, 01:13:22 am »
That's probably expecting a bit much. The Arduino was originally developed so people studying for a bachelor of arts could build/program electronic stuff for their art projects.

Granted, it's grown a huge amount since then, but it's still about simplicity rather than efficiency.

I am not complaining at all.  The arduino platform is great because you can almost always start off simple, then drop down to the lower level as needed.  That is really a huge advantage over any microcontroller system based on byte code or a high level language.  Because you are really writing in C, you can always use the low level stuff when you need it.  I was merely trying to explain why someone might want to install a serial port interrupt handler even though the arduino uses interrupt driven serial IO already.

Yeah, i was just commenting in general, wasn't aiming it at you :)
Greek letter 'Psi' (not Pounds per Square Inch)
 

Offline Mechatrommer

  • Super Contributor
  • ***
  • Posts: 9322
  • Country: my
  • reassessing directives...
Re: Arduino interrupts.
« Reply #18 on: September 14, 2011, 12:22:09 pm »
in arduino mega, at1280, usart register are UCSRnX (X=A,B,C), interrupt related are in UCSRnB (RXCIEn, TXCIEn, UDRIEn), vector for the INT USART0 are H0032-H0034 (USART0 RX, UDRE, TX)... from the datasheet. other arduino i dont know, but should be about the same name i suspect.
if something can select, how cant it be intelligent? if something is intelligent, how cant it exist?
 

Offline Gadget999

  • Newbie
  • Posts: 2
Re: Arduino interrupts.
« Reply #19 on: September 16, 2011, 08:29:38 am »
writing to the serial port is simple

for example inside the code for the interrupt - ie a pin has been triggered

Serial.Print("TEXT") will send data to the port

you can see a list of the serial commands here

http://arduino.cc/en/Reference/Serial



i was resistant to arduino at first but now i have tried it there is no going back - the amount of pre written software libraries is huge !

you can always get the job done with an arduino

and if you want to scale the project up - it is simple to do because it is AVR

 

Offline PeterG

  • Frequent Contributor
  • **
  • Posts: 813
  • Country: au
Re: Arduino interrupts.
« Reply #20 on: September 16, 2011, 09:28:47 am »
I agree, writing to the serial port is easy, however i would like to have access to the RX interrupt. I have found it more efficient to is this interrupt rather than check the port via the main loop.

To my thinking you should be able to use something similar to the following statement.

attachInterrupt(Serial1, RXdata, RECEIVE); //Where Serial1 is the serial port in use.

Regards

Testing one two three...
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf