PDA

View Full Version : multiple switches~one ADC



naga
- 16th August 2009, 18:16
Planning to connect multiple switches into single port.one ADC pin to detect voltage level when each switch for each state is pressed and each switch is associated with a voltage.connecting 8 switches with 1 adc.. Looking for some coding tips.. Thanx.

mackrackit
- 16th August 2009, 19:57
That should work. After you read the ADC have a gosub with some if/thens or case statements to do ??? at a certain ADC reading. Then go back and read the ADC again.

You may want the code to loop around reading the ADC and have the value with out a switch being pressed do nothing.

Acetronics2
- 16th August 2009, 20:13
Hi, Dave

This also works very well with POT command ( no ADC required ) ... a well known STAMP Application.

EPE had also published an original design around a 555 ... key pressing generates a Pulse depending on the key pressed ...
Interrupt use to detect keystroke ... nice !

Alain

mackrackit
- 16th August 2009, 20:37
Never cared much for the POT command. Seems not as stable. But that is me.

I am going to look at your other option. Sounds interesting.
Thanks!

Acetronics2
- 17th August 2009, 08:34
Never cared much for the POT command. Seems not as stable. But that is me.

!

Hi, Dave

I generally do not use POT ... but for 10-12 keys or less result is reliable with a reasonnable range of values for each key.

soooo, I do it simple ( for once !!! )

I'll try dig out the EPE Keybd ... it's somewhere in my HDD !!!

Alain

naga
- 17th August 2009, 12:09
Hi All, tried this code, its working but is there any other reliable way....

adval var byte ' Create adval to store result

TRISA = %11111111 ' Set PORTA to all input
ADCON1 = %00000010 ' Set PORTA analog
Pause 500 ' Wait .5 second
loop: ADCIN 0, adval ' Read channel 0 to adval
Lcdout $fe, 1 ' Clear LCD
Lcdout "Value: ", DEC adval ' Display the decimal value
Select case adval
CASE is <51
high porte.0
cASE 102
high porte.1
CASE 153
high portd.2
CASE 204
high portd.3
CASE is >225
low porte.0 : low porte.1 : low portd.2 : low portd.3
Pause 150
END SELECT
pause 100
Goto loop
End
* this below line is not accepting :ERROR
case >1 to <53
'do some thing
case >53 to < 102
'do some thing..
So On.....

mackrackit
- 17th August 2009, 12:31
That way looks good to me.

This part ...


* this below line is not accepting :ERROR
case >1 to <53
'do some thing
case >53 to < 102
'do some thing..
So On.....

Work something like this into your code, maybe before the CASE part. If the conditions are not met then the code will run to the CASE select part.


IF (ADVAL > 1) AND (ADVAL < 53) THEN ....

naga
- 17th August 2009, 13:53
Thanks mackrackit, IF (ADVAL > 1) AND (ADVAL < 53) THEN ... this code is working..
Since we using ADCIN for switches, should be able to TOGGLE the ports.. I have tried with WHILE..WEND also.. no luck.. Could you pls ..Thanks again.

mackrackit
- 17th August 2009, 14:18
Can we see your WHILE/WEND code? Or better yet your new and updated code.
But something like this should work


ADCIN 0, adval
WHILE (ADVAL > 1) AND (ADVAL < 53)
do something
ADCIN 0, adval
WEND
SELECT CASE ....

naga
- 17th August 2009, 15:29
Hi, The debounce was the problem..Following both codes are working after adding some delay.. I didnt use CASE.. Pls suggest if it needs to be fine tuned..Thanx..
if (ADVAL > 200) AND (ADVAL < 256) THEN
toggle porte.0
pause 150
endif
-----------------------------------------------
ADCIN 0, adval
WHILE (ADVAL = 255) 'AND (ADVAL < 256)
toggle porte.0
pause 150
ADCIN 0, adval
WEND

mackrackit
- 17th August 2009, 15:43
If it is all working then I would say it is good.
not knowing yhe whole project it is hard to say if it needs fine tuned, but what you have looks like a good solution.

naga
- 17th August 2009, 16:07
If it is all working then I would say it is good.
not knowing yhe whole project it is hard to say if it needs fine tuned, but what you have looks like a good solution.
Thanks..Shall post the complete project shortly..

peterdeco1
- 17th August 2009, 19:07
Hi Naga. I did something like this before. I edited the code & schematic to show only the relevent info. The only thing different is if you press the bottom 2 switches at the same time, it will do a 5th function.

ANSEL = 0 'all inputs digital ADC command converts to analog
CMCON = 7 'comparators off
DEFINE OSCCAL_1K 1 ' Set OSCCAL for 1K device
@ device pic12F675, INTRC_OSC_NOCLKOUT, wdt_on, BOD_ON, pwrt_on, mclr_off, protect_on
BUT VAR BYTE 'MESSAGE BUTTONS VARIABLE
BUT2 VAR BYTE 'VARIABLE TO COMPARE BUT READINGS TO PREVENT WRONG ADC READINGS

Pause 500 'SETTLE DOWN

START:
NAP 0 'REDUCE STANDBY CURRENT
ADCIN 3,BUT
IF BUT < 100 Then TABLE 'BUTTON PUSHED
GoTo START

TABLE:
ADCIN 3,BUT
Pause 100
ADCIN 3,BUT2
IF BUT <> BUT2 Then TABLE 'RE-READ THE ADC

IF BUT < 91 AND BUT > 71 Then (DO 1ST THING)
IF BUT < 50 AND BUT > 40 Then (DO 2ND THING)
IF BUT < 15 AND BUT > 8 Then (DO 3RD THING)
IF BUT < 8 AND BUT > 4 Then (DO 4TH THING)
IF BUT < 5 AND BUT > 2 Then (DO 5TH THING)
GOTO START

naga
- 18th August 2009, 07:42
@ Peterdeco1,Thank you very much .. Very nice..Working perfectly.. Thanks to all of you..
Other problem is..
When using MeLabs default (PM) Assembler compiles OK:
@ device pic12F675, INTRC_OSC_NOCLKOUT, wdt_on, BOD_ON, pwrt_on, mclr_off, protect_on

When using Microchip's (MPASM) Assembler:

@ __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _PWRTE_ON & _MCLRE_OFF & _BODEN_ON
3 errors,image attached..

mackrackit
- 18th August 2009, 10:20
1 error and two warnings.
Not sure about the warnings but they are just warnings.
The error looks like the Inc file has not been commented out.
http://www.picbasic.co.uk/forum/showthread.php?t=543