/*****************************************************
This program was produced by the
CodeWizardAVR V1.24.7d Standard
Automatic Program Generator
© Copyright 1998-2005 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
e-mail:office@hpinfotech.com

Project : 
Version : 
Date    : 27.5.2009
Author  : Marek Barka                               
Company :                                
Comments: 


Chip type           : ATmega8
Program type        : Application
Clock frequency     : 1,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 256
*****************************************************/
 
// Declare your global variables here
unsigned char data; 
int Test; 
int prep,e,pocet; 
int d[16];

// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <mega8.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)
                      
// Write a character to the USART Transmitter
#ifndef _DEBUG_TERMINAL_IO_
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while ((UCSRA & DATA_REGISTER_EMPTY)==0);
UDR=c;
}
#pragma used-
#endif
    
   
                    
char SPI(int a) {  // send character over SPI
	//char data = 0;
	SPDR = a; 
	while((SPSR & 0x80) == 0);
	//data = SPDR;
	return (data);
}

void main(void)
{

// Declare your local variables here

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=Out Func2=Out Func1=In Func0=In 
// State7=T State6=T State5=0 State4=T State3=0 State2=0 State1=T State0=T 
PORTB=0b00000110;                                                               //aktivovanie CS =1
DDRB=0b00101110;

// 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=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=0x10; 

// 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: 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
MCUCR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, Odd Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Sync. Master UCPOL=1
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0x00;
UCSRC=0xF7;
UBRRL=0x59;
UDR=d[e];

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

PORTB=0b00000100; 
SPCR=0b01010011; 
SPI(0b11000100);                                                         //
SPI(0b00001010);
PORTB=0b00000110;
  
delay_us(50); 
prep=1;
e=0;
            
while(01)
 {
PORTB=0b00000100;      
SPI(0b00000000);
Test = SPDR;
if (Test == 0b11000000) 
        {
        switch(prep)
                {
                case 1: e++;
                        SPI(0b00000000);
                        d[e] = SPDR;
                        PORTB=0b00000110;
                        if (d[e] == 0b00001101)
                                {
                                prep=2;
                                pocet=e;
                                } 
                        break;
                        
                case 2: e++;
                        SPI(0b00000000);
                        d[e] = SPDR;
                        PORTB=0b00000110;
                        if (d[e] == 0b00001010)
                                {
                                for (e=0; e < pocet; e++)              //Konverze ASCI na ScanCode
                                        {   
                                        switch(d[e])                   
                                                {
                                                case 0x30:
                                                        d[e]=0x70;     //Znak "0"
                                                        break;
                                                case 0x31:
                                                        d[e]=0x69;     //Znak "1"
                                                        break;
                                                case 0x32:
                                                        d[e]=0x72;     //Znak "2"
                                                        break;
                                                case 0x33:
                                                        d[e]=0x7A;     //Znak "3"
                                                        break;
                                                case 0x34:
                                                        d[e]=0x6B;     //Znak "4"
                                                        break;
                                                case 0x35:
                                                        d[e]=0x73;     //Znak "5"
                                                        break;
                                                case 0x36:
                                                        d[e]=0x74;     //Znak "6"
                                                        break;
                                                case 0x37:
                                                        d[e]=0x6C;     //Znak "7"
                                                        break;
                                                case 0x38:
                                                        d[e]=0x75;     //Znak "8"
                                                        break;
                                                case 0x39:
                                                        d[e]=0x7D;     //Znak "9"
                                                        break;
                                                case 0x2D:
                                                        d[e]=0x4E;     //Znak "-"
                                                        break;
                                                case 0x2F:
                                                        d[e]=0x4A;     //Znak "/"
                                                        break;
                                                case 0x2E:
                                                        d[e]=0x49;     //Znak "."
                                                        break;
                                                }
                                         }
                                UCSRB=0x08;                                                          
                                for (e=0; e < pocet; e++)             //Odeslání dat na PS-2 rozhraní
                                        {
                                        UDR=d[e];                     
                                        UCSRA=0x00;
                                        while ((UCSRA & 0x20)== 0x00);
                                        delay_us(1000);
                                        UDR=0xF0;                     //Znak vypuštění klávesy
                                        UCSRA=0x00;
                                        while ((UCSRA & 0x20)== 0x00);
                                        delay_us(1000);
                                        UDR=d[e];
                                        UCSRA=0x00;
                                        while ((UCSRA & 0x20)== 0x00);
                                        delay_us(1000);
                                        }                
                                UDR=0x5A;                             //Znak klávesy ENTER
                                UCSRA=0x00;
                                while ((UCSRA & 0x20)== 0x00);
                                delay_us(1000);
                                UDR=0xF0;
                                UCSRA=0x00;
                                while ((UCSRA & 0x20)== 0x00);
                                delay_us(1000);
                                UDR=0x5A;
                                UCSRA=0x00;
                                while ((UCSRA & 0x20)== 0x00);
                                delay_us(1000);        
                                UCSRB=0x00;
                                pocet=0;
                                e=0;
                                }
                        prep=1;
                        break; 
                               
                } 
        }

        PORTB=0b00000110;
        delay_us(100);
            
}




}
