Interrupt: read a button state

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how to use the Interrupt to react a button state change.

Introduction to GPIO

An Interrupt is a way to stop the normal program flow to do something else (other code block that you program) and allows to the system to react immediately an event (e.g button pressed, timer, etc.).

How to use it

The openPicus APIs allow to set 3 external interrupts (2,3 and 4) and associate each one of them to a specific pin. In this example we use an interrupt to toggle one LED while the main loop toggles the other connected LED.

Flyport PRO

Code example

The functions used in this tutorial are the following:

Name Function Description
 void IOInit(int  io, int  putval) Initializes the specified pin with the desired function. Remappable pins allow the user to set the pin not just as input or output, but also with advanced functionalities, as such UART or SPI.

Parameters: io specifies the pin. putval specifies how the pin must be initialized. The valid parameters for interrupts are the following:

  • EXT_INT2 External Interrupt 2 input pin.
  • EXT_INT3 External Interrupt 3 input pin.
  • EXT_INT4 External Interrupt 4 input pin.
 void INTInit( int intNum, void functionName(), BOOL mode) Initialize the External Interrupt.
Parameters: intNum the number of external interrupt peripheral (2, 3 or 4). functionName the name of the user function to execute. mode the edge mode for interrupt execution (0 on positive edge, 1 on negative edge).
 void INTEnable(int intNum) Enables the External Interrupt.

Parameters: intNum the number of external interrupt peripheral (2, 3 or 4).


Source code for taskFlyport.c:

#include "taskFlyport.h"
 
void external_interrupt_function()
{        
    IOPut(p32, toggle); // Toggles p32 Led
}        
 
void FlyportTask()
{
    int i;
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    IOInit(p31, out); //initializes pin 31 as output
    IOInit(p32, out); //initializes pin 32 as output
    IOPut(p31, off); // turns off led 
    IOPut(p32, off); // turns off led 
 
    IOInit(p7, inup); //initializes pin 7 as input... remember to connect a push button!
    IOInit(p7, EXT_INT2); //sets pin 7 as external interrupt 2
    INTInit(2, external_interrupt_function, 1); // initializes external interrupt 2 and associate a function
    INTEnable(2); // enables external interrupt 2.
 
    while(1)
    {
        IOPut(p31,toggle); // Toggles p31 LED
        vTaskDelay(100);  // wait for 1 second
    }    
}


Flyport

Bill of Materials

  • 1x Flyport Module
  • 1x miniUSB programmer
  • 2x LED (Green, Red)
  • 1x Push button
  • 2x Resistors (330 Ohm)
  • 1x Proto Nest Board
  • 1x Breadboard


Schematic

DinSchematic 2LED.png


DigitalIN Image 2LED.jpg

Code example


The functions used in this tutorial are the following:

Name Function Description
 void IOInit(int  io, int  putval) Initializes the specified pin with the desired function. Remappable pins allow the user to set the pin not just as input or output, but also with advanced functionalities, as such UART or SPI.

Parameters: io specifies the pin. putval specifies how the pin must be initialized. The valid parameters for interrupts are the following:

  • EXT_INT2 External Interrupt 2 input pin.
  • EXT_INT3 External Interrupt 3 input pin.
  • EXT_INT4 External Interrupt 4 input pin.
 void INTInit( int intNum, void functionName(), BOOL mode) Initialize the External Interrupt.
Parameters: intNum the number of external interrupt peripheral (2, 3 or 4). functionName the name of the user function to execute. mode the edge mode for interrupt execution (0 on positive edge, 1 on negative edge).
 void INTEnable(int intNum) Enables the External Interrupt.

Parameters: intNum the number of external interrupt peripheral (2, 3 or 4).


Source code for taskFlyport.c:

#include "taskFlyport.h"
 
void external_interrupt_function()
{		
  IOPut(P11,toggle);//Toggle the LED Red
}		
void FlyportTask()
{
     IOInit(p9,out); //initialized the pin 9 as output
     IOInit(p11,out); //initialized the pin 11 as output
     IOInit(p7,inup); //initialized the pin 7 as input
     IOInit(p7, EXT_INT2); //set the pin 7 as the external interrupt 2
     INTInit(2, external_interrupt_function, 1);//initialized the external interrupt 2 and associate a function
     INTEnable(2);//enables the external interrupt 2.
 
     while(1)
     {
       IOPut(p9,toggle);//Toggle the LED Green
       vTaskDelay(100);  
     }	
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox