ADC with an ATTINY and two external components.

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

Hi all,

I needed an ADC on an ATTINY. But we have the comparator. :-)

u8 get_adc (void)
{
  int i;
  int val=0;
  u8 s;

  DDRB |= _BV (PB2); // Drive the signal
  for (i=0;i<255;i++) {
    s = (ACSR >> ACO) & 1;
    val += s;
    PORTB = (PORTB & ~_BV(PB2)) | (s << PB2);
  }  
  PORTB &= ~_BV (PB2);
  DDRB &= ~_BV(PB2); // release the signal. 
  return val; 
}

There is a resistor (2k in my case) on PB2 connected to a capacitor (0.1uF in my case) from PB1 (AIN1) to ground. The analog signal is connected to PB0 (AIN0).

Initialization:

  DIDR = 3; // Disable both digital inputs. 
  ACSR = 0;

I suggest taking the RC time about 10 times longer than the time for one iteration. My Attiny is clocked at 1MHz, and my compiler generated a 20µs loop for the code above. So I chose a 200µs RC time.

with PB2 disabled there is a leakage current towards GND, leading to slightly high samples if you don't sample for a while. The time constant is on the order of seconds, so if you keep sampling a few times per second, there is nothing to worry about. You could improve this by taking a single comparator sample, driving the output to the right value, and then waiting for the comparator to change.

The loop does a few things inefficiently, but it is written to have consistent timing wether we sample zero or one from the comparator.

The loop can be further improved by using byte wide variables. The loop can be further tweaked by switching to assembly.

In my case, 250 times through the loop for an 8bit result takes 5ms, good enough for me.

Here is the hardware:

The RC is just to the right of the tiny, and not mounted when the picture was taken. The analog in (actually POT connector) is the three pin connector "north" of the attiny. Also not mounted yet on this picture.

In this version I messed up the pinout of the second 2x3 header (the programming header). Besides flipping it over I ALSO managed to connect two signals wrong.

I've contemplated patenting this, but several ideas are close, but not quite. But some ideas are so close that I'd get in trouble as violating their patent or more likely that that would be claimed as prior art (public domain). And this trick hasn't dawned on Atmel either, they propose a non-linear ADC trick in AVR400 that works for 2/5th of the voltage range. This is linear and works for the full voltage range.

So now this is published and in the public domain. Probably was already for a long time too, but I couldn't find it with google.

Last Edited: Fri. Dec 18, 2009 - 05:52 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Doctor, doctor, it hUrts when I do this."

"Then don't do that anymore. That will be $59."

OK, I'll bite: why pick practically the only Tiny in captivity that does not have an ADC?

Quote:

Probably was already for a long time too, but I couldn't find it with google.

How about one of the earliest AVR app notes?
http://www.atmel.com/dyn/product...
AVR400: Low Cost A/D Converter

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

If you need ADC and are using a Tiny2313, then ask if you are using the USART. If not then switch to a Tiny48. Same chip price, far less hassle because the Tiny48 has ADC built in.

If the Tiny48 won't fit on your board, then consider using the Tiny261, a 20-pin IC with ADC but no USART. If you need the USART and ADC, go with the Mega48.

The 20th century is over. Sure there are tricks and techniques that can implement ADC/SPI/I2C/DAC/USARTs in software. But, there is no advantage inplementing microcontroller functions in software if you can get a version of the chip that has the function designed into the hardware.

Reliable software USARTs are OK for outputs, but, in my experience, they don't work well for input.

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

Apparently I didn't get the "there has been a reply" emails. :-( Oh well.

1) I have tiny2313 here in stock.
2) I like the ease-of-soldering the SO20 packaging.
3) I can't find anything resembling my trick in the page-of-appnotes you linked. Did you mean to specify a specific appnote? [edit] reading is an art. Ok Appnot AVR400. It has the nonlinear capacitor-charge trick that only works for 2/5ths of the VCC range...
4) It's still neat to be able to do an AD conversion with just a comparator, should the need arise.

5) I'll order a bunch of '48 chips, and start using those in the future. :-)
(I had tried to order a bunch of tiny2313 chips, but they were not in stock. "Available in a few days" it said. Turns out to be several months before they are restocked. I talked to customer service about that, and they got me angry and Customer service then cancelled my order, which I didn't want them to do.....)
6) I like the fact that you can design a mega168 footprint and then solder on a tiny48 if you go into production or somehow end up being more cost-sensitive.

Anyway, that's nice, now I have decicded to switch to '48 devices for the places where I want a cheap device. :-)

Last Edited: Fri. Dec 18, 2009 - 05:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

3) I can't find anything resembling my trick in the page-of-appnotes you linked. Did you mean to specify a specific appnote?

I did.
Quote:

2) I like the ease-of-soldering the SO20 packaging.

Tinys come in a wide variety of packages.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

But tiny48 and tiny88 do not come in so-20 or packages like that. :-(

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

As I said,

Quote:

Tinys come in a wide variety of packages.

Any particular model comes in packages appropriate the the number of I/O. I didn't recommend that Tiny model series in particular. It still remains that
Quote:

why pick practically the only Tiny in captivity that does not have an ADC?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

So you re talking about and ADC slope.

My question:
What will you do if the external voltage is lower than the comparator sellected level? The capacitor voltage will never reach the comparator. So you have to set a time out there.

Michael.

User of:
IAR Embedded Workbench C/C++ Compiler
Altium Designer

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

Icarus. Again I didn't get to see the "new posts" emails and I don't look here that often. Evidently.

There IS a timeout. the loop runs 255 times, no matter the value of the analog input. If the last measurement was high, and the curent one is low, the comparator will say "lower" all 255 times and the result will be "0". I recommend a time-constant of about 1/5th of the time the measurement takes. This means that with the discharge curve of the capacitor you will only have a small (0.7% =~ 2LSB) error on a full-scale swing of the output. i.e. if the last measurement was 255, the current measurement will be 0, even if the voltage is really the value for 1.

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

I recently posted some code to make a successive approximation ADC with a t2313.