/*******************************************************
This program was created by the
CodeWizardAVR V3.12 Advanced
Automatic Program Generator
© Copyright 1998-2014 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : MUON telescope
Version : 1
Date    : 03.10.2018
Author  : Khodnevych
Company : private
Comments: 


Chip type               : ATmega32A
Program type            : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 512
*******************************************************/

#include <mega32a.h>
#include <delay.h>

int G1=0, G2=0, G3=0,G4=0,G5=0;
// Declare your global variables here
     int flag=0;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
if (PINB.3)    {G1=(G1<<1)+1;PORTC.0=1; }
else    {G3=(G3<<1)+1;PORTB.5=1;}
//if (PINB.3)    {G1=(G1<<1)+1;PORTC.0=1; }
//if (PINB.1)    {G3=(G3<<1)+1;PORTB.5=1;}
   PORTD.0=1; 
   flag=1;
}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
if (PINB.0)    {G4=(G4<<1)+1;PORTC.6=1;}
else    {G5=(G5<<1)+1;PORTC.7=1;}	
//if (PINB.0)    {G4=(G4<<1)+1;PORTC.6=1;}
//if (PINB.5)    {G5=(G5<<1)+1;PORTC.7=1;}
          PORTD.0=1;
             flag=1;

}



// External Interrupt 2 service routine
interrupt [EXT_INT2] void ext_int2_isr(void)
{
G2=(G2<<1)+1; 
PORTC.1=1; 
   PORTD.0=1;
              flag=1;

}

  int cnt=0 ;



void main(void)
{

DDRA=0xff;
PORTA=0x00;

DDRB=0x20;
PORTB=0x00;;

DDRC=0xff;
PORTC=0x00;

DDRD=0xf3;
PORTD=0x00;



// External Interrupt(s) initialization
GICR|=(1<<INT1) | (1<<INT0) | (1<<INT2);
MCUCR=(1<<ISC11) | (1<<ISC10) | (1<<ISC01) | (1<<ISC00);
MCUCSR=(1<<ISC2);
GIFR=(1<<INTF1) | (1<<INTF0) | (1<<INTF2);

// Global enable interrupts
#asm("sei")
    
while (1)
      {  
      
      if (flag>=1){cnt++;}
      if (cnt>=20){cnt=0;flag=0;}    
      if (G1>255|G2>255|G3>255|G4>255|G5>255){G1=0;G2=0; G3=0;G4=0;G5=0;} 
      
      if (flag==0)  {
      PORTC.7=0; 
      PORTC.6=0; 
      PORTC.0=0; 
      PORTB.5=0;
      PORTC.1=0; 
      PORTD.0=0;
      }
        PORTD.1=1;   PORTD.6=0;  PORTD.5=0;  PORTD.4=0;  PORTD.7=0; 
        PORTA=G4; 
        delay_ms(5);
        PORTD.1=0;   PORTD.6=1;  PORTD.5=0;  PORTD.4=0;  PORTD.7=0; 
        PORTA=G5;  
        delay_ms(5);
        PORTD.1=0;   PORTD.6=0;  PORTD.5=1;  PORTD.4=0;  PORTD.7=0; 
        PORTA=G2;  
        delay_ms(5);
        PORTD.1=0;   PORTD.6=0;  PORTD.5=0;  PORTD.4=1;  PORTD.7=0; 
        PORTA=G1;  
        delay_ms(5);
        PORTD.1=0;   PORTD.6=0;  PORTD.5=0;  PORTD.4=0;  PORTD.7=1; 
        PORTA=G3; 
        delay_ms(5);
      }
}
