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.