Digital Input: toggle LED by push button

From openPicus Wiki
Jump to: navigation, search

Contents

What you are going to learn

This tutorial shows how Flyport can toggle an LED using a push button by a GPIO.

FlyportPRO warning

This tutorial is made for Flyport modules but works on FlyportPRO as well. Just adapt the code since the pinout is slightly different.

Introduction to GPIO

GPIO (General Purpose IO) are pins to interface an embedded system to external digital devices. A GPIO pin can be configured both as input or output. For example a GPIO can drive a led or read the state of a button.

How to use it

In this tutorial we explain how toggle an LED, using a pushbutton. The pushbutton input is managed by the function IOButtonstate that checks the rising/falling edge of the input. This function internally manage the "debounce" of the button. This function do not generate an interrupt so you need periodically poll it.
In the main loop the firmware performs a check on the button state, when the button is pressed, the LED is toggled.

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 allows the user to set the pin not only as input/output, but also as UART, SPI and others.
 void IOPut (int io, int putval) Puts the putval value on the specified IO output pin.
 void IOButtonState(int io) Polls for the state of the button implemented on the specified pin. This command doesn't return the voltage level of the pin, but the state (button has been pressed or released). The "debounce" of the button is embedded into the function. It doesn't matter if the button is implemented with a "low logic" or "high logic": you just have to initialize the pin like "inup" or "indown".


Source code for FlyportTask.c:

#include "taskFlyport.h"
void FlyportTask()
{
    int i;
    // Disable all Analog channels
    for(i = 0; i < 9; i++)
        ADCDetach(i);
    ADCAttach(2);    // Enable only Analog 2 channel (pin p5)
 
    IOInit(p31, out); //initialize the pin 31 as output
    IOInit(p11, inup); //initialize the pin 11 as input
 
    while(1)
    {
       if (IOButtonState(p11) == pressed)
            IOPut(p31,toggle);
    }
}


Flyport

Bill of Materials

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


Schematic

DinSchematic.png


DigitalIN Image.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 allows the user to set the pin not juar as input/output, but also as UART, SPI and others.
 void IOPut (int io, int putval) Puts the putval value on the specified IO output pin.
 void IOButtonState(int io) Polls for the state of the button implemented on the specified pin. This command doesn't return the voltage level of the pin, but the state (button has been pressed or released). The "debounce" of the button is embedded into the function. It doesn't matter if the button is implemented with a "low logic" or "high logic": you just have to initialize the pin like "inup" or "indown".


Source code for FlyportTask.c:

#include "taskFlyport.h"
void FlyportTask()
{
   IOInit(p9,out); //initialize the pin 9 as output
   IOInit(p7,inup); //initialize the pin 7 as input
 
   while(1)
   {
       if (IOButtonState(p7) == pressed)
            IOPut(p9,toggle);
   }
}
Personal tools
Namespaces

Variants
Actions
START HERE
DEVELOPMENT
HARDWARE INFO
RESOURCES
PHASED OUT
Toolbox