| 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
MARCHING ledS - mrdeb Thursday, 11 March 2010 10:48 - [2 replies]
REALISTIC LED candle flicker - mrdeb Wednesday, 10 March 2010 05:48 - [0 replies]
the LCD demo hello world - mrdeb Wednesday, 10 March 2010 05:41 - [29 replies]
USB problem - jon chandler Tuesday, 09 March 2010 11:31 - [3 replies]
Servo Module - andyo Tuesday, 09 March 2010 03:39 - [3 replies]
pic to ps2 communication - roshan Friday, 05 March 2010 01:30 - [7 replies]
Way Off Topic: Worldwide Ignite Week - jon chandler Wednesday, 03 March 2010 20:33 - [4 replies]
Recent Comments
- 2010-03-11 21:03:32 Jon C...
If this wasn't a class project that had to be done on a PIC16F690 u...
- 2010-03-11 15:30:06 uuu
I am going to send two data from ADC to USART(async mode) to serial...
- 2010-03-05 13:24:23 mrpse...
I was thinking of battery with powered routers or powered for every...
- 2010-03-05 12:17:40 andyo
50 rooms - wow. The XBee modules are very easy to use but I'm not ...
- 2010-03-05 11:46:37 mrpse...
Thanks AndyO. What I have to do is design a device to monitor temp...
- 2010-03-05 03:08:58 andyo
The circuit used for the remote sensor can be found here: [url]http...
- 2010-03-04 07:19:06 renno
plz i need help do u know from where i could download it or how cou...
- 2010-03-04 07:16:05 renno
hello everyone i'm trying to finish my final project and i want to ...




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