/*****************************************************
Chip type           : ATmega8
Program type        : Application
Clock frequency     : 16,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/

#include <mega8.h>

// Standard Input/Output functions
#include <stdio.h>

#include <delay.h>

#define ADC_VREF_TYPE 0x40  

unsigned char blanking = 0;
unsigned char znX = 0;
unsigned char znY = 0;  
unsigned char st_bytte;
unsigned char st_bit0;
unsigned char st_bit1;
unsigned char st_bit2;
unsigned char st_bit3;
unsigned char st_bit4;
unsigned char st_bit5;
unsigned char st_bit6;
unsigned char st_bit7;    
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)  
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out 
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=1 
PORTB=0x01;
DDRB=0x07;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In 
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T 
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=In Func3=In Func2=In Func1=In Func0=In 
// State7=1 State6=1 State5=1 State4=T State3=T State2=T State1=T State0=T 
PORTD=0xE0;
DDRD=0xE0;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 16000,000 kHz
// Mode: Ph. correct PWM top=00FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA1;
TCCR1B=0x01;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 9600
UCSRA=0x00;
UCSRB=0x18;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x67;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 1000,000 kHz
// ADC Voltage Reference: AVCC pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

putsf("OK_START");       

              delay_ms(20); 
         PORTD.7= 0 ; 
               delay_ms(100); 
            PORTD.7= 1 ;  
                 delay_ms(50); 
              PORTD.7= 0 ; 
               delay_ms(100); 
            PORTD.7= 1 ;  
                 delay_ms(50); 
                 
       PORTD.6= 0 ; 
       PORTD.5= 0 ;  
       delay_ms(50);                
       
 

while (1){   


st_bytte = getchar() ;   

  st_bit0 = st_bytte & 0b10000000;  //  стартбит     
  st_bit1 = st_bytte & 0b01000000;  //  стартбит 
  st_bit2 = st_bytte & 0b00100000;  //  стартбит 
  st_bit3 = st_bytte & 0b00010000;  //  бланкинг 
  st_bit4 = st_bytte & 0b00001000;  //  дым помпа  
  st_bit5 = st_bytte & 0b00000100;  //  дым разогрев 
  st_bit6 = st_bytte & 0b00000010;  //  резерв
  st_bit7 = st_bytte & 0b00000001;  //  резерв 
 if(st_bit0>0 & st_bit1==0 & st_bit2>0)  //101
 {      
         if(st_bit3>0) {blanking = 1;} else {blanking = 0;}; 
         if(st_bit4>0) {PORTB.0 = 1;}  else {PORTB.0 = 0;};
         if(st_bit5>0) {PORTD.7 = 1 ;} else {PORTD.7 = 0 ;}; 
         if(st_bit6>0) {PORTD.6 = 1 ;} else {PORTD.6 = 0 ;}; 
         if(st_bit7>0) {PORTD.5 = 1 ;} else {PORTD.5 = 0 ;}; 
      znX=getchar();
      znY=getchar();   
  OCR1AL = znX;
   OCR1BL= znY ; 
//OCR1AL = znX+(read_adc(0)+read_adc(1)/4);  
//OCR1BL = znY+(read_adc(2)+read_adc(3)/4); 
      putsf("OK");
      st_bytte=0;
   } else {  putsf("error");  
        PORTD.7= 0 ; 
               delay_ms(50); 
            PORTD.7= 1 ;  
            delay_ms(10);
   };  	  
};
}                 