How to "Demo" of 85's ADC ??

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

Hi all

I am looking for an "ASM" example for the ATtiny 85 to simply
demonstrate (Correct setup usage) of the ADC function within
the 25/45/85's

Just a Potentiometer setup when rotated and the voltage at the ADC
input reaches half the 5VDC supply voltage 2.5 volts to light an LED

Just want to see the correct method of setting up the control registers
and reading / the 10 bit value.

And please.. I hope an example in ASM.

Thanks..

"We look for things.. Things that make us go."

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

It probably won't be much different than the worked example at www.avrbeginners.net for the Mega8 or whatever.

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

One of the first things I tried was: read 8 bits of ADC and put them to OCR0A and vary the output pulsewidth with a pot.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Thanks "theusch"

Perhaps close.. but would much rather see an example
for the ATtiny 85

As some register mask bits could be unique.

At first I thouhgt of using the "The Analog Comparator" for such a simple function ??

That would would work (for 1 voltage)

But at some point I would like to "Expand" on this to detect 2 or 3 voltage levels.

Lighting a "Unique" LED / pending the voltage (window) at the ADC input..

By Branching (Pending 10 bit) value returned.
so in effect Multiple "Comparators" (like a Mini VU meter)

Just thought I would start with 1 LED and work from that point once I understand how it works from example.

"We look for things.. Things that make us go."

Last Edited: Wed. Mar 27, 2013 - 06:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hey "Torby"... Let me guess
Project for "Model Railroad" ?? :-)
Speed Control

"We look for things.. Things that make us go."

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

Well, you need to read the datasheet anyway. And you need to account for >>your<< choice of voltages, reference selection, channel, and the like. So correspond the example there with information on ADC from the Tutorials forum.

Post your work, and any specific questions.

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

Hi again "theusch"

Well my "Choice" of voltages are not determined
at this time.. (or the voltage window for them.)

Figured if I could see an example for detecting
"mid point" at 2.5 (or less than) volts for a
window would give me a start to understanding.

As for Posting my work.. (I assume you mean code)

I Don't have any..

Didn't want to dive into code until I could see an example for the ATtiny85

Reading the ADC 10bit value.. and Test branching with it.

"We look for things.. Things that make us go."

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

Quote:

Well my "Choice" of voltages are not determined

Well, it is pretty hard to give a working example without them...

-- Reference voltage selection impacts the setup code, as well as the circuit layout.
-- Rather obviously, channel selection is important to create a working example. Again, depends on test circuit.
-- Clock speed is important to get a "good" conversion result.

See, the short answer is that one-size-does-not-fit-all with an example such as this. [The same thing generally for timer and PWM or similar.] There are just too many options to cover different situations.

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

Quote:

Perhaps close.. but would much rather see an example
for the ATtiny 85

As some register mask bits could be unique.


Well as only about 3 registers are involved it's not that complex to see what might be different. The key control register for ADC is ADCSRA. If you compare the mega8 and tiny85 datasheets you wil find that 7 of the 8 bits are identical and in the same position. The only difference is bit 5 which is called ADFR in the mega8 - that is ADC Free Run and simply means that once ADSC is set the ADC will make back to back conversions repeatedly. Bit 5 in ADCSRA for t85 is described as ADATE. This is a bit more complex than simply ADFR as it brings into play a whole new register. When bit 5 of ADCSRA is set in t85 then ADCSRB dictates how the "auto conversion" mode will operate (mega8 doesn't even have ADCSRB). So now read the documentation of ADCSRB in the t85 data and you find that (a) it defaults to 0 and (b) when it holds 0 (so the bottom three ADTS bits are all 0) then this selects "free running" mode. If you didn't know better you could actually believe that Atmel designed it this way on purpose so that (until ADCSRB is changed) it operates identically to old CPUs like mega8.

The beginners code also shows 0 written to ADMUX. So now compare the two datasheets... M8 tells us this will select AREF as a reference, no left shifting and channel 0. The t85 is slightly different in that to select AREF would erquire bit 6 to be set. So you have two choices. Either you leave the code as it is and accept that ADMUX=0 is going to select Vcc as the reference or you change the line:

ldi r16, 0
out ADMUX, r16

to be:

ldi r16, (1 << REFS0)
out ADMUX, r16

Another thing you will want to change is:

.include "m8def.inc"

at the top to be:

.include "tn85def.inc"

but if you are using AS6 you don't need any .include at all as simply setting "tiny85" in the project options passes a command to the assembler that effectively does that .include anyway.

One other small difference is:

org 0x000E
rjmp ADC_ISR

The vector for ADC moves from 0x000E to 0x0008 inthe t85. But it is bad practice to hard code the address anyway (I would have thought avrbeginners.net would have taught this in fact!). It's far better to refer to it symbolically:

org ADCCaddr
rjmp ADC_ISR

