Page 1 of 1

Reading Multi switches on only one ADC pin

Posted: Wed Jun 05, 2013 7:38 am
by bitfogav
I know this isn't anything new but its worth sharing and someone here might like to use it in there next project..

For a new project ive been working on ive used an ADC pin of a PIC to read several switches, this limits the amount of pins needed by the PIC to control several switches..

Basically by using several resistors inline between the switches we can read the voltage on the line using an ADC pin and then we can evaluate the switch that was pressed..

We can have a function (Firewing code) to read the ADC pin and we can return which key was pressed and use this in our prog:

Code: Select all

function Keypress() as byte
   dim voltage as ushort = Adc.Read(A0) ' get ADC voltage(value)
   Keypress = 0                         ' set Keypress to default value
   select voltage
      case < 50  : Keypress = 1  ' select
      case < 200 : Keypress = 2  ' fire
      case < 400 : Keypress = 3  ' right
      case < 600 : Keypress = 4  ' up
      case < 800 : Keypress = 5  ' down
      case < 1000 : Keypress = 6 ' left
   end select
end function

Sub Main()
   While True
      Dim Key as Byte = Keypress
      Console.Write("Key = ",str(Key),13,10)
   End While
End Sub
adcpinswitches.jpg
Schematics:
adcpinswitches.jpg (21.89 KiB) Viewed 13886 times
*Edited - added more code to show keypress*

Re: Reading Multi switches on only one ADC pin

Posted: Wed Jun 05, 2013 10:40 pm
by brad
I remember reading up on this on digital-diy.com a while back. It's a great way to get multiple buttons on one port pin with minimal hassel.

Thanks for sharing the firewing code - Actually I think I'll open up a new forum specifically for firewing because I think eventually I will head down that path.

Re: Reading Multi switches on only one ADC pin

Posted: Thu Jun 06, 2013 2:47 am
by bitfogav
brad wrote:Thanks for sharing the firewing code - Actually I think I'll open up a new forum specifically for firewing because I think eventually I will head down that path.
Thats not a bad idea actually Brad.

Firewing at this time is limited to only one pic 24HJ128, I like the fact that its so easy to port code from Swordfish to Firewing and vice versa, though im not going to give up on Swordfish has I really like the 18F pic's.. :)

Re: Reading Multi switches on only one ADC pin

Posted: Thu Jun 06, 2013 9:52 pm
by brad
Good to hear that you're still sticking with 18F's (They are still my favourite!)