/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 3.4.2013
Author  : NeVaDa
Company : 
Comments: 


Chip type               : ATmega64L
Program type            : Application
AVR Core Clock frequency: 8,000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*****************************************************/

#include <mega64.h>

#include <delay.h>

#include "t6963c.h"

// Standard Input/Output functions
#include <stdio.h>


    unsigned int sound_speed;           // rychlost zvuku
    unsigned int temperature;           // teplota

//#define ADC_VREF_TYPE 0x00

// Read the AD conversion result
unsigned int read_adc_usonic(unsigned char adc_input)
{
//ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
ADMUX=0xC0;
// Delay needed for the stabilization of the ADC input voltage
delay_us(1);           //10
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}


// Read the AD conversion result
unsigned int read_adc_temperature(unsigned char adc_input)
{
//ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
ADMUX=0xC3;
// Delay needed for the stabilization of the ADC input voltage
delay_us(200);                           //10
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}

// zobrazi integer na pozici ktera je aktualne nastavena, je potreba ji nastavit predem !!!!
void show_int(int temp)
{     
    unsigned int a;
    unsigned char b;  
      a = temp;           
      //LCD_text_xy(0,0);
      b=a/10000;
      LCD_char(b+'0');
      a=a-b*10000;
      b=a/1000;
      LCD_char(b+'0');
      a=a-b*1000;            
      b=a/100;
      LCD_char(b+'0');
      a=a-b*100;
      b=a/10;
      LCD_char(b+'0');
      //LCD_char('.');      
      a=a-b*10;
      b=a;
      LCD_char(b+'0');
}

// zmeri aktualni teplotu a vypocita rychlost sireni zvuku
void compute_temperature(void)
{
    char n;
      temperature = 0;                                       // 2x oversampling   
      for(n=0;n<4;n++) temperature = temperature + read_adc_temperature(3);          
      temperature = temperature * 0.625;            
      sound_speed = 331.6 + 0.61 * (temperature/10);        // rychlost zvuku
}


// Declare your global variables here

void main(void)
{



    unsigned char b;
    unsigned int a,x;

    unsigned char n;
    //float temp;

// 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=Out Func3=In Func2=In Func1=In Func0=In 
// State7=T State6=T State5=T State4=0 State3=T State2=T State1=T State0=T 
PORTB=0x00;
DDRB=0x10;

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

// Port E initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=Out Func0=In 
// State7=T State6=T State5=T State4=T State3=T State2=T State1=0 State0=T 
PORTE=0x00;
DDRE=0x02;

// Port F 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 
PORTF=0x00;
DDRF=0x00;

// Port G initialization
// Func4=In Func3=In Func2=In Func1=In Func0=In 
// State4=T State3=T State2=T State1=T State0=T 
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 8000,000 kHz
// Mode: CTC top=OCR0
// OC0 output: Toggle on compare match
ASSR=0x00;
TCCR0=0x19;
TCNT0=0x00;
OCR0=0x63; // 63           podle mojeho osciloskopu je 40 000 kHz presne 60, teoreticky 63

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// Timer/Counter 3 initialization
// Clock source: System Clock
// Clock value: Timer3 Stopped
// Mode: Normal top=0xFFFF
// OC3A output: Discon.
// OC3B output: Discon.
// OC3C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer3 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR3A=0x00;
TCCR3B=0x00;
TCNT3H=0x00;
TCNT3L=0x00;
ICR3H=0x00;
ICR3L=0x00;
OCR3AH=0x00;
OCR3AL=0x00;
OCR3BH=0x00;
OCR3BL=0x00;
OCR3CH=0x00;
OCR3CL=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
// INT3: Off
// INT4: Off
// INT5: Off
// INT6: Off
// INT7: Off
EICRA=0x00;
EICRB=0x00;
EIMSK=0x00;

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

ETIMSK=0x00;

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: On
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x18;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x33;

// USART1 initialization
// USART1 disabled
UCSR1B=0x00;

// 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: AREF pin
// Only the 8 most significant bits of
// the AD conversion result are used
ADMUX=0xC3;
ADCSRA=0x87;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

///////////////////////////////////////////////////////////////////////////////////




    LCD_init(); 
    LCD_clr_text();
    LCD_clr_gr(0x00);      
    
       
    LCD_mode_set(LCD_MODE_EXOR);    
    LCD_display_mode(LCD_CURS_OFF,LCD_TEXT_ON_GRAPH_ON,1);       
    LCD_diacritics_generate();  
    
     n=0x63;
    
while (1)
      {     
      //a=0;
      //b=0;
      // Place your code here 
  
      //a = read_adc_temperature(4);
      //a=a*2.5;      
      //temperature = temperature * 244;
      
      compute_temperature();
      
      LCD_text_xy(0,0);
      
      a=temperature;
      b=a/10000;
      //LCD_char(b+'0');
      a=a-b*10000;
      b=a/1000;
      //LCD_char(b+'0');
      a=a-b*1000;            
      b=a/100;
      LCD_char(b+'0');
      a=a-b*100;
      b=a/10;
      LCD_char(b+'0');
      LCD_char('.');      
      a=a-b*10;
      b=a;
      LCD_char(b+'0');      
      LCD_char_diacritic(STUPEN);
      LCD_char('C');      
      
      LCD_char(' ');
      
      a=sound_speed;
      b=a/10000;
      //LCD_char(b+'0');
      a=a-b*10000;
      b=a/1000;
      //LCD_char(b+'0');
      a=a-b*1000;            
      b=a/100;
      LCD_char(b+'0');
      a=a-b*100;
      b=a/10;
      LCD_char(b+'0');
      a=a-b*10;
      b=a;
      LCD_char(b+'0'); 
      LCD_string("m/s");

      
      a = read_adc_usonic(0);
      LCD_text_xy(0,2); 
      show_int(a); 
      
        
           

      
      for(x=0;x<500;x++)
      {      
      if(PINE.3==0)
                {
                    n=n+1;
                    while(PINE.3==0);
                }
      if(PINE.4==0)
                {
                    n=n-1;
                    while(PINE.4==0);
                }                
      delay_us(500);
      

      OCR0=n;
      LCD_text_xy(0,10);
      LCD_string("OCR0 = ");
      show_int(n);  
      }   
      //delay_ms(500);
      }  
      
}
