Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

atmega8 to atmega168 porting problem
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> AVR
View previous topic :: View next topic  
Author Message
mpo

Bascom Member



Joined: 18 Dec 2009
Posts: 39

poland.gif
PostPosted: Mon Nov 04, 2013 11:51 am    Post subject: atmega8 to atmega168 porting problem Reply with quote

Hi,
Not sure if my question is more bascom or hardware related, but I start here.

I have device based on atmega8 which has feature: "write few bytes to eeprom when power off".
It's done by monitoring power line. When the voltage drop, interrupt occurs and chip still has enough power from condensator to write to eeprom and stop.
This works perfect with atmega8.
Recently I switched to atmega168 (because of bigger flash) and it doesn't work anymore.
In both cases BOD is disabled, and internal 8MHz clock is used.
What I forgot to change, when I put atmega168 to my device??? Rolling Eyes

Relevant part of the code:

Code:

Ddrd = &B1000_0010                                                              
Portd = &B1111_1110                                                            
Dim E_pulses_current As Eram Single
Dim Pulses_current As Single
On Int1 Power_fail
Config Int1 = Falling
Enable Int1
Enable Interrupts

do
'code making all other magic here
loop

Power_fail:
E_pulses_current = Pulses_current   'write four bytes (variable single type) to eeprom
Stop                                              'just stop
Return                                           'in fact it will never return
 


Part of the diagram below.

(BASCOM-AVR version : 2.0.7.6 )
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 386
Location: Wuppertal, Germany

germany.gif
PostPosted: Mon Nov 04, 2013 2:10 pm    Post subject: Reply with quote

To tell weather your problem is HW or SW, I would change R23 from 0R to 1K. Then pull down the regarding input, while power is still up and stable.
If the code works fine, you need more capacitiy on your backup-C!

Marc
Back to top
View user's profile
mpo

Bascom Member



Joined: 18 Dec 2009
Posts: 39

poland.gif
PostPosted: Mon Nov 04, 2013 9:15 pm    Post subject: Reply with quote

I tried this.
When INT1 is just pulled via resistor and I ground it manually, interrupt occurs and eeprom is written.
But I see no reason why backup capacitor is not enough. I tried to add 1000uF more and still nothing. According to datasheet, required current in active state is about 50% less than in atmega8.
Weird...
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Mon Nov 04, 2013 9:35 pm    Post subject: Reply with quote

mpo wrote:
According to datasheet, required current in active state is about 50% less than in atmega8.

If current is drawn from an active pin, the current for the cpu is negligible.
Quote:
I tried to add 1000uF more and still nothing.

I'd try an 1k parallel to C2 to have a sharp drop in input voltage.
Back to top
View user's profile
Evert :-)

Bascom Expert



Joined: 18 Feb 2005
Posts: 2156

netherlands.gif
PostPosted: Mon Nov 04, 2013 9:39 pm    Post subject: Reply with quote

and what power consumption did your measured with the M8 and the M168?
_________________
www.evertdekker.com Bascom code vault
Back to top
View user's profile Visit poster's website
mpo

Bascom Member



Joined: 18 Dec 2009
Posts: 39

poland.gif
PostPosted: Mon Nov 04, 2013 10:22 pm    Post subject: Reply with quote

MWS wrote:

I'd try an 1k parallel to C2 to have a sharp drop in input voltage.

Good idea - I'll try this tomorrow.
I have lot of power consumers connected before schottky diode, so drop should be immediate. But 1k will not harm.

Evert,
I did not measured power consumption. I believe in datasheets Smile
There is enough energy in 220uF cap to write at least eight bytes to eeprom on interrupt (using atmega8 without any power saving methods). Maybe I can write much more - never tried.
So I assume it should be sufficient also in case of atmega168. But maybe something is differently designed inside chip.
Work in progress...
Back to top
View user's profile
Neill

Bascom Member



Joined: 08 Dec 2008
Posts: 50

newzealand.gif
PostPosted: Tue Nov 05, 2013 10:11 am    Post subject: Reply with quote

Is it too late to make some changes in hardware? I would use a comparator to generate the interrupt signal, with a crude reference like a Zener on one pin and a Voltage divider from Vcc on the other.

I think some AVRs even have an analogue comparator built in, perhaps it can generate an interrupt, something to look into?
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 386
Location: Wuppertal, Germany

germany.gif
PostPosted: Tue Nov 05, 2013 12:00 pm    Post subject: Reply with quote

Signal forming is a good point, but I think doing that on the SW side using any kind of debounce should do.
What I woory about is the potential on the input.
If Vcc drops, there is no more Vcc. But does that mean there is a proper GND?
A 1k from Vcc to GND could help, but not for signal forming but to get a proper potential at the input.
Back to top
View user's profile
six1

Bascom Expert



Joined: 27 Feb 2009
Posts: 553

germany.gif
PostPosted: Tue Nov 05, 2013 12:04 pm    Post subject: Reply with quote

the +5V line is connected to other parts, so there might be a big capacitance inside which holds the 5V up!

Take another Diode to INT Input with little C and 10K R for defined drop down!

_________________
For technical reasons, the signature is on the back of this message.
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Nov 05, 2013 12:55 pm    Post subject: Reply with quote

six1 wrote:
Take another Diode to INT Input with little C and 10K R for defined drop down!

Won't make any difference.
Quote:
the +5V line is connected to other parts, so there might be a big capacitance inside which holds the 5V up!

