Arduino interrupts. - Page 1
Forum section: electronics / general

author: PeterG
author_rank: Frequent Contributor
timestamp: September 12, 2011, 09:50:03 am
content: 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

author: Psi
author_rank: Super Contributor
timestamp: September 12, 2011, 10:33:35 am
content: 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.

author: johnboxall
author_rank: Unknown
timestamp: September 12, 2011, 10:52:27 am
content: http://www.arduino.cc/en/Reference/Interrupts

author: PeterG
author_rank: Frequent Contributor
timestamp: September 13, 2011, 04:53:19 am
content: QuoteI 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

author: IanB
author_rank: Super Contributor
timestamp: September 13, 2011, 05:11:50 am
content: 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.

author: PeterG
author_rank: Frequent Contributor
timestamp: September 13, 2011, 05:37:43 am
content: 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

author: Bored@Work
author_rank: Super Contributor
timestamp: September 13, 2011, 06:17:47 am
content: Quote from: PeterG on September 13, 2011, 05:37:43 amWinAvr 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.

author: IanB
author_rank: Super Contributor
timestamp: September 13, 2011, 06:40:21 am
content: Quote from: PeterG on September 13, 2011, 05:37:43 amYou 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?

author: ejeffrey
author_rank: Super Contributor
timestamp: September 13, 2011, 08:16:23 am
content: 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.QuoteInterrupt 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.

author: westfw
author_rank: Super Contributor
timestamp: September 13, 2011, 08:50:12 am
content: 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.)

author: Unknown
author_rank: Unknown
timestamp: September 13, 2011, 09:13:11 am
content: Quote from: IanB on September 13, 2011, 06:40:21 amWhat free, closed-source compiler did you have in mind?

author: Psi
author_rank: Super Contributor
timestamp: September 13, 2011, 09:51:43 am
content: 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.

author: ejeffrey
author_rank: Super Contributor
timestamp: September 13, 2011, 10:44:03 am
content: Quote from: westfw on September 13, 2011, 08:50:12 amRight, 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.

author: Psi
author_rank: Super Contributor
timestamp: September 13, 2011, 11:27:11 am
content: Quote from: ejeffrey on September 13, 2011, 10:44:03 amThat'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.

author: PeterG
author_rank: Frequent Contributor
timestamp: September 13, 2011, 01:38:46 pm
content: 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

author: ejeffrey
author_rank: Super Contributor
timestamp: September 13, 2011, 01:46:59 pm
content: Quote from: Psi on September 13, 2011, 11:27:11 amI 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.

author: westfw
author_rank: Super Contributor
timestamp: September 13, 2011, 04:35:56 pm
content: 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.Quoteeww. 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.

author: Psi
author_rank: Super Contributor
timestamp: September 14, 2011, 01:13:22 am
content: Quote from: ejeffrey on September 13, 2011, 01:46:59 pmYeah, i was just commenting in general, wasn't aiming it at you

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 14, 2011, 12:22:09 pm
content: 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.

author: Gadget999
author_rank: Newbie
timestamp: September 16, 2011, 08:29:38 am
content: writing to the serial port is simplefor example inside the code for the interrupt - ie a pin has been triggeredSerial.Print("TEXT") will send data to the portyou can see a list of the serial commands herehttp://arduino.cc/en/Reference/Seriali 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 arduinoand if you want to scale the project up - it is simple to do because it is AVR

author: PeterG
author_rank: Frequent Contributor
timestamp: September 16, 2011, 09:28:47 am
content: 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