In this game, we will control 4 LEDs using 4 switches. Each switch controls a LED. Pressing switch toggles the corresponding LED
Code:
void main() {
TRISB = 0 ; PORTB = 0 ;TRISC = 255 ;while(1){if(PORTC.F0 == 0){PORTB.F0 = ~ PORTB.F0 ;while(PORTC.F0 == 0) ; Delay_ms(50) ;}if(PORTC.F1 == 0){PORTB.F1 = ~ PORTB.F1 ;while(PORTC.F1 == 0) ; Delay_ms(50) ;}if(PORTC.F2 == 0){PORTB.F2 = ~ PORTB.F2 ;while(PORTC.F2 == 0) ; Delay_ms(50) ;}if(PORTC.F3 == 0){PORTB.F3 = ~ PORTB.F3 ;while(PORTC.F3 == 0) ; Delay_ms(50) ;}}}
Interfacing with buttons
Switch bounce problem
-When a button is opened or closed, we perceive that it is a clean operation that really looks like a step function.
-In reality, the contacts of a switch bounce when they make contact, resulting in a jagged signal.
When the signal is passed to the MCU, the microcontroller can recognize it as multiple button presses, which will cause the application software to act as if multiple, very fast button presses have occurred.
Solving the switch bounce problem
To avoid this problem the noisy switch press is de-bounced into an idealized press, or the step function.
No comments:
Post a Comment