View previous topic :: View next topic |
Author |
Message |
kishen
Joined: 16 Apr 2015 Posts: 8
|
External push button |
Posted: Thu Apr 16, 2015 1:32 am |
|
|
hi, i have read the code where push button A4 was used to turn on LED, but i am not able to switch on led through external push button, lets say for example if a push button is connected to pin F0 for pic18f8722. Please help. |
|
|
Sam_40
Joined: 07 Jan 2015 Posts: 127
|
|
Posted: Thu Apr 16, 2015 6:54 am |
|
|
kishen,
I did not completely understand your question! This code should get you start in the right direction. You have to change the input and output to your project!
Code: | #include <18f8722.h>
/* you have to define your fuses here! */
#use delay(clock=8000000)
void main()
{
while(true)
{
if(input(PIN_A0 )==0) /* Check the input */
{
output_high(PIN_B1); /* Fire the LED */
}
if(input(PIN_A0 )==1) /* Check the input */
{
output_low(PIN_B1); /* Turn OFF the LED */
}
}
} |
|
|
|
kishen
Joined: 16 Apr 2015 Posts: 8
|
RE: |
Posted: Thu Apr 16, 2015 9:14 pm |
|
|
I have tried out this code the light turns on and off automatically and flicks. is this due to key debouncing or we have to add set_tris_f() to tell 18f8722 that f0 is input. I have tried putting an LDR in place of the push button but nothing seems to happen. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 16, 2015 9:48 pm |
|
|
Your push-button circuit should look like this:
Code: | +5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC -----------------o o------
pin |
F0 --- GND
-
|
If you are running your PIC at +3.3v, then change the pullup resistor
voltage to +3.3v also. |
|
|
Sam_40
Joined: 07 Jan 2015 Posts: 127
|
|
Posted: Fri Apr 17, 2015 7:38 pm |
|
|
Hello PCM programmer,
I have question regarding denounce. It is related to the kishen question. I did work on this for a good amount of time in the past, I use a oscilloscope and different codes, I even implemented the debounce codes. However I do not see the difference? I am using PIC18F2685 and PIC16F73. I used your circuit with 10K and I added .1uF ceramic cap to GND as suggested to me by temtronic in one of my projects.
A simple circuit like yours or the one below seems to be sufficient without the extra lines of code. Would you please elaborate on the subject so it will be clear.
Code: | +5v
|
<
> 10K
< ___ Switch
To | _|_|_
PIC ------------------o o------
pin | |
X | |
___ |
___ .1uF |
| |
| |
_____ _____
___ GND ___ GND
_ _
|
By the way I like your ascii art
Thanks, |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Apr 18, 2015 4:30 pm |
|
|
I don't want to go do this assignment right now. I'd have to get at least 5
different types of switches, test them with, and without the capacitor, and
capture the waveforms with a DSO scope, and write up a report.
It just occurred to me that I have done this in the past (in the late 90's ?).
I may have written it up in a notebook. If I can find the notebook.... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Sun Apr 19, 2015 3:11 am |
|
|
Code: |
+5v
|
<
> 4.7K
< ___ Switch
To | _|_|_
PIC ----------\/\/\/-o o------
pin | 220R |
F0 | |
___ |
___ |
| |
| |
--- GND --- GND
- -
|
Problem is that just putting the capacitor directly across the switch hardly slows the falling edge at all. Dead short (nearly), so edge speed is almost as fast as without the capacitor. The rising edge is then slowed a little, but unless the capacitor is made a lot larger, the effect on bounce is very small.
Unfortunately also, it introduces quite high currents through the switch contacts.
Using the two resistor approach improves things noticeably. You can use a larger capacitor without damaging the switch, and both edges now have some time constant (still 20* faster for the 'on' edge than the 'off'), but a lot better.
A better circuit is:
Code: |
+5v
|
---- |
| <
___ > 4K7R
470nF___ <
| |
---- o |-
| |
o |-
|
|
|______
|
<
> 22KR
<
|
___
_
|
Which means the switch now pulls up, rather than down. You can use the same circuit with a pull down approach, but you have to be careful of what voltage is considered a '0'. Typically only 0.6v, while (for 5v non Schmitt inputs), a '1', only requires 2.4v. To get the signal 'low enough' again means very small resistors involved on the pull down side, and reduces the time constant involved....
Realistically, a 'soft debounce' is much better. For your single button, a delay and re-test. For a keypad, using a timer interrupt, and waiting till the key remains the same for two successive interrupts.
You need to be looking for between 10mSec and perhaps 40mSec total debounce time (slower than 40mSec has people thinking the key is slow responding), faster than 10mSec makes it very likely the key will be seen multiple times.
It does depend massively on the keys themselves, and you must remember that these will tend to get worse with age..... |
|
|
Sam_40
Joined: 07 Jan 2015 Posts: 127
|
|
Posted: Sun Apr 19, 2015 4:05 pm |
|
|
PCM programmer and Ttelmah,
Thanks for the inputs. I do agree with you Ttelmah, a simple circuit like the one provided by PCM programmer and a simple delay code before you read the switch input as valid "software debounce" is the most realistic way of doing this!
I will however try both of the circuit that you provided to see how the scope read them. One more question you suggested to use a larger cap since there is a dump resister on the first circuit. What is the value?
Thanks, |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19499
|
|
Posted: Mon Apr 20, 2015 12:38 am |
|
|
Calculate the time constant....
This is down to:
1) The supply voltage on your circuit.
2) The actual high/low voltages of the pin you are using. Preferably a Schmitt input.
The 'key' point is that most switches will bounce for up to perhaps 10 to 20mSec. Generally though since this is 'bounce', a time constant around 10mSec will work OK (since though the whole sequence - make/break/make etc., takes up to 20mSec, the period between individual edges is much less).
Look here:
<http://www.ganssle.com/debouncing-pt2.htm>
The PIC input is the gate shown, and this gives the maths to calculate resistors and capacitor for different time constants.
The circuit there is actually about the best simple one that can be done. |
|
|
|