Go Down

Topic: Atmega 8 weird behaviour  (Read 301 times) previous topic - next topic

mryazdani

Jan 12, 2018, 11:16 am Last Edit: Jan 12, 2018, 11:30 am by mryazdani
Hi,

I'm trying to get familiar with this microcontroller. I'm using an arduino uno board without micro to program Atmega 8. I've put it in a breadboard. With 328p all things is OK but win atmega 8 it's not.
Let me explain the problem.
1- After reset it takes a while to start the code.
2- If I disconnect the serial wires (pin 0, 1), It won't go to sleep.
3- If I use "main" function, I can see the microcontroller can consume 0.5 micro amp right according to datasheet. But if I use "setup" & "loop" functions, it will consume about 130 micro amps which doesn't make sense.
4- The important part is if I use RTC library or other library in my code when the micro wakes up, it can't go to sleep again.

Code: [Select]

#include <avr/io.h>
#include <avr/sleep.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <Rtc_Pcf8563.h>

Rtc_Pcf8563 rtc;
void PowerDown();

//int main()
void setup()
{
  sei();

  for(int i =0; i <= 13; i++)
  {
    pinMode(i, OUTPUT);
    digitalWrite(i, 1);  
  }

  pinMode(3, INPUT);           // set pin to input
  digitalWrite(3, HIGH);       // turn on pullup resistors
  rtc.initClock();
/*  while(1)
  {
  }*/
}

void loop() {
     rtc.setTime(0, 0, 0);
     rtc.setAlarm(1, 99, 99, 99);
     PowerDown();
     rtc.clearAlarm();
     _delay_ms(5000);
}

ISR(INT1_vect)
{
}

void PowerDown() {
 sei();
 
 // level interrupt INT0 (low level)
 MCUCR &= ~((1 << ISC11) | (1 << ISC10));
 // enable external interrupt
 GICR |= (1 << INT1);
 // set sleep mode
 set_sleep_mode(SLEEP_MODE_PWR_DOWN);
 sleep_enable();
 sleep_cpu();
 
 // disable all interrupts
 sleep_disable();
 GICR &= ~(1 << INT1);
 cli();  
}

Smajdalf

I know little about this but AFAIK using setup+loop silently enables and configures some settings of Arduino - i.e. Timer0 for millis() function and enables and configures ADC. I don't know ATMega8 but on similar ATMega328 enabling ADC would cause the extra current consumption - the analog part of ADC stays awake even in Power Down sleep easily causing the observed result. Since you are not using analogRead you may try to disable ADC in setup:
ADCSRA=0;

mryazdani

Thanks for your reply. Your guide was helped me. The problem number 3 has solved but I still having problem with going into sleep. I'm sure I should put some code but I cant find out what. If I delete rtc code and make the interrupt manually I can put Atmega 8 into sleep mode for several times.

Smajdalf

I guess you are not clearing the alarm properly. Try to check voltage on the alarm pin with a DMM. IIRC the alarm pin of PCF8563 is open drain - do you have pull-up resistor on the pin? But I think all those things are properly handled in software...
In PowerDown() you disable interrupts via cli() and you enable them again just before going into sleep. I think it may cause the problem - default I2C in Arduino is interrupt driven so you ruin it with disabling the interrupts generally.
In Arduino interrupts are enabled by default so you don't need sei() (but it is no problem to use it just to be safe). From your code i don't think cli() should be used at all.

Go Up