/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.9 Standard
Automatic Program Generator
© Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project : 
Version : 
Date    : 28.03.2014
Author  : F4CG                            
Company : F4CG                            
Comments: 
Chip type           : ATmega16
Program type        : Application
Clock frequency     : 12,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/
#include <mega16.h>                 
#include <delay.h>  
#define RXB8 1
#define TXB8 0
#define UPE 2
#define OVR 3
#define FE 4
#define UDRE 5
#define RXC 7
#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<OVR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)
// USART Receiver buffer
#define RX_BUFFER_SIZE 2
unsigned char rx_buffer[RX_BUFFER_SIZE];
#if RX_BUFFER_SIZE<256
unsigned char rx_wr_index,rx_rd_index,rx_counter;
#else
unsigned int rx_wr_index,rx_rd_index,rx_counter;
#endif
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
                                       
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
unsigned char status,data;
status=UCSRA;
data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {   
   rx_buffer[rx_wr_index]=data;
   if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
   if (++rx_counter == RX_BUFFER_SIZE)
      {
      rx_counter=0;
      rx_buffer_overflow=1;
      };     
   };
}
                    
void putchar1(unsigned char c){
  //while ((UCSRA & DATA_REGISTER_EMPTY)==0);
  while(!(UCSRA & (1<<UDRE)));
  UDR=c;
} 
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
unsigned char getchar(void)
{
unsigned char data;     
while (rx_counter==0);
data=rx_buffer[rx_rd_index];
if (++rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0;
#asm("cli")
--rx_counter;
#asm("sei")
return data;
}
#pragma used-
#endif
// Standard Input/Output functions
#include <stdio.h>
// Declare your global variables here
                
unsigned char command=0;//символ команды
unsigned char engine=0;//байт на движки
unsigned char keys=0;//концевики    
unsigned char keys0=0;//антидребезг
unsigned char encode=0;//показания энкодера
unsigned char encode0=0;//антидребезг 
unsigned char encodeOld=0;//старое значение для определения направления    
unsigned char direction=0;//направления вращения по показаниям энкодера
unsigned char mode=0;//0-жду команду/ выполняю команду - 1      
//unsigned char command_table[27]={0b00000000,0b0000000l,0b00000010,0b00000100,0b0000010l,0b00000110,0b00001000,0b0000100l,0b00001010,0b00010000,0b0001000l,0b00010010,0b00010100,0b0001010l,0b00010110,0b00011000,0b0001100l,0b00011010,0b00100000,0b0010000l,0b00100010,0b00100100,0b0010010l,0b00100110,0b00101000,0b0010100l,0b00101010};
unsigned char command_table[27]={0x00,0x01,0x02,0x04,0x05,0x06,0x08,0x09,0x0a,0x10,0x11,0x12,0x14,0x15,0x16,0x18,0x19,0x1a,0x20,0x21,0x22,0x24,0x25,0x26,0x28,0x29,0x2a};
unsigned char validCommand(unsigned char c){
        int i;
        for(i=0;i<27;i++){
                if(c==command_table[i]){return command_table[i];}
        }                                            
        return 255;
}
  
void main(void)
{
// Declare your local variables here
// Reset Source checking
if (MCUCSR & 1)
   {
   // Power-on Reset
   MCUCSR&=0xE0;
   // Place your code here
   }
else if (MCUCSR & 2)
   {
   // External Reset
   MCUCSR&=0xE0;
   // Place your code here
   }
else if (MCUCSR & 4)
   {
   // Brown-Out Reset
   MCUCSR&=0xE0;
   // Place your code here
   }
else if (MCUCSR & 8)
   {
   // Watchdog Reset
   MCUCSR&=0xE0;
   // Place your code here
   }
else if (MCUCSR & 0x10)
   {
   // JTAG Reset
   MCUCSR&=0xE0;
   // Place your code 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=0xff;
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=0xff;
// Port C 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 
PORTC=0xff;
DDRC=0x00;
// 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: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// 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=0x00;
TCCR1B=0x00;
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
// INT2: Off
MCUCR=0x00;
MCUCSR=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: 19200
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x31;//0x26;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// Global enable interrupts
#asm("sei")
                 
printf("OK BOUNTY\r\n");
while (1)
      {  
      // Place your code here     
      //независимо от режима надо снять показания концевиков и энкодера
      //причём для энкодера надо посчитать тип состояния было-стало
      //если данные с портов одинаковы за 50мс значит это данные, а не дребезг
      if(keys0 == ~PINA){keys= ~PINA;}
      keys0= ~PINA;    
printf("~PINA = %i\r\n",keys0);
        
      if(encode0==PINC){encode=PINC;}
      encode0=PINC;
printf("PINC = %i\r\n",encode0);
      //определить тип показаний энкодера неизменны, справа на лево, слева на право
      direction=0;                                                 
      if((encode & 0x01) != (encodeOld & 0x01)){
        //изменение было
        direction |= 0x03;
      }  
                                       
      if((encode & 0x02) != (encodeOld & 0x02)){
        //изменение было
        direction |= 0x0c;             
//printf("0x0c\r\n");
      }                                       
      
      if((encode & 0x04) != (encodeOld & 0x04)){
        //изменение было
        direction |= 0x30;
      }                   
      direction &= command;
printf("Encode %i EncodeOld %i Dir %i\r\n",encode, encodeOld, direction);    
      encodeOld = encode;
  
printf("Command = %i mode=%i encode=%i\r\n", command,mode,encode);
      
        if(mode==0){
                command=getchar();
                //проверить валидность команды   
                command-=0x31;//временный костыль, чтобы команды были от 0, а в протеусе терминалом получается от 31 ("1")
                //если команда валидна, перейти в mode=1      
                if(validCommand(command)!=255){
putsf("valid \r\n");
                        mode=1;
                }               
        }
        
        if(mode==1){    
putsf("mode=1 \r\n");       
                //рассчитать команду на основании концевиков
printf("aC %i \r\n",command);
                command = (keys & command) ^ command;
printf("bK %i C %i\r\n", keys, command);
                //рассчитать engine на основании энкодеров
                engine = (direction & command) ^ command;
printf("engine = (%i & %i) ^ %i = %i\r\n", direction,command,command, engine);
        
                PORTB=engine;    
                //если все движки встряли, то
                if((engine & 0x3f) == 0){
                        putchar1(command+0x31);     
                        mode=0;     
                        encode0=PINC;
                        encode=encode0;
                        encodeOld=encode0;
printf("command = %i mode=%i\r\n", command,mode);
                }
        } 
        
        delay_ms(50);//задержка между циклами обработки
      };
}