/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.7a Evaluation
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : tank_tank
Version : 
Date    : 26.05.2010
Author  : Freeware, for evaluation and non-commercial use only
Company : 
Comments: 


Chip type               : ATmega16
Program type            : Application
AVR Core Clock frequency: 1,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 256
*****************************************************/

#include <mega16.h>

// Standard Input/Output functions
#include <stdio.h>                  
                     
unsigned char adc_data[2];
unsigned char i;

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Place your code here
    PORTC.0 = 0;
    PORTC.1 = 0;      
    PORTC.2 = 0;
    PORTC.3 = 0;
    #asm("wdr")
}

interrupt [USART_RXC] void usart_rxc_my(void) 
{
    if(UDR & 0b10000000)
    {
        adc_data[1] = UDR;    
        if(adc_data[1] & 0b01000000)   
        {   
            i = (adc_data[1] << 2);            
            OCR1B = 0x00ff - i;      
            if(OCR1B < 0x000f)
                OCR1B = 0x000f;  
        }                                  
        else
        {         
            i = (adc_data[1] << 2); 
            OCR1B   = 0x00ff & i;    
            if(OCR1B < 0x000f)
                OCR1B = 0x000f;  
        }
    }
    else                  
    {
        adc_data[0] = UDR;    
        if(adc_data[0] & 0b01000000)   
        {   
            i = (adc_data[0] << 2);            
            OCR1A   = 0x00ff - i;  
            if(OCR1A < 0x000f)
                OCR1A = 0x000f;  
        }                                  
        else
        {         
            i = (adc_data[0] << 2); 
            OCR1A   = 0x00ff & i;  
            if(OCR1A < 0x000f)
                OCR1A = 0x000f;    
        }
    }
} 


// Timer1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here 
    if(adc_data[0] & 0b01000000)   
    {
        PORTC.0 = 1;                  
    }
    else
    {
        PORTC.1 = 1;                 
    }  
}

// Timer1 output compare B interrupt service routine
interrupt [TIM1_COMPB] void timer1_compb_isr(void)
{
// Place your code here    
    if(adc_data[1] & 0b01000000)   
    {
        PORTC.2 = 1;                  
    }                                  
    else
    {                                
        PORTC.3 = 1;         
    }
                             
}


// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTA=0x00;
DDRA=0x00;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=0 State2=0 State1=0 State0=0 
PORTC=0x00;
DDRC=0x0F;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15,625 kHz
// Mode: Fast PWM top=00FFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: On
TCCR1A=0x01;
TCCR1B=0x0B;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0xff;
OCR1BH=0x00;
OCR1BL=0xff;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x1C;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: Off
// USART Mode: Asynchronous
// USART Baud Rate: 2400
UCSRA=0x00;
UCSRB=0x10;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
UCSRB = (1<<RXCIE)|(1<<RXEN);

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

#asm("wdr")
// Watchdog Timer initialization
// Watchdog Timer Prescaler: OSC/2048k
#pragma optsize-
WDTCR=0x1F;
WDTCR=0x0F;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Global enable interrupts  //
//#asm("cli")
#asm("sei")
#asm("wdr")

adc_data[0] = 0;
adc_data[1] = 0;


while (1)
      {
      // Place your code here

      }
}
