I'm making the switch debouncing thing to work, but however, I end up the whole LED blinking instead of one touch switch, LED ON, and then another one, LED OFF.
Here's the code I've squeezed out so far. Wow, ASM is really tough!
The thing that is missing is some comments. We have no idea if A3 is high or low when a key is pressed or if C0 low makes the LED light.
However, the best way to detect a key press is to wait for it to be released and then wait for it to be pressed. I've assumed that the key makes A3 <edit>low</edit> and C0 high is LED on.
Code:
WaitNoKey btfs[COLOR="Red"]s[/COLOR] PORTA,3 ;key released?
goto WaitNoKey ;no,so wait
call Delay ;short delay for debounce
WaitKey btfs[COLOR="red"]c[/COLOR] PORTA,3 ;key pressed?
goto WaitKey ;Edit, changed this. no,so wait
call Delay ;short delay for debounce
;now toggle LED
btfss PORTC,0 ;is LED on
goto TurnOn ;no, so go turn it on
bcf PORTC,0 ;yes, so turn it off
goto DoneLED
TurnOn bsf PORTC,0
DoneLED goto WaitNoKey ;go do it all again
Note, the delay should be very short - around 10mS.
The code should have worked but with the LED changing when the key is released. I've edited it now so it is active low.
A little tip when writing code, try to keep the different sections separate. If you look at the code above it is in two sections, key reading and LED toggling. By replacing the key section with a 1 second delay will be able to tell if the LED section is working before you go onto debugging the key code.
The code should have worked but with the LED changing when the key is released. I've edited it now so it is active low.
A little tip when writing code, try to keep the different sections separate. If you look at the code above it is in two sections, key reading and LED toggling. By replacing the key section with a 1 second delay will be able to tell if the LED section is working before you go onto debugging the key code.
But, however when I press the switch, the LED turns on only. Pressing back the switch, it wouldn't budge, and it remains on.
I got the switch debouncing part already, but I love to make those toggle switch like the one in many new electrical appliances where you can turn on the thing and off the thing using the same button.
Yeah I mean the code you have posted. Like any R-S flip-flop (or bistable multivibrator), this LED only switches on once the button is pressed. Pressing the same button will not turn it off.
What I mean is, turning the LED on and off using the same button. I kinda figured that one for hours, but couldn't get it.
Solving a problem your own way is important, so I don't mean to undermine anything, but solving a debounce in software has always seems silly to me. Why not just throw a resistor and capacitor in parallel to ground? Chose values such as ~1k and .1uF, or whatever suits your cause.
Seems easier to me, but if solving it with software is the point, then nevermind.
If you want hardware solution in a small 4 pin package look at the MAX6816/MAX6817/MAX6818...
Use a 1k & 10uf for a 10mS delay
1K * 10uf = 10mS
1000 * .000010 = .010 seconds
Commercial designs do, saving space and components, simplifying the PCB.
In line with that, most of the "software" functions could be implemented in hardware. That would be silly!
By the way, once you know what debouncing means, implementing it in software is conceptually simple. I had to use up to 30 ms with bad quality pushbuttons.
There is an interesting article by Jack Gansle (I think that is the right name) and a lot of routines by Scot Datalo where all that is addressed in detail. Read them. Worth your time.
We do all the time. We push port pins through buffers and static protection, doing the debounce and keyboard scanning in software for 64+ button membrane switches.
It is worth noting that computer keyboards are scanned and debounced in software as well... it would be VERY prohibitive to do it all in hardware.