ATtiny25 Error / Code Problem?

Discussion in 'The Projects Forum' started by Rick Meijers, Jul 27, 2015.

  1. Rick Meijers

    Thread Starter New Member

    Jul 27, 2015
    1
    0
    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.

    Code (C):
    1.  
    2. ---------------------------------------------------------------------------
    3.  
    4. #define F_CPU 1000000UL // 1Mhz
    5. #include <avr/io.h>
    6. #include <util/delay.h>
    7.  
    8. int button_pressed_counter = 0;
    9. bool button_pressed()
    10. {
    11.     bool a = !(PINB & (1 << PB3));
    12.     if (a)
    13.     button_pressed_counter++;
    14.     else
    15.     button_pressed_counter = 0;
    16.  
    17.     if (button_pressed_counter >= 10)
    18.     {
    19.         button_pressed_counter = 0;
    20.         return true;
    21.     }
    22.     return false;
    23. }
    24.  
    25. bool temp_shutdown()
    26. {
    27.     bool a = (PINB & (1 << PB0));
    28.     if (a)
    29.     {
    30.         return true;
    31.     }
    32.     else
    33.         return false;
    34. }
    35.  
    36.  
    37. int main(void)
    38. {
    39.     DDRB |= (1 << DDB1);    //raspberry power 5V
    40.     DDRB |= (1 << DDB2);    //lcd power 12V
    41.     DDRB &= ~(1 << DDB0);    //'sf' Temp. shutdown feedback Rasp
    42.     DDRB &= ~(1 << DDB3);    //button on/off
    43.     PORTB |= (1 << PINB3);    //pullup for button
    44.     DDRB |= (1 << DDB4);    //shutdown signal for raspberry
    45.  
    46.     bool rasp_on = false;
    47.     bool lcd_on = false;
    48.     bool shutdown_signal = false;
    49.  
    50.     int state = 0;
    51.  
    52.     int lcd_counter = 0;
    53.     int second_counter = 0;
    54.     int shutdown_counter = 0;
    55.  
    56.     while(true)
    57.     {
    58.         switch (state)
    59.         {
    60.             case 0:    //Power on
    61.             rasp_on = true;
    62.             lcd_counter++;
    63.                 if (lcd_counter >= 300)
    64.                 {
    65.                     rasp_on = true;
    66.                     lcd_on = true;
    67.                     second_counter++;
    68.                     if (second_counter >=100)
    69.                     {
    70.                         if (button_pressed())
    71.                         {
    72.                             second_counter = 0;
    73.                             lcd_counter = 0;
    74.                             state = 1;
    75.                         }
    76.                         if (temp_shutdown())
    77.                         {
    78.                             second_counter = 0;
    79.                             lcd_counter = 0;
    80.                             state = 1;
    81.                         }
    82.                     break;
    83.                     }
    84.                 break;
    85.                 }
    86.             break;
    87.          
    88.             case 1:    //Shutdown
    89.             rasp_on = true;
    90.             lcd_on = false;
    91.             shutdown_counter++;
    92.             if (shutdown_counter >= 250)
    93.             {
    94.                 shutdown_counter = 0;
    95.                 state = 2;
    96.             }
    97.             break;
    98.          
    99.             case 2:    //Power off
    100.             rasp_on = false;
    101.             lcd_on = false;
    102.             second_counter++;
    103.             if (second_counter >= 100)
    104.             {
    105.                 if (button_pressed())
    106.                 {
    107.                     second_counter = 0;
    108.                     state = 0;
    109.                 }
    110.                 break;
    111.             }
    112.             break;
    113.         }
    114.      
    115.         //rasp switched_on
    116.         if (rasp_on)
    117.         PORTB &= ~(1<<PB1);
    118.         else
    119.         PORTB |= (1<<PB1);
    120.  
    121.         //lcd switched_on
    122.         if (lcd_on)
    123.         PORTB |= (1<<PB2);
    124.         else
    125.         PORTB &= ~(1<<PB2);
    126.      
    127.         //Shutdown signal for Raspberry Pi
    128.         shutdown_signal = state == 1;
    129.         if (shutdown_signal)
    130.         PORTB |= (1<<PB4);
    131.         else
    132.         PORTB &= ~(1<<PB4);
    133.  
    134.         _delay_ms(100);
    135.     }
    136. }
    137.  
    Moderetors note : used code tags
     
    Last edited by a moderator: Jul 27, 2015
Loading...