where "ADCCaddr" is define in the header for mega8 as 0x000E and in the header for t85 as 0x0008 so the same code would work on both without change if they had done it this way.

Finally the m8 program outputs to eight LEDs on the whole of PORTD (m8 is 28 pins after all). A t85 doesn't have PORTD/DDRD, only DDRB/PORTB and not a full 8 pins for LEDs so you need to change those lines to pick the other port and perhaps just one or two LEDs showing the top 2-3 bits of the ADCH reading (use a shift/and to get this)

As far as I can see that's it.

This analysis I've just done is something you should learn to do yourself. You aren't always going to find code for the tiny85 or mega328 or xmega192A1 or whatever it is you happen to be using. But most of the time it's possible to "port" code from one AVR to another. All it takes is a comparison of the two datasheets to find out how anything being used differs.

Perhaps someone else reading this can double-check my work to see if I got anything wrong or missed anything but I'm pretty confident this will give you a piece of working t85 code.

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

Perhaps compare the approaches from the
AVRbeginners page http://www.avrbeginners.net/arch...
RetroDan's page https://sites.google.com/site/av...

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

Quote:

RetroDan's

You know what you did there? (HWMNBN!)

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

Thanks again "theusch"

I seen that example at (Avr Beginners)

RetroDan's is nice with examples using different (Modes)

The free-run mode I assume would work the best for this. (I think)

Once I determine my 2 or 3 (Test voltage windows)

Just want to start with 1 simple voltage windown for now (2.5 or less)

Thanks "clawson"

"This analysis I've just done is something you should learn to do yourself.
You aren't always going to find code for the tiny85 or mega328"

Ya. I know..

You guys have worked with this stuff "For Years"
and every day no doubt..

Like the back of your hand intuition.

You just sometimes need to understand (or maybe remember)
how "Beginner" can a "Beginner" be.

"We look for things.. Things that make us go."

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

You still need to read the datasheet. Otherwise, how are you going to adjust this from RetroDan?

    LDI   A,0b1100_0011  ;ENABLE, START CONVERSION & SET PRESCALER
	       OUT   ADCSRA,A
	       LDI   A,0b0000_0011  ;SELECT ADC INPUT #3, PORTB 3
	       OUT   ADMUX,A

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

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

Well I don't expect "to never have to" read a data sheet..

Just reading data sheets for 2 or more "Totally different" chips.

On a topic that I have little (Experience) or (Confidence) in achieving.

I've had a few past (experimental) failures
perhaps in translation errors (Don't know)

I gave up on that (PWM) how it works project..
Maybe I'll get back to it..

It's nice that the code is Remarked so that I know what that line does.. "As a flow chart" so to speak

And thanks for the other "Code Examples" in ASM..

Would have been nice to find an exact code sample
for the 85 to avoid most if not all the "Translation" confusion.

Because if it did not work.. I could eliminate
the possibility of it being a code error.

(I hope)

So I'll see if I can manage with the (How to) ADC function examples for other chips

Cross your fingers for me

With my luck the LED will light with all the voltages but 2.5

"We look for things.. Things that make us go."

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

kds12345 wrote:
Hey "Torby"... Let me guess
Project for "Model Railroad" ?? :-)
Speed Control

Nothing so useful. I just wanted to see if I was using the A/D correctly and a pulse width was the easiest way I could think of to get an 8 bit value out of a Tiny45.

PWM to an output FET is a VERY useful technique.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Indeed, the datasheet is the main resource, but sometimes a bit of code to look at can clarify what the datasheet is trying to tell you.

I sometimes finding myself thinking, "Ok, I've read about all these bits in all these registers, so, um, how do I make this work?" Then I see an example and realize you select the reference, set the clock speed and do it, just like the datasheet has been trying to tell me all along. Once you understand you're dealing with a forest, all the trees start to make sense.

If you don't know my whole story, keep your mouth shut.

If you know my whole story, you're an accomplice. Keep your mouth shut. 

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

Torby wrote:
Once you understand you're dealing with a forest, all the trees start to make sense.

Ya.. No kidding a forest

But I always forget my Compass.

"We look for things.. Things that make us go."

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

Quote:
Just reading data sheets for 2 or more "Totally different" chips.
Accept when you get into xmegas or AVR32s, there are no "Totally different" AVRs. The cores are all basically the same. Systems such as I/O ports, ADC and USARTS are pretty much the same for basic usage. Even the most complex peripheral like the timers, only come in a few varieties. I think you are obsessing too much on what bits to set rather than how they work. Once you know how they work, what bits to set is easy to figure out.

Regards,
Steve A.

The Board helps those that help themselves.

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

Well I'll give it all a go.. (With links provided)
and with any luck get it to do what I hope.

Thanks all.. (And again)

Cross you fingers for me :-)

"We look for things.. Things that make us go."