Anniyan_x
Member
hi, i have to read 8 inputs switches, 3 connected to portA and 5more in portB, i have to use this connections, if all are in single port i can read the whole and use masking to chk which pin is pressed and do deboucing then call the corresponding event..but now i face a problem coz there 2different ports, in portA the switches are connected to pin4,5,1 and in portb the pins are connected to pin3,4,5,6,7..the switches are pull to GND..LOW..and wn pressed goes high....and my question is whats the most efficient way to do this..reading the inputs+ debouncing and call the event.
last time wn i did in same port the code was as below and this time the switches are pulled up. the code is as below
/*complement coz wn pressed pull to GND */
#define PUSH_BUTTONS ( (~PCDR ) & 0x03 )
#define UART_BT5 0b00000001
#define Button_BT6 0b00000010
#define DEBOUNCE_TIME 0x3E80 // approx > 20miliseconds???
while (1)
{
unsigned char B;
B =Debounce ();
switch (B)
{
case UART_BT5 : TX_DATA();
break ;
case Button_BT6 :_nop_();
break ;
default:_nop_();
break ;
}
while ( 0 != Debounce ()) ;
// wait for release stablising..or all buttons released.
}//end while
//--------------------------
unsigned char Debounce ( void )
{
unsigned char PreviousButton = PUSH_BUTTONS ;
int DebounceTime = DEBOUNCE_TIME ;
/* Wait for state stable */
while ( DebounceTime != 0 )
{
if ( PreviousButton != PUSH_BUTTONS )
{
DebounceTime = DEBOUNCE_TIME ; /* state changed : restart delay */
PreviousButton = PUSH_BUTTONS ;
}
else
DebounceTime-- ;
}
switch ( PreviousButton )
{
case UART_BT5 :
case Button_BT6 :
return PreviousButton ;
default:
return 0 ; /* No button pressed or more than one button pressed. */
}
}
so now im thinking modifiying the above code so tht i can read frm differents ports and and do debouncing and call the event..but i need help in how is best way to do this..or modifying the above code,but im not sure how and make the above code work?? or got other methods,codes,ideas and solutions
thanks & regards
anniyan
last time wn i did in same port the code was as below and this time the switches are pulled up. the code is as below
/*complement coz wn pressed pull to GND */
#define PUSH_BUTTONS ( (~PCDR ) & 0x03 )
#define UART_BT5 0b00000001
#define Button_BT6 0b00000010
#define DEBOUNCE_TIME 0x3E80 // approx > 20miliseconds???
while (1)
{
unsigned char B;
B =Debounce ();
switch (B)
{
case UART_BT5 : TX_DATA();
break ;
case Button_BT6 :_nop_();
break ;
default:_nop_();
break ;
}
while ( 0 != Debounce ()) ;
// wait for release stablising..or all buttons released.
}//end while
//--------------------------
unsigned char Debounce ( void )
{
unsigned char PreviousButton = PUSH_BUTTONS ;
int DebounceTime = DEBOUNCE_TIME ;
/* Wait for state stable */
while ( DebounceTime != 0 )
{
if ( PreviousButton != PUSH_BUTTONS )
{
DebounceTime = DEBOUNCE_TIME ; /* state changed : restart delay */
PreviousButton = PUSH_BUTTONS ;
}
else
DebounceTime-- ;
}
switch ( PreviousButton )
{
case UART_BT5 :
case Button_BT6 :
return PreviousButton ;
default:
return 0 ; /* No button pressed or more than one button pressed. */
}
}
so now im thinking modifiying the above code so tht i can read frm differents ports and and do debouncing and call the event..but i need help in how is best way to do this..or modifying the above code,but im not sure how and make the above code work?? or got other methods,codes,ideas and solutions
thanks & regards
anniyan
Last edited: