Electronic – Interfacing Switch Input with Seven Segment Display using ARM 7

armarm7microcontroller

I am trying to detect a switch input so that, if the switch is pressed, a number is incremented on the seven segment display. For example if the seven segment display shows 0, and the switch is pressed, the SSD will now show 1. If pressed again, it will show 2 and so on.

  • My seven segment display is connected on P0.0 to P0.6
  • MY switch is on P0.9

How can I detect each time the switch is pressed? I am trying to increment a variable k in my program so that each time the switch is pressed, k is incremented, and thus I will be able to perform different functions each time the switch is pressed. However this part is not working properly.

I am thinking something similar to the following program. However, I must be overlooking something as only the first button click is working. When the switch is pressed, 1 appears on the display. Nothing happens when it is pressed again.

#include <LPC21xx.h>
#define a 0x00000001 
#define b 0x00000002
#define c 0x00000004
#define d 0x00000008
#define e 0x00000010
#define f 0x00000020
#define g 0x00000040


void small_delay (void);

void infinite (void);
int main (void)
{
int k = 0;
unsigned int SW1;

PINSEL0 = 0;                                //Set P0.0 - P0.15 as GPIOS
PINSEL1 = 0;                                //Set P0.16 - P0.31 as GPIOS
IO0DIR  = 0xFFFFFDFF;               //Setting  P0.9 as input for SW1     

while (1)
{

    SW1 = IO0PIN & 0x00000200; //switch connected on P0.9

    if ( SW1 == 0x00000200 )        //when not pressed
    {   

    }

  if ( SW1 != 0x00000200 )  //when pressed      
    {
        k++;

        if (k == 1){
        IO0SET = b;                     
        IO0SET = c;                         //displaying number 1 

        }       

        else if (k == 2){

        IO0CLR = c; 
        IO0SET = a;
        IO0SET = b;
        IO0SET = g;                         //displaying number 2
        IO0SET = d;
        IO0SET = e;
        //small_delay();
            }
        }
    }
}

 void small_delay (void) 
 {
 unsigned int i, j;

 for (i=0; i<1000; i++)
 {  
    for (j=0; j<1000; j++)
     {
     }
 }
 }

Any suggestions or pointers on what I could improve, would be appreciated.

Best Answer

Two issues I can see. Firstly when setting the display to show a 2 you don't first clear the display so all of the segments that were lit remain lit.

Secondly my guess is that you are seeing switch contact bouncing issues. When a switch or button closes the contacts touch, bounce apart, touch again, bounce apart etc... this can go on for several milliseconds and will look like lots of very fast button presses to the software.

The techniques to avoid this are known as debouncing, you can either implement them in hardware (a low pass filter on the input pin) or software (wait for the value on the input to remain stable for a fixed length of time) or a mixture of both.

For details on how to implement this sort of thing see google or search here for debouncing.