| MPASM Tutorial - Using Switches |
|
|
| Tuesday, 14 April 2009 12:24 | |
|
User interaction, a key element to micro controllers in projects; and there probably is not any more simple than the humble switch.
A switch in series with a resistor can either pull a voltage high or low depending on how it is configured. Here's an animated example;
Keep in mind that Vdd/Vss are not connected in the above image
And of course, the code that makes the above animation work in real life; LIST p=16F628 ;define which chip is in use
The main program loop is the following code; Loop btfss SWPORT, SW1 ;poll the switch to check state
It continually "polls" "Switch1" which is defined as PORTA.7. When this goes "low", the program will call a debounce routine called "SWDel". Notice that this delay is defined earlier in the program; SWDel Set Del50 ;set the de-bounce delay (has to use 'Set' and not 'Equ')
This means that when the compiler sees "SWDel", it will replace it with Del50. This can be modified to call any of the other delays such as "Del1","Del100", "Del250" (the number of mS to delay for).
I hear you asking - why wait after a switch has been pressed before you do something? Well to put it simply, when a switch physically actions, it will bounce on the contacts for a moment of time. A micro controller will detect this and without safety measures such as a "debounce" routine as above, well you might find your program doing some strange things! |
|
| Last Updated ( Tuesday, 14 April 2009 13:24 ) |
Whos Online
- Jon Chandler
- MrDEB
Forum Activity
LCD / 18F1320 - mrdeb Friday, 19 March 2010 07:19 - [0 replies]
A 'throw-away" PIC board - mrdeb Thursday, 18 March 2010 06:19 - [32 replies]
Credit Where Credit's Due - jon chandler Tuesday, 16 March 2010 12:01 - [9 replies]
Marching LEDs - mrdeb Saturday, 13 March 2010 22:54 - [7 replies]
USB 8 Channel Servo Controller - andyo Saturday, 13 March 2010 01:19 - [2 replies]
Dedicated Servo Controller - graham Friday, 12 March 2010 17:58 - [0 replies]
Must have....delta temperature glowies! - graham Friday, 12 March 2010 17:31 - [3 replies]
Recent Comments
- 2010-03-17 22:58:18 Jon C...
Graham, Thanks for fixing the spacing on the array. This is how i...
- 2010-03-17 21:14:34 ASDne...
Thanks Graham, It is a problem with Flowcode because the demo wa...
- 2010-03-17 19:50:32 Graha...
Looks like you've pretty much solved every minor issue that was enc...
- 2010-03-17 11:42:03 Jon C...
The low-cost servo does have one other feature. The origina...
- 2010-03-17 10:56:44 Graha...
I am not familiar with flowcode, though did you try powering the bo...
- 2010-03-15 21:47:55 ASDne...
Hello there, I am still trying to get the LCD to work but I am pro...
- 2010-03-15 13:16:38 Graha...
Hi Hop, I've finally got some time this weekend to delve into the ...
- 2010-03-13 16:36:21 andyo
Very nice - 900,000 packets and 0 errors - can't ask for better tha...




How did you come about using the config code "__config 0x3D18"? I thought you had to define each setting. For example;
__CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _FCMEN_OFF & _IESO_OFF