ATtiny13 ADC what i'm doing wrong (or not?)

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

Hello everyone,
I will try to provide the most info, in order someone to help me with this little problem...

I'm using attiny13 in order to measure small changes
at a voltage level around 3,578 Volt.
The pin used is PB4 (ADC2) and is driven through a
resistor network (voltage divider by approx.3)
The input of the voltage divider is from transformer
9V, diode bridge, Capacitor filter (100uF).
When used on wall outlet without the tiny13,
i setup the voltage on PB4 to be around 3,578 V (so
223 Volt AC on the outlet). When inserting the
tiny13 on the circuit, it's doesn't work as i want
and many times gives an up / down sequence, but when
i measure the input voltage on PB4 is far away my setup point (around 2,95 V)
The PB0 and PB1 are setup according to the:

PB1  PB0
 1    1 -> AC between 215V (3,497) and 225V (3,660)
 0    0 -> AC between 215V (3,497) and 225V (3,660)
 0    1 -> AC lower than 215V
 1    0 -> AC higher than 225V

The tiny13 ADC is clocked by internal 4,8MHz
The setup for the PORTB (i have equed the PBx ports
in the header file, instead using PORTBx, PINBx)

ldi genio, (1<<PUD) (with genio = r16)
out MCUCR, genio    Disable Pull-ups (but the same problem is occured and with pull-ups enabled)

ldi genio, 0b100111 ; PB4,3=In (ADC),PB5,PB2-0 = OUT
out DDRB, genio
ldi genio, 0b000100 ; Startup values / No pullups
out PORTB, genio    ; PB2 is used as "operating Led"

The watchdog is not used, turned off as described on
the datasheet ot the tiny13

The Setup for ADC:

ldi genio, (1<<ADEN) | (0<<ADSC) | (0<<ADATE) | (0<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0)
out ADCSRA, genio

With Prescaler = 64, ADC Clock = 4,8Mhz/64=75kHz

Channel and Reference

ldi genio, (0<<ADLAR) | (0<<REFS0) | (1<<MUX1) | (0<<MUX0)	
out ADMUX, genio

Vcc=5V as ref., no left adjust, channel=ADC2/PB4

Start a conversion and wait until completed:

sbi ADCSRA, ADSC
nop
WaitADC:
sbis ADCSRA, ADIF
rjmp WaitADC

After conversion completed, i get the values:

in ResLo, ADCL	(ResLo = r17)
in ResHi, ADCH  (ResHi = r18)

My desired values are between $2CC and $2EE with
the value $2DD being for the 220V output
(about 3,578 to the PB4 pin)
I created a table for the 10-bit analysis of 5V
and found the above values (approx.)
So $2CC = 716decimal and $2EE = 750dec approx.

The checking routine to decide what to do...
(The label ToReturn Jumps to Main Program
after a small delay ~ 5ms)

cpi	ResHi, 2
breq	CheckLevels
cpi	ResHi,3
breq	isHigher		
cpi	ResHi, 1		
breq	isLower

CheckLevels:
cpi     ResLo, $CC
brlo	isLower			
cpi	ResLo, $DD
brsh	isHigher

;If not jumped on CheckLevels, we are between, so OK
sbi	PORTB, PB0
sbi	PORTB, PB1
rjmp	ToReturn

isLower:
sbi	PORTB, PB0
cbi	PORTB, PB1
rjmp	ToReturn

isHigher:
cbi	PORTB, PB0
sbi	PORTB, PB1

The PB0 and PB1 are read by a AT90S2313 MCU
and according to the states, lights the
corresponing Led and chenges the driving
sequence for the rest of the circuit

My problem is that the voltage measured on PB4
is not as it had to be, and even worse
when the output goes higher (i.e 230V)
the PB4 pin and so the tiny13 still has less
than what it should be (i.e 3,205 V instead of 3,990)

Do you see any software problem?

Any help wold be very much aprreciated
Thanks in Advance

Nikos

[some code tags added - moderator]

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

Quote:

Do you see any software problem?

How could a software problem influence the voltage present on input to the ADC pin?

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

Thanks moderator for the code quotes.

Software can't incluence the voltage,
but i can't see anything else wrong so far
(the hardware is as it should be - at least
to my knowledge)
Without the chip inserted to the socket, the
voltage on PB4 is as it must be.
With the tiny13 on socket it is not.
By software i mean, if i miss-defined something
(i.e a pull-up) or checkig something not correctly
that influences the behaviour of the port pin.

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

Nope, setting the pin as IO or enabling a pull-up might have been the cause but AFAICS it's 0-0 for pin 3 so as long as you haven't mixed things up and that is the active ADC pin you are using then I don't think anything in the software can be affecting it so I'd be looking at the circuit design if I were you. One thing you could try is to lift that leg of the IC so the AVR is not in a position to affect the presented voltage then measure the level. Has it changed? IOW was the AVR affecting it?

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

