For using swith and button, we would like to understand the concept of pull-up and pull-down resistor. A pull up or pull down resistor are used on Input pins to define a state in the case an input does not have anything connected or the connected part is in the define (high impedance) state. Inputs without a defined state have the problem that the input value can be anything (0 or 1), called floating.
It is useful to steer an input pin to a known state if no input is present. This can be done by adding a pullup resistor (resistor to +5V), or a pulldown resistor (resistor to ground) on the input, with 10K being a common value.
Pull-up resistor
Pull-down resistor
PBP Program
'Program to use pull-up resistor(press switch 1) to ON LED1 and pull-down resistor(press switch 2) to ON LED2
DEFINE OSC 20 ' using 20MHz clock oscillator
@ device hs_osc
sw1 var PORTD.2
sw2 var PORTD.3
LED1 var PORTB.6
LED2 var PORTB.7
If (sw1=0) Then 'sw1 is pressed, pull-up resistor
High LED1
Pause 100
End If
If (sw2=1) Then 'sw2 is pressed, pull-down resistor
High LED2
Pause 100
End If
Goto loop
End
No comments:
Post a Comment