ATTiny85 a port for ADC and a port for the same signal

Go To Last Post
11 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The ADC software I previously wrote works fine for the project but now I need to be able to count the pulses of the same signal going into the ADC.

I need to count the positive pulses during a given time window as defined in software to determine if the frequency of the ADC input has shifted.

I have an extra port pin PORTB4 and was wondering if I could simply couple the ADC input at PORTB3 to PORTB4 with, say, a 1K resistor, and count the 1's I see.

I can't see any way to do a ADC read over and over because the ADC input rarely goes to ground but it does have a peak voltage that flutters up and down slightly.

I assume I could bias the extra port input in such a way that anything over a certain voltage, the ADC input never goes all the way to 5V, equals a 1.

Any comments or suggestions would be appreciated.

BADBAUD

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

I can't see any way to do a ADC read over and over because the ADC input rarely goes to ground but it does have a peak voltage that flutters up and down slightly.

Can you draw a picture of the kind of wave/signal you are expecting to receive. If you are making contiguous ADC samples then I'd have thought (assuming you meet Nyquist requirements) you could profile the picture of the wave without needing to count highs/lows separately - you already know when it's high or low by the ADC readings.

Something like this:


The red lines are your ADC readings.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The signal is 4 kHz current pulses produced by the load and read by a MAX current sense IC. .02 Om in series with the load.

BADBAUD

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK so if it's something like the picture above would you not agree that in that picture the signal is "high" when the Y value is 5 or more and "low" below this? Why would something like this not be applicable to your code?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I see your point. The trick is to determine what is low and what is high as the load never turns off completely. Are you also saying that I can't couple a analog input to a digital input and do ADC analog on one and do a digital count via the, second, digital input port? I read the digital function of the ADC input must be turned off. Would this "coupling" be unacceptable also?

BADBAUD

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can but the limits on a digital pin's view of what is "high" and "low" are fixed. low is up to Vcc*0.3 and high is above Vcc*0.6. The bit ni the middle is therefore undefined. It also wastes a pin needlessly when you can determine a more accurate high/low threshold using ADC readings. In fact if you were to have a graphic display of the signal you could almost do the same sort of thing as you do on a 'scope and wind a "threshold" line up and down to exactly position where to the "trigger" point for the high/low transition was. (depending on the signal you may want a little hysteresis so that a signal around the transition doesn't keep switching 010101010)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I see no problem with just connecting an ADC pin and a digital input pin together. Except that it uses up a whole pin.

You could simply change the functionality of the ADC pin from analog to digital as required.

If you only want to determine the frequency of your analog signal, you just make note of the analog value passing a 'zero' threshold. The exact switching level is rarely critical. If it is, use the Analog Comparator function.

Personally, I would just analyse the ADC values passing 'zero'. Count several cycles and take the running average. This will take out the granularity where you do not catch the exact 'zero'.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Personally, I would just analyse the ADC values passing 'zero'.

My point entirely.

(clearly the DC offset of the +ve only ADC means 'zero' is somewhere else - perhaps at the "half range" point? 5 on the 0..10 scale above)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, Cliff. I was just duplicating your excellent advice.

I was unsure as to say 'zero' point, 'midpoint' or whatever. With a repetitive signal, all that matters is that you catch some point in the cycle. It need not be the zero-crossing but choosing zero means you can measure smaller amplitudes. You might be more susceptible to noise, but software can take out unexpected transitions.

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

That to me is one of the advantages of using the ADC readings themselves - you can vary (tune?) that 'zero' point to whatever level you choose.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Writing in assembly only. My problem is I can't visualize the process needed to "tune" the result. I can read the ADC and store it in SRAM, read it again and compare with the previous, if higher overwrite the SRAM with the new value, if lower save that value in a separate SRAM location(s). If the next read is higher then the highest stored overwrite it, if it is lower then the low stored overwrite the lower SRAM. I could do this all day long and still not determine when the signal is actually low or high. Should I set a counter and count how many lows or highs before deciding I have a high or low? Like, maybe, 10 writes in a row to the low SRAM location indicates I am at a low point in the signal? Or ten compares with the lower, because it wasn't higher then the high, indicates a low signal?

BADBAUD