Everything is fine on the pcb (not because i made it!).
Voltages with pin up are as they should and behavior
of the rest circuit also.
I will do the same program in Bascom-AVR (which i also use in some projects) and see if the circuits behaves
differently.
As for now, thanks for your replies.

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

Ntnano: have you decoupled power supply with a ceramic capacitor close to the Attiny13? Should be 0.1 uF or more.

If your resistor network is high impedance (> 10K appr.), that may cause the problem. AVR ADC is low impedance. You may have to lower resistor values or buffer with an opamp.

Peter

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

peteralarsen wrote:
Ntnano: have you decoupled power supply with a ceramic capacitor close to the Attiny13? Should be 0.1 uF or more.

If your resistor network is high impedance (> 10K appr.), that may cause the problem. AVR ADC is low impedance. You may have to lower resistor values or buffer with an opamp.

Peter


Peter, thanks for the reply.
Decoupling is done with 1uF tantalium (not ceramic, but i use these capacitors in all of my circuits without any problems so far on any ic).
My impedance is about 9k, i might lower it a little
and see...The datasheet specs are for up to 10k for neglible sampling time and grows a little with higher.
A possible problem is what's next on the datasheet:
"The user is recommended to only use low impedant sources with slowly varying signals"
The problem is that it is not a slowly varying signal, but continuously changing, until the output voltage is stabilized
My BASCOM program performed the same (i might say a little better - but i used higher margins)

My final though is that first i will use the lower impedance path and increase the voltage margin
and time to measure the voltage, even though it's not
100% my preference, or to return to original layout,
with manually select through 2 switches the duty cycle of my inverter - until i solve this.
A buffer might help also (actually it was my first though also when i saw the behavior), because of the low output impedance, but this involves a new pcb, maybe in the future - not much time to build it now.

But first of all, i will recheck both of the firmwares (adc and driver circuit), because sometimes the obvious is in front of us and we can't see - maybe i missed something in the code.

Thanks.
Nikos

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

Quote:
Start a conversion and wait until completed:
Code:
sbi ADCSRA, ADSC
nop
WaitADC:
sbis ADCSRA, ADIF
rjmp WaitADC
Where do you clear ADIF ?

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

Visovian wrote:
Quote:
Start a conversion and wait until completed:
Code:
sbi ADCSRA, ADSC
nop
WaitADC:
sbis ADCSRA, ADIF
rjmp WaitADC
Where do you clear ADIF ?

Frankly i don't, because it's initial value is 0 (i added a sbi ADCSRA, ADIF to clear it,
after reading the ADCL and ADCH, but with no results also)...

The problem might be the construction of the rest circuit, even though i'm using Metal Film Resistors
and low esr capacitor...

The circuit works from the beginning, but not as expected, meaning that if i set an inital value
without the tiny13 inserted and go from there,
it sends lower / higher in other output voltages
than it should be...
So, the output might begin at 215 VAC and goes
all the way down to 150VAC before it send the
'lower' signal to start going up...It might
reach 260VAC also in the up steps before
signaling the 'higher' even though at that
output voltages, the voltage on ADC2 is 4.15V
(and it had to send the 'lower' signal way before)...

I will try some other things in mind also...

Thanks for your point anyway.
Nikos

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

Maybe use a regular old 10K pot from 5V right to the a/d and see if the leds operate as expected. If so, its your diver network.

Imagecraft compiler user

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

bobgardner wrote:
Maybe use a regular old 10K pot from 5V right to the a/d and see if the leds operate as expected. If so, its your diver network.

Yes, this is the 'other things in mind' and even
disconnect the output reference and put the
reference dc voltage after the bridge on that pin.
Thank you also

Edit: Additions about 3 hours later:
I tried disabling the ADC after reading the values of ADCL/H and output the PB0,PB1 states, by:

cbi ADCSRA, ADEN 

and then re-enable it after a short delay, trying to relieve the pin

sbi ADCSRA, ADEN 

but this made things even worse, because the first (and last as it seems) conversion stayed (as a result)
The reason for this, is because it seems like the Tiny13 "Holds" my voltage on that pin even the conversion has been finished, even when i switch off the output stage (and thus my reference voltage drops to about 6 VAC), but the voltage on ADC pin remains about the same as when 219VAC was feeding the circuit (looks like the S/H circuit really holds...)
Maybe i will go back and re-design the window comparator i used a few years ago, but the problem with that approach is that it can't tuned for this precision, at least not without finding a very accurate comparator and resistors of 0.1%, that's why i decided to implement it with the 10bits precision and the ease of a microcontroller...

Can i really do what i tried with the off/on of the ADC?
I event changed PORTB state for that pins and tried even 0 or 1 but with no results, worth writing here...
Any help would be very much appreciated.

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

Just a quick update to inform all that problem solved...

I re-wrote the assembly program from scratch,
lowered the input path to PB4 by 1/10, added a 470pF
capacitor very near the input of the ADC and finally
changed the startup values and timing of the
driver to 3 times the time of ATtiny13 ADC report.
Probably the timing or some logical error that
i couldn't find caused the problem...

And happily ever after...