ATTINY13A sleep mode ?

Hi friends. how can I put the ATTINY13A to sleep ? my sketch can put the ATTINY45 to sleep without problem but can't working on ATTINY13A.
the sketch:

#include <avr/sleep.h>
#include <avr/interrupt.h>

#define FLASH_TIME      30000ul
#define TOGGLE_ONTIME   200ul
#define TOGGLE_OFFTIME  5000ul

const byte pinLED = 4;
const byte pinSwitch = 3;

bool bLEDIsFlashing, bLEDState;

unsigned long timeLED;
byte ucSwitch, ucLastSwitch;

void setup()
{
  pinMode( pinSwitch, INPUT );
  ucLastSwitch = digitalRead( pinSwitch );
  pinMode( pinLED, OUTPUT );
  bLEDIsFlashing = false;
  bLEDState = false;
  sleep();
}

void loop()
{
  ToggleLEDState();
  if ( bLEDIsFlashing )
  {
    if ( bLEDState == true )
      digitalWrite( pinLED, HIGH );
    else
      digitalWrite( pinLED, LOW );
    if ( (millis() - timeLED) >= FLASH_TIME )
    {
      ucLastSwitch = digitalRead( pinSwitch );
      digitalWrite( pinLED, LOW );
      bLEDIsFlashing = false;
      sleep();
    }
  }
  else
  {
    ucSwitch = digitalRead( pinSwitch );
    if ( ucSwitch != ucLastSwitch )
    {
      ucLastSwitch = ucSwitch;
      if ( ucSwitch == LOW )
      {
        timeLED = millis();
        bLEDIsFlashing = true;
      }
    }
  }
}

void ToggleLEDState( void )
{
  static unsigned long timeLED = TOGGLE_OFFTIME, timeLEDToggle = 0;
  unsigned long timeNow;

  timeNow = millis();
  if ( (timeNow - timeLEDToggle) >= timeLED )
  {
    timeLEDToggle = timeNow;
    if ( bLEDState )
    {
      bLEDState = false;
      timeLED = TOGGLE_OFFTIME;
    }
    else
    {
      bLEDState = true;
      timeLED = TOGGLE_ONTIME;
    }
  }
}

void sleep() {
  GIMSK |= _BV(PCIE);
  PCMSK |= _BV(PCINT3);
  ADCSRA &= ~_BV(ADEN);
  set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  sleep_enable();
  sei();
  sleep_cpu();
  cli();
  PCMSK &= ~_BV(PCINT3);
  sleep_disable();
  ADCSRA |= _BV(ADEN);
  sei();
}
ISR(PCINT0_vect) {
}

Describe the problem. Is there a compile error? Post it.
Does it compile but not do what you expect? Described how its behavior differs from your expectations.

DrAzzy:
Describe the problem. Is there a compile error? Post it.
Does it compile but not do what you expect? Described how its behavior differs from your expectations.

thanks for reply. It's compile and upload without any problem but the chip can't be sleeping.

Did you correctly set ATTiny13A as target? If not it will upload, parts of the code may be working while other will be broken.

Smajdalf:
Did you correctly set ATTiny13A as target? If not it will upload, parts of the code may be working while other will be broken.

yes I set the board in the arduino IDE to ATTINY13A

here is what i use the sleep attiny13A, have fun.

#include <avr/sleep.h>
ISR(WDT_vect) {}


int main(void){
  init();
  {

    WDTCR |= (1<<WDP3) | (0<<WDP2) | (0<<WDP1)| (1<<WDP0); //sleep 8s
    WDTCR |= (1<<WDTIE);
    sei(); // Enable global interrupts  
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);// Use the Power Down sleep mode

  }




  while(1) {

    //do something here....


    sleep_mode();

  }
}

How do you know the sleep is not working? There are some strange things in your code but I don't see any mistake that will prevent it to sleep.

swe-dude:
here is what i use the sleep attiny13A, have fun.

#include <avr/sleep.h>

ISR(WDT_vect) {}

int main(void){
  init();
  {

WDTCR |= (1<<WDP3) | (0<<WDP2) | (0<<WDP1)| (1<<WDP0); //sleep 8s
    WDTCR |= (1<<WDTIE);
    sei(); // Enable global interrupts 
    set_sleep_mode(SLEEP_MODE_PWR_DOWN);// Use the Power Down sleep mode

}

while(1) {

//do something here....

sleep_mode();

}
}

thanks for reply. where place it in my sketch ?

Smajdalf:
How do you know the sleep is not working? There are some strange things in your code but I don't see any mistake that will prevent it to sleep.

I measure the ATTINY13A current with the multimeter it's drain 2.4 mAh but the ATTINY45 drain 900nA.

Try to make a MWE code. Also you may try this:

#include <avr/sleep.h>
#include <avr/interrupt.h>

int main (void) {
  cli();
  MCUCR=(1<<SE)|(1<<SM1);
  sleep_cpu();
}

It simply disables interrupts, enables Power Down sleep and goes to sleep. If it does not sleep the ATTiny properly you either do something wrong or the chip is damaged. If it works there is probably a mistake in your code - it never goes into the branch which invoke sleep or you have a source enabled which wake up the ATTiny as soon as it goes to sleep.

Smajdalf:
Try to make a MWE code. Also you may try this:

#include <avr/sleep.h>

#include <avr/interrupt.h>

int main (void) {
  cli();
  MCUCR=(1<<SE)|(1<<SM1);
  sleep_cpu();
}




It simply disables interrupts, enables Power Down sleep and goes to sleep. If it does not sleep the ATTiny properly you either do something wrong or the chip is damaged. If it works there is probably a mistake in your code - it never goes into the branch which invoke sleep or you have a source enabled which wake up the ATTiny as soon as it goes to sleep.

it's not working. the TINY13A cannot be waking up

Ofc it cannot. It is a test to prove it is able to sleep.