0

I am trying to emulate the behavior of a circuit using an Attiny85

This is the result I got so far:

enter image description here

In the picture above, D2 is VCC, D4, D5 and D6 are outputs of the circuit I am trying to emulate. D7 is the output of Attiny85

The code I am using is:

#define F_CPU 8000000L
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>

int main() {

  DDRB = 0b00001000;

  PORTB = 0b00001000;
  _delay_ms(5);

  PORTB = 0b00000000;
  _delay_ms(5);
  PORTB = 0b00001000;
  _delay_ms(5);

  PORTB = 0b00000000;
  _delay_ms(5);

  _delay_ms(5);

  while(1)
  {  
     PORTB = 0b00001000;
    _delay_ms(1);
     PORTB = 0b00000000;
    _delay_ms(1);
  }
    return 0;
}

According to this, it takes roughly 64ms from powerup until it actually run the main program.

Is there any reason for such a delay? How can I reduce it somehow (ideally to 20ms or less)?

2
  • do you use a bootloader or you program it over ISP?
    – Juraj
    Nov 11, 2018 at 6:19
  • Sorry, forgot to mention - yes, I programmed over a ISP (usbtiny) to make sure the bootloader is not active Nov 11, 2018 at 6:54

1 Answer 1

5

The ATtiny85 has a built-in delay counter that keeps the MCU in reset mode for some time after the power is applied. This seems to be intended as a wait for the clock and power to be stabilized. The length of this delay depends on your selected clock source, and is somewhat configurable with the fuses.

According to your code, your MCU is clocked at 8 MHz. I assume you are then using the internal RC oscillator as a clock source. If this is the case, you have three choices for the startup delay:

  • 20 CPU cycles
  • 20 CPU cycles + 4 ms
  • 20 CPU cycles + 64 ms

The latter is recommended if your power rises slowly, and is the default configuration in which these MCUs are shipped. If you power level rises fast, you may want to select a shorter delay by programming the SUT fuses. See the datasheet for details (section System Clock and Clock Options → Clock Sources → Calibrated Internal Oscillator).

1
  • You are right - my L fuse was programmed as 0xE1 (PLL clock, 64ms delay). I changed it to 0xD2 (Internal clock, 8Mhz, 4ms delay) and it worked Nov 11, 2018 at 20:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.