Datasheet
Button
560
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Libraries
mikroC PRO for AVR
CHAPTER 6
Prototype
unsigned short Button(unsigned short time, unsigned short
active_state)
Returns
- 255 if the pin was in the active state for given period.
- 0 otherwise
Description
The function eliminates the influence of contact flickering upon pressing a button
(debouncing). The Button pin is tested just after the function call and then again
after the debouncing period has expired. If the pin was in the active state in both
cases then the function returns 255 (true).
Parameters :
- time: debouncing period in milliseconds
- active_state: determines what is considered as active state. Valid
values:
0 (logical zero) and 1 (logical one)
Requires
Global variables :
- Button_Pin: Button pin line
- Button_Pin_Direction: Direction of the button pin
must be defined before using this function.
Example
On every PB0 one-to-zero transition PORTC is inverted :
// Button connections
sbit Button_Pin at PINB.B0; //Input pin, PINx register is used
sbit Button_Pin_Direction at DDRB.B0;
// End Button connections
bit oldstate; // Old state flag
void main() {
Button_Pin_Direction = 0; // Set pin as input
DDRC = 0xFF; // Configure PORTC as output
PORTC = 0xAA; // Initial PORTC value
oldstate = 0;
do {
if (Button(1, 1)) { // Detect logical one
oldstate = 1; // Update flag
}
if (oldstate && Button(1, 0)) {//Detect one-to-zero transition
PORTC = ~PORTC; // Invert PORTC
oldstate = 0; // Update flag
}
} while(1); // Endless loop
}