#include <io.h>
#include <delay.h>

void main(void)
{      
    unsigned char mode = 0; 
    unsigned char counter = 0;
    unsigned char dirChanged = 0;
    unsigned char delayCnt = 0;
    
    // Input
    DDRC &= ~((1 << 0) | (1 << 1));
    // Pullup       
    PORTC |= ((1 << 0) | (1 << 1)); 
    
    // Output
    DDRA = 0xFF;
            
    PORTA = 1 << mode;  
    while(1)
    {       
        // Mode     
        if((PINC & (1 << 0)) == 0)
        {                  
            while((PINC & (1 << 0)) == 0)
            {
                // do nothing
            }
              
            mode++;
            if(mode >= 5)
            {           
                mode = 0;
            }                 
            
            PORTA = 1 << mode;
        }
         
        // Enter
        if((PINC & (1 << 1)) == 0)
        {          
            PORTA = 0;  
            counter = 0;
            while((PINC & (1 << 0)) != 0)
            {        
                if(delayCnt >= 40)
                {                 
                    delayCnt = 0;
                    
                    if(mode == 0)
                    {             
                        // Blink
                        PORTA = ~PORTA;
                    }           
                    
                    if(mode == 1)
                    {                  
                        // UpCounter
                        PORTA++;
                    }
                    
                    if(mode == 2)
                    {    
                        // ringCounter
                        PORTA = 1 << counter;
                        counter++;
                        if(counter >= 8)
                        {              
                            counter = 0;
                        }
                    }
                    
                    if(mode == 3)
                    {     
                        if(dirChanged  == 0)
                        {                  
                            PORTA = 1 << counter;    
                        }
                        else
                        {
                            PORTA = 1 << (7-counter);
                        }  
                        
                        counter++;
                        if(counter >= 8)
                        {          
                            dirChanged = !dirChanged;    
                            counter = 1;
                        }
                    }
                    
                    if(mode == 4)
                    {    
                        PORTA ^= 1 << counter;
                        counter++;
                        if(counter >= 8)
                        {              
                            counter = 0;
                        }
                    } 
                }
                                
                delay_ms(50);  
                delayCnt++;
            }    
        }    
    }
}
   