ATtiny25 Error / Code Problem?

Thread Starter

Rick Meijers

Joined Jul 27, 2015
1
Hello,

I recently started programming AVR chips. I made a program to switch off a raspberry and it's power.
All seemed to be working perfect, but when the program has run for about 2 days in a row and I try ro switch it off, it will not react. After several attempts suddently the device reacts and shuts off. This again happens when I try to switch it back on. If I switch off the power, wait a couple of seconds and start up again it all works perfect.

This is my code, I'm using a ATtiny25.

C:
---------------------------------------------------------------------------

#define F_CPU 1000000UL // 1Mhz
#include <avr/io.h>
#include <util/delay.h>

int button_pressed_counter = 0;
bool button_pressed()
{
    bool a = !(PINB & (1 << PB3));
    if (a)
    button_pressed_counter++;
    else
    button_pressed_counter = 0;
  
    if (button_pressed_counter >= 10)
    {
        button_pressed_counter = 0;
        return true;
    }
    return false;
}

bool temp_shutdown()
{
    bool a = (PINB & (1 << PB0));
    if (a)
    {
        return true;
    }
    else
        return false;
}


int main(void)
{
    DDRB |= (1 << DDB1);    //raspberry power 5V
    DDRB |= (1 << DDB2);    //lcd power 12V
    DDRB &= ~(1 << DDB0);    //'sf' Temp. shutdown feedback Rasp
    DDRB &= ~(1 << DDB3);    //button on/off
    PORTB |= (1 << PINB3);    //pullup for button
    DDRB |= (1 << DDB4);    //shutdown signal for raspberry
  
    bool rasp_on = false;
    bool lcd_on = false;
    bool shutdown_signal = false;

    int state = 0;
  
    int lcd_counter = 0;
    int second_counter = 0;
    int shutdown_counter = 0;
  
    while(true)
    {
        switch (state)
        {
            case 0:    //Power on
            rasp_on = true;
            lcd_counter++;
                if (lcd_counter >= 300)
                {
                    rasp_on = true;
                    lcd_on = true;
                    second_counter++;
                    if (second_counter >=100)
                    {
                        if (button_pressed())
                        {
                            second_counter = 0;
                            lcd_counter = 0;
                            state = 1;
                        }
                        if (temp_shutdown())
                        {
                            second_counter = 0;
                            lcd_counter = 0;
                            state = 1;
                        }
                    break;
                    }
                break;
                }
            break;
          
            case 1:    //Shutdown
            rasp_on = true;
            lcd_on = false;
            shutdown_counter++;
            if (shutdown_counter >= 250)
            {
                shutdown_counter = 0;
                state = 2;
            }
            break;
          
            case 2:    //Power off
            rasp_on = false;
            lcd_on = false;
            second_counter++;
            if (second_counter >= 100)
            {
                if (button_pressed())
                {
                    second_counter = 0;
                    state = 0;
                }
                break;
            }
            break;
        }
      
        //rasp switched_on
        if (rasp_on)
        PORTB &= ~(1<<PB1);
        else
        PORTB |= (1<<PB1);

        //lcd switched_on
        if (lcd_on)
        PORTB |= (1<<PB2);
        else
        PORTB &= ~(1<<PB2);
      
        //Shutdown signal for Raspberry Pi
        shutdown_signal = state == 1;
        if (shutdown_signal)
        PORTB |= (1<<PB4);
        else
        PORTB &= ~(1<<PB4);

        _delay_ms(100);
    }
}
Moderetors note : used code tags
 
Last edited by a moderator:
Top