Document created by GUO XIAOLI Employee on Jul 12, 2012Last modified by johnmc on Sep 18, 2012
Version 4Show Document
  • View in full screen mode

This tutorial is meant to introduce you to the use of a push button. It will give an explanation and example code of how you could implement a push button. Push buttons can be a great way to set a number of different states. Push buttons are advantageous because you can change your code physically versus pulling up the debugger every time you want to make a little change.





A button can have many different functions on your autonomous vehicle. Most notably the button has been used for testing to start, stop, or put your car in a configuration mode. Configuration mode would let you test to see if all the peripherals except the motors are working. This would help you test your camera data and servo angles without always having to run after your car. During your race you can even set your button to different speed states. Since you have two chance to traverse the track you may have a slower safer speed on one state and a faster speed the pushes the limit on another state. In the end, what you do with the button is up to you and the choices are unlimited.

Description of Example Code


Below there will be an example of how to implement a push button. This push button will be connected to PTA16. When reading this pin a high, "1" or 5V, is considered "OFF" and a low, "0" or 0V, is considered "ON." To reduce the effects of bounce and/or the chance of a false press, additional code has been added to filter the signal. This is done by checking the button every 10ms for 50ms. If the button has been pressed for 3 or more of the 5 times we will change the state, otherwise it will not be considered a "press."

Button Initialization


Here is the initialization code that can be put in a header such as "Button.h."




  // Button's Defined State 
  // 0 means button not pressed
  // 1 means button pressed
short fButton = 0;
short iButtonCount=0;
short iButtonTimer=0;

  // Button Triggered Start time
short iButtonTime;

void initButton() 
  //turn on clock to Port A

  // configure pin to be GPIO

  // configure PTA16 to be input
  GPIOA_PDDR &= (0<<16);  



See GPIO for explanation of how these specific commands work.

Button Implementation


Below is an example of a function that implements the button function. This function can be stored in a header file "Button.h" along with the initialization code. To call this function you would just place "readButton();" in your 10ms Flextimer source code.

Read comments for description of each line


Void readButton () {             
     short fButtonState  = 0;   // initializes the button state to "OFF"
     iButtonCount++;            // increments button count

     if (GPIOA_PDIR & (1<<16)) {                    // if button read as high then its off otherwise its on
          fButtonState = 0;
     } else {
          fButtonState = 1;
     if (fButtonState && ! fButton) {                          // if the button is pushed and it previously wasn't then start a count
          if (iButtonTimer <= 1) {                                              
               iButtonCount = 0;                                 // Reset the Button Count if the timer is less or equal to 1
     } else if (! fButtonState && fButton) {
          if ( iButtonTimer <= 1) {
               iButtonCount = 0;                               // Reset the Button count if the timer is less or equal to 1
     if ( iButtonCount > BUTTONLENGTH && iButtonTimer >0) {     // if button has been read for 50ms check to see if we passed the test!
          if ( iButtonTimer > (3*BUTTONLENGTH/4) && fButton) {
               // fButton = 0;
          } else if (iButtonTimer > (3*BUTTONLENGTH/4) && ! fButton) {
               fButton = 1;
          iButtonCount = 0;
          iButtonTimer = 0;