views:

298

answers:

3

I have what I hope is a simple question pertaining to the Atmel AVR microcontrollers. So I want to use the ATTiny85's Analog Comparator to determine if a signal is above or below a threshold. This signal is normally "floating" and pulled toward ground when "active" (i.e. it's an active low - open collector signal). If I enable the pullup on the input pin (which is also the comparator input) by doing:

DDRB  = 0x00; // DDRB.1  = 0 = input 
PORTB = 0xFF; // PORTB.1 = 1 = internal pullup enabled

If i use the analog comparator and select PORTB.1 as AIN1 will the internal pullup be applied to my input signal? I'm hoping someone has personal experience to verify this behavior. Hope this question isn't too 'hardware-oriented' for stack-overflow. Thanks!

EDIT In response to some of the comments below to the effect of "why not treat it digitially", it's because my "signal" is generated by a sensor (namely an optical IR sensor). I can't be assured that it will pull down "hard enough" to be interpretted as a zero digitally, so I'm going to use the internal 2.56V reference as my switching threshold. I'm just trying to pick up the "IR event," so this seemed the simplest way to me.

Also, by using the analog comparator, I can detect the event "asynchronously" using an ISR. Granted some of the pins can invoke an interrupt on a digital edge, but the one don't want to rely on the response to dip below V_IL.

+2  A: 

I can't say for a fact that it will work (I've never done the case you're describing), but electronically speaking your reasoning is sound. When the switch is open, AIN1 will see VCC through the internal pullup, and when the switch is closed, they will both see 0V.

VCC
|
100k(?)
|
AIN1
|
 /
|
GND

Without actually reading the datasheet for that processor, there's a chance that you won't be able to use an internal pull-up on a pin configured for analog input; in that case, you might be able to use another pin on the chip, configured as a digital input, for the pullup:

VCC
|
100k(?)
|
Other---AIN1
        |
        /
        |
        GND

But if the voltage on AIN1 is constantly in the "neither logic high nor logic low" range, that might throw your current consumption fairly high.

Mark Rushakoff
Unless theres something else going on here use a digital input pin with a pull up for this, there no reason to use the comparator.With a pull up the power usage of an open collector signal shouldn't be bad. When the pin is floating it should be very near 0.
Mark
edited my question in light of this response, thanks!
vicatcu
@Mark edited my question in light of this response, thanks!
vicatcu
@vicatu Be careful with this approach, without hysteresis in the level detection you can run into a situation where the IR diode pulls down to roughly your reference voltage and "wobbles" there causing spurious interrupts to be created.
Mark
+2  A: 

Normally you would use the digital input with the pull-up, since you seem to have a digital input signal.

Is there a reason to use the analog input?

starblue
+1 for asking the obvious question "why does this need an analogue input when there are only two states of interest?". Sounds like a candidate for a digital input to me.
Clifford
edited my question in light of this response, thanks!
vicatcu
@Michael edited my question in light of this response, thanks!
vicatcu
+4  A: 

From the datasheet figure 10-5 you can see that the input to the analog stage is affected by the pull-up.

Thus you can use the internal pull-up together with the comparator. As mentioned by the datasheet, you might want to disable the digital input stage of that port to reduce power consumption - of course only if you don't use it for digital input.

Note: Floating pins can cause huge current consumption for the digital input stage. Try to avoid them.

ziggystar
+1 for directing him to the datasheet; always the first place to look.
Clifford
Yea, that's what I thought, thanks!
vicatcu
Unfortunately, I accepted this answer before trying it out. It turns out (despite inference from the datasheet) that the internal pullup *must* be disabled in order to use a pin as an input to the analog comparator.
vicatcu
What happens when you enable the pull-op when doing ADC? Is the ADC disabled or is it that you're measuring something different than you expected?
ziggystar
I've just read through some forum threads and it seems that it should work. http://www.mikrocontroller.net/topic/89658 (it's german)
ziggystar