to read a switch

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

to read a switch

1,099 Views
avditkohli
Contributor III

Hi,

I am trying to read a mechanical switch to initialise a LCD every time the switch is turned ON. I use the following code :

if(ON==0)// is the switch OFF
{
delay_ms(8);
if(ON==1)// If turned ON
lcd_init();
}

 

The LCD does not initialise correctly when I turn ON the switch.

 

Regards,

Avdit

Labels (1)
Tags (2)
0 Kudos
Reply
4 Replies

963 Views
wendersonolivei
Contributor IV

Hi Avdit,

I think which you problem is in a multiple inicializations, or glitches at I/O.

If you can use a simple debounce filter to avoid glitches at I/O, like the example code:

#define TRESHOLD    20 //Here you will define a treshold for the debounce filter

byte debounce = 0;

bool state = 0; //State of your input

void Main()

{

     [...]

     for(;;)

     {

         if(input && (debounce < TRESHOLD)) debounce++;

         else

         {

               if(!input && debounce) debounce--;

         }

         if(debounce >= TRESHOLD) state = 1;

         else state = 0;

          if(state)

          {           

               lcd_init(); //Initializes your lcd

               break; //Get out from loop, to not initialize again

          }

     }

     [...]

}

I hope it helps!

0 Kudos
Reply

963 Views
avditkohli
Contributor III
0 Kudos
Reply

963 Views
avditkohli
Contributor III

Hi Wenderson,

Thanks for your reply. I managed to solve it using a delay of around

50ms to read the change in state of the input.

Avdit

On Thu, Jul 24, 2014 at 7:29 AM, Wenderson Oliveira <

0 Kudos
Reply

963 Views
arpitaagarwal-b
NXP Employee
NXP Employee

Hello Avdit,

With the given details, problem is not clear.

It seems you might be having this code in while loop and using GPIO data register directly to check high and low.

When you will turn ON the mechanical switch, there will be glitches at the I/O port. Because of which you can see flickering in LCD but this should settle down.

I would suggest to use KBI edge detect interrupt to detect switch ON/OFF position. This will avoid any glitches.

-Arpita