Author Topic: Using an ADC input  (Read 6585 times)

David

Using an ADC input
« on: July 17, 2007, 04:30:30 PM »
This is one of those "you can order eggs" or "you can order toast" kind of things.  What I'd like to do is order both eggs AND toast.

I'm scoping out a project on the PIC 16F88 that's documented in my LDR and PWM thread.  I plan to use the ADC facility to read the displacement of a volume pot.  I've also found code examples that read a pushbutton from the ADC.  My question is, is it possible to use the ADC to read a pot AND keep track of some pushbuttons (who knows, I might put one in that would switch the pot output to a voltage-controlled wah!)?  I've seen examples of a pot feeding the ADC and I've seen examples of reading pushbuttons.  No examples of both, though -- and I have LOOKED.

I want to use the hardware PWM facility to drive my output LEDs because it's my understanding that this can run more or less as a background task.  OK, good.  I hope that means I don't have to use interrupts to work with it.

I've got about 1.5 million other questions, but I don't even kinow how to ask them right now.  For now, I'll setlle for asking if anyone can point me to an example of using ADC to process pushbuttons and a pot -- AND how you determine the meaning of the ADC output so you can know which input to process and what its value was?

The Tone God

Re: Using an ADC input
« Reply #1 on: July 17, 2007, 11:13:48 PM »
Can it be done, well kinda. The problem is how would you have the uC identify the open and closed switch values from the pot's value. You would have to lose some range of the pot to dedicate to the switch values and configure the switch to give those values.

I have done multiple switches on a single pin using A/D. Another option is to use a spare output pin to control a multiplexer of some sort on the A/D pin input.

Andrew

Dave_B

Re: Using an ADC input
« Reply #2 on: July 18, 2007, 12:47:05 AM »
There's probably an easier way, but you could do something like this. 

The switch is a dpdt.  With the switch "on", it shorts the top resistor and the ADC will receive values between 2.5v and 5v.  In other words, the top bit on the ADC should be on when the switch is in this position indicating the switch is closed.  Flip the switch the other way and the bottom resistor is shorted.  The ADC then receives values between 0v and 2.5.  This would set the top bit to a zero, indicating the switch is open.



Another way would be use a summing op-amp.  Are you sure you don't have a spare I/O pin?   ;D
Help build our Wiki!

David

Re: Using an ADC input
« Reply #3 on: July 18, 2007, 08:17:45 AM »
For once, pins are not the issue.   ::)

What concerns me here is:

1)  The ADC might need an interrupt
 
2)  If I do software PWM, I need an interrupt.  I haven't figured out if hardware PWM requires an interrupt.  I had hoped maybe there was some way it could run more or less as a background task since it will be doing the bulk of the work -- I'll want the LED/LDR pairs busy all the time controlling the volume level.

3)  I have to monitor the pushbuttons somehow.  Polling them is easy, but I'm not sure that's the best use of the main routine's time.  I'm still trying to suss this all out.
      :icon_idea:  IDEA:  Maybe I should make the PWM processing the mainline of the program.  Then I could dedicate one timer to the ADC, and another to the pushbutton polling.

4)  I have example code that uses interrupts to read and write EEPROM.  Yup - another interrupt.  I think the '88 has three timers so  I know I'm all right there, but I have no idea what's involved in task information mechanics when you have multiple interrupts.

Land sakes alive!  All this just to make a smart volume pedal!   :icon_rolleyes:

Dave_B

Re: Using an ADC input
« Reply #4 on: July 18, 2007, 10:24:04 AM »
There's a heirarchy for interupts, but I don't know that it matters for what you're trying to do.  In my mind, most of the things you're talking about could be done as part of the main loop.  That loop might be executed several thousand times a second, so don't get too hung up on using interupts for everything, especially switches.  You should be debouncing them anyway.

Is it necessary to have the ADC use up a timer?  I thought they had a free-running mode with a prescaler, where you could poll the output register as part of your main loop, but I may be remembering that incorrectly. 
Help build our Wiki!

David

Re: Using an ADC input
« Reply #5 on: July 18, 2007, 12:22:25 PM »
I found out that the PWM facility uses Timer 2 as its processing clock.  Boom!  One timer gone.  However, that process is now free-running until the PWM duty cycle gets changed.

danngreen

Re: Using an ADC input
« Reply #6 on: July 18, 2007, 12:41:50 PM »
3)  I have to monitor the pushbuttons somehow.  Polling them is easy, but I'm not sure that's the best use of the main routine's time.  I'm still trying to suss this all out.
      :icon_idea:  IDEA:  Maybe I should make the PWM processing the mainline of the program.  Then I could dedicate one timer to the ADC, and another to the pushbutton polling.

I'm coming from an AVR background, but have done a little in PICs, so correct me if I'm missing a difference between the two...

If you run each pushbutton directly into an I/O pin, you can poll them once per main loop just by reading that pin's value. It will only eat up a cycle or two, and like Dave_B says you'll need code to debounce them (one way is to check if the switch's value has remained the same for an arbitrary number of loops) Of course, total processing time depends on what you want to DO with the pushbuttons (e.g. writing to EEPROM, etc.. )

Why do you want to use interrupts/timer for the ADC? from how im understanding it, you can probably just read the ADC once in the loop, and then update the PWM duty cycle if the ADC value has changed. then restart the loop (read switches, write to eeprom if needed, read ADC, update PWM if needed, repeat, repeat, repeat)

David

Re: Using an ADC input
« Reply #7 on: July 18, 2007, 01:03:18 PM »
Why do you want to use interrupts/timer for the ADC? from how im understanding it, you can probably just read the ADC once in the loop, and then update the PWM duty cycle if the ADC value has changed. then restart the loop (read switches, write to eeprom if needed, read ADC, update PWM if needed, repeat, repeat, repeat)

I don't want to use any interrupts I don't have to.  I've just been trying to figure out how all this is going to fit together so I can start assembling my "design".  Forewarned is forearmed.

The Tone God

Re: Using an ADC input
« Reply #8 on: July 18, 2007, 03:02:44 PM »
Is your current design using all your I/O pins ?

Andrew

David

Re: Using an ADC input
« Reply #9 on: July 18, 2007, 03:12:21 PM »
Is your current design using all your I/O pins ?

Andrew

Nope!   :icon_mrgreen:   ;D

The way I figure it, I'm using one ADC pin to read the pot, one (perhaps two) CCP output pin(s) to output the PWM "data" and one for my debounced "threshold" pushbutton.  If I add any other pushbuttons (for example, to switch between volume and wah functionality -- if I do this, I'll probably need a second PWM output for the additional voltage-controlled function), I'll have to factor them in, too.  Oh, and I'm also going to light an LED when the ADC reading matches the threshold reading I've stored in EEPROM.

The Tone God

Re: Using an ADC input
« Reply #10 on: July 18, 2007, 03:33:53 PM »
I'm just thinking that if you have a spare output you can use a multiplexer on the A/D input to switch between the button and pot but if you have a spare pin you might as well use it for the button alone.

Andrew

Dave_B

Re: Using an ADC input
« Reply #11 on: July 18, 2007, 03:42:06 PM »
The F88 only has one PWM, right?
Help build our Wiki!

David

Re: Using an ADC input
« Reply #12 on: July 18, 2007, 04:25:04 PM »
The F88 only has one PWM, right?

Uhhh, yeah.   :icon_redface:  Oh, well...   :icon_cry:

The Tone God

Re: Using an ADC input
« Reply #13 on: July 18, 2007, 04:44:34 PM »
Are you planning on controlling this external like with a rocker pedal ? Are trying to reduce the number of conductors in the cable ?

Andrew

R.G.

Re: Using an ADC input
« Reply #14 on: July 21, 2007, 07:27:49 PM »
Quote
Are you planning on controlling this external like with a rocker pedal ? Are trying to reduce the number of conductors in the cable ?
That was my take.

It's far simpler to put a PIC in the remote pedal, grab all the data, then squirt it across a serial line at a megabit or two and unfold it on the other end with a second PIC.

When life is complicated, sometimes a sledgehammer is an acceptable solution. They're cheap, take two.
R.G.

Quick IQ Test: If anyone in a governmental position suspected that YOU had top-secret information on YOUR computer, how many minutes would you remain outside a jail cell?

RaceDriver205

Re: Using an ADC input
« Reply #15 on: July 22, 2007, 03:13:48 AM »
Quote
It's far simpler to put a PIC in the remote pedal, grab all the data, then squirt it across a serial line at a megabit or two and unfold it on the other end with a second PIC.
Have you tried this? I ask because I need to do a similar thing, but people inform me that the speed should be kept much lower than that to get reliable transmission down a cable.

danngreen

Re: Using an ADC input
« Reply #16 on: July 22, 2007, 10:22:09 PM »
It's far simpler to put a PIC in the remote pedal, grab all the data, then squirt it across a serial line at a megabit or two and unfold it on the other end with a second PIC.

Sounds like the most versatile thing to do. With three conductors for the cable, you could send V+, GND, and data to avoid having to power the remote pedal. The 16F88 has a UART, right? so this should be a snap.

David

Re: Using an ADC input
« Reply #17 on: July 23, 2007, 08:28:43 AM »
Ummm...  actually, I had seen this as a self-contained volume pedal.  The only output I had envisioned was audio.  The only inputs would be power and audio.