If the capacitance can hold up the 5V line, it will do the same via diode, cap and resistor, minus the voltage drop of the diode.
A simple bleeding resistor as suggested will solve that.
Back to top
View user's profile
laborratte

Bascom Expert



Joined: 27 Jul 2005
Posts: 299
Location: Berlin

germany.gif
PostPosted: Tue Nov 05, 2013 1:53 pm    Post subject: Reply with quote

I agree to Neill. Monitoring a power line is a task for analog circuts.

If the falling time in power line is too slow (i.e. a big cap in power supply) the avr power behind the diode will also be fallen when the int occur. INT as digital input has a LOW voltage of about 0,2 to 0,3 Vcc, so it is depended on your voltage behind the diode. In worst case there will be not enough voltage for writing EEPROM when the interrupt occurs. AVRs are very sensitve on voltage for EEPROM write access, that is the reason why it is recommend to use BOD when working with EEPROM.

It seems that the mega8 could handle the EEPROM write with the cap's energy but the mega168 (PA?) fails because of slightly different electrical characterisics on LOW voltage and needed voltage for EEPROM write.

If you can change your circut use the analog comparator of the AVR. You can make a voltage divider in your power line (i.e: 3:1) which gives you a nominal 1,25V. Feed this in negative input of analog comparator and put the positive to the internal bandgap reference (1,1V). The comparator then can fire an interrupt when the voltage drops below 1,1V, which equals a 4,4V on power line. This gives you enough headroom for writing the EEPROM. Or choose a voltage divider which fits your needs, but make shure that you have at least 2,4V for 5ms after interrupt occurs. This is in fact an BOD which points to an isr rather than a reset.

BTW your isr should also handle the case where main power returns before the AVR stops working, i.e. a short power drop. Could also be done with watchdog...
Back to top
View user's profile
mpo

Bascom Member



Joined: 18 Dec 2009
Posts: 39

poland.gif
PostPosted: Tue Nov 05, 2013 3:18 pm    Post subject: Reply with quote

Well, the PCB is fixed, so there is no room for additional comparator.
On the output of 7805 I have only one 100nF cap. Total current is about 100-200mA, so voltage should drop immediately.

Some progress...
I tried 1k resistor in parallel to C2 - no changes.
I removed ceramic C2 - no changes
I replaced ceramic C2 with tantalum cap 1uF - random trash in eeprom
I added again 1k to 1uF - much better, but sometimes random eeprom address is written with random value

Unfortunatelly I have no possibility to check how fast voltage drops before diode and how long it's high enough on backup capacitor.

laboratte,
As I wrote, I don't have big cap on power supply line.
This used to be worked with smaller chip and I'm very surprised with this situation.
According to datasheet mega168 (not the picopower version in my case) works properly from 1,8V so it should be even more time before energy from cap will exhaust.
What do you mean by "fails because of slightly different electrical characterisics on LOW voltage and needed voltage for EEPROM write."?
Regarding 'power return situation' - I have watchdog running. When avr stop after eeprom write, it will be restarted after timeout.

I wonder if it will make difference if I disable pullup on D3 (INT1) pin?
Back to top
View user's profile
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2262

blank.gif
PostPosted: Tue Nov 05, 2013 4:50 pm    Post subject: Reply with quote

You can try (if your circuit allows for it) to disable the output pins in the voltage-low ISR, maybe one of the pins sucks out the µC's power, so disabling them can buy you some time. The same, disabling the pullups can help therefore.
As you run in a stop-loop you could also use NOSAVE, which spares a few cycles. However, shouldn't make a difference, compared to the cycles used up by eeprom-erase/write.
But try it out...
Code:
On Int1 Power_fail NOSAVE
...
Power_fail:
DDRD = 0
SFIOR.PUD = 1
E_pulses_current = Pulses_current
Stop
Return

Are you aware, as power down may interrupt in-midst of writing Pulses_current, that the value therein may be invalid?
Btw., I see in your circuit the cap for the µC's VCC/GND is 10n, that's too less. Should be 100n ceramic for sourcing the current peaks while writing the eeprom.
Back to top
View user's profile
mpo

Bascom Member



Joined: 18 Dec 2009
Posts: 39

poland.gif
PostPosted: Wed Nov 06, 2013 10:33 am    Post subject: Reply with quote

I added some sugested changes to isr

Code:

power_fail:
'switch all to input
DDRB=0
DDRC=0
DDRD=0
'and disable pullup
MCUCR.PUD=1
 


Also I disabled unused peripherials to lower power consumption PRR=&b1100_1100
Few more or less cycles before writing eeprom makes no difference. In my case, most of the time avr spends servicing interrupt triggered by Timer0, so I have to wait for return from isr_timer0.
It would be nice to have interrupt priority feature Rolling Eyes

In current setup, eeprom shows some garbage after about 5% poweroff events. I don't feel comfortable with this - some more work is apparently needed.
(10n caps values is copy/paste mistake. On pcb I physically have 100n)
Back to top
View user's profile
Arera

Bascom Member



Joined: 23 Sep 2007
Posts: 386
Location: Wuppertal, Germany

germany.gif
PostPosted: Wed Nov 06, 2013 11:18 am    Post subject: Reply with quote

Here we go again:
DON'T WASTE TIME IN ISR!
just set flag (bit) in the ISR.
Check for that flag in the main routine and do your stuff if it's set. Don't forget to reset the flag after your are done with the things to do aftre that int occured.

Marc
Back to top
View user's profile
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum