2018
February Thursday 22

Bookmark and Share

Related Articles...

MILWAUKEE WI Arduino Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Arduino Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Raspberry Pi Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Raspberry Pi Make Space Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Internet of Things (IoT) Meet Up Make Space

WANTED: CORE TEAM MEMBER TO HELP START NON-PROFIT GROUP.

WANTED: Angle Investor Start-Up Funding

Large Industrial Zoned Make Space Acquired!

Full Advanced Fabrication Facility, machine Shop, Welding, Electronics Lab Equipment Acquired!

Email :

COASMailBox @ gmail.com

MILWAUKEE WI Machine Repair, & Installation  - Tier 1 Business Support

Machine Technician Mechanic 20+ Years  Personal Experienced In Industrial Factory Machine Servicing, Rebuilding, Installation And Repair - Plus A 35+ Years Experienced Computer Engineer Service Technician Team Ready For Your Outsourced Market Advantage Needs!

Hartung Industrial - Member - CGI Science & Technology Group

Email :

COASMailBox @ gmail.com

2017 REVIEW | 'IoT & Embedded MAKER SPACE - MKE' Group

 

Arduino Real Time OS

2017 OCTOBER | by Gene Casanova

Senior Systems Engineer


Using Interrupts on Arduin

A real time processing mechanism, referred to as "interrupt" is available on Arduino systems.

An Interrupt enables a processor to respond as soon as possible (ASAP) to events.

When a specified signal is detected by a processor, the current process being executed by the processor, will be haulted as soon as possible,  and then the process begins processing a sub-process.  When the sub-process is finished, the processor resumes the interrupted process.

Utilizing this interrupt mechism, is referred to as "real time" processing.

Utilizing interrupts, enables a system to react in 'real time', effectivly to events.  Interrupts enable a processor to execute multiple tasks, without executing every task in serial order and becomming delayed by a slow executing process, while a more important event occures.


Interacing With Real World Events

Buttons are a typical daily "event" for a computer system, including embeded computers, and other types, encounter.

With my lifelong interest in sciences, learning about, and becoming a scientist, I conceive experiments to enhance my learning process, gather a knowledgebase of factual information, and acchive a higher level of enlightenment.

I recommend starting here, with an experiment, to start learning and gaining a deeper understanding for the interrupt concept.

A button experiment - An Arduino source code for using an interrupt to monitor a button event.

Start with the example Arduino "Button" sketch, included with the Arduino IDE application.

Open the Arduion IDE applcations, and go to the main menu. Click on "File" > "Examples" > "Digital" > "Button".

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  }
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Monitoring a pin for an event, can be done using an interrupt.

The experimental soource code below, introduces and utilizes an interrupt to monitor the pin for an event:

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// Variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
	// Attach interrupt to ISR vector:
attachInterrupt(0, pin_ISR, CHANGE); } void loop() { } void pin_ISR() { buttonState = digitalRead(buttonPin); digitalWrite(ledPin, buttonState); }

Source Code Breakdown:

┬áThe work to be done in the first source code (process), was done by a "if/else" function. 

The work to be done, was changed in the second process, where a new function is created to do the work; the function is given the name "pin_ISR()".  This type of function is referred to as an "interrupt service routine" (IRS).   The IRS process must be short-as-possible and be most efficient (optimized).

When the interrupt event happens, the processor begins to execute the IRS.  There is no need for a loop in thsi soruce code process.

The function "setup()" routine, is used to set the system interrupts.  The function, "attachInterrupt()", creats a single inruppt, and accepts three arguments:

1) Interrupt Vector
Determines the point in memory, to monitor for an interrupt/event.
A reference to a point in memory for the processor to monitor for an interup to occure.  The specified vector memory location, corresponds to a specific external pin.
BEWARE ----- Not all pins can generate an interrupt.  On an Arduino UNO system, pins 2 and 3, are capable of generating interrupts, and they correspond to interrupt vector "0" for pin-2 and "1" for pin-3. For a list of what pins are available as interrupt pins, see the Arduino documentation on "attachInterrupt()".
2) The Function Name of the Interrupt Service Routine
The name determines the code to execute when the interrupt condition is met.
3) The Interrupt Mode
Determines what pin action triggers an interrupt.  An Arduino UNO, supports four interrupt modes:
RISING
Activates an interrupt on a rising edge of the interrupt pin.
FALLING
Activates on a falling edge.
CHANGE
Responds to any change in the interrupt pin's value.
LOW
Triggers any time the pin is a digital low.

The function "attachInterrupt()" sets interrupt vector 0 (pin 2), to respond to interrupts by executing the function "pin_ISR()", and to execute the function "pin_ISR()" with any change of state on pin-2.


Volatile

The ISR uses the variable "buttonState" to store the state of the pin.

The definition of the function "buttonState". The variable id defined as type "volatile int".  "volatile" is a C keyword applied to variables.  It means, the value of the variable, is not entirely within control of the program/main-process.  The type "volatile int", indicates the value of "buttonState" could change, without control over the change (in this case, an outside real world input).

The "volatile" keyword, protects from any accidental compiler optimization.  Compilers have a few purposes in addition to parsing source-code to build a machine-code executable process.  One of the tasks is to remove unused source code variables.  Since the variable "buttonState" is not used or executed directly in the "loop()" or "setup()" functions, there is a risk the compiler might remove it as an unused variable.  The "volatile" keyword directs the C compiler to leave the variable in place.


Interrupts

Interrupts are a simple way to make a system more responsive to time sensitive tasks.  They also have the added benefit of freeing the main "loop()" function to focus on some primary task in a system.

The example above illustrates the most basic use of an interrupt.  Interrupts can be used for reading an I2C device, sending or receiving wireless data, and starting/stopping a motor.

An iterrupt is triggered based on a hardware condition.  An interrupt service routine (IRS) is a function executed when a hardware condition is met.  Specify what function to execute for an iterrupt, only once in a source code.


Arduino Open Source Community member - Tier 1 Full Stack Developer

'IoT & Embedded MAKER SPACE - MKE'

CONACT to learn about a new group in METRO Area of WI & IL - Milwaukee, Racine, Kenosha, South Eastern WI And Northern IL, space and/or spaces - Forming Now!

We Have Industrial Zoned Making Space With Outfitted Machine Shop And Lab Space Available!  Multiple Dedicated Online Locations Available In Milwaukee 3rd Ward.

I am building a network of creative Makers/Doers!... Come Learn More, Join, Engage, Enjoy, Create A Solid Future Of Fun And Reward... Come Help..... Core Team Members Wanted, Or Simply be A Hangout Member Learning, Socializing/Networking... Start-Up Incubator Space, Students Tutoring Space, Retired... Hobbyists, Educators, Scientists, Researchers, Developers...

Mega Industrial Machines Building!!!!  AI Development.... Super Computing.... @ Tier 1 Level.  Locations looking to establish a network space, include Chicago IL, Melbourne Vic Australia.


Use The Technology Wisely & Keep It Simple

- Cheers!

Gene Casanova


Need More?   Need Help? ........Software, Network, System & Data Center Builder & Developer | Development Services Available - Freelance Small Jobs; To Outsourced Long Term Service Provider Contract Labor Available.

CGI Computer Wares | EST 1979

Send E-MailCONTACT: [click]@CGIComputerWares.com