/*****************************************************

Projekt : Kompaktní snímač operativní teploty
Date    : 23.5.2012
Author  : Ing. Jan Janečka

Chip type               : ATmega128
Program type            : Application
AVR Core Clock frequency: 14,745600 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*****************************************************/
  
#include <mega128.h>

// Standard Input/Output functions
#include <stdio.h>
#include <delay.h>
#include <math.h> 
#include <stdlib.h> 
#include <string.h>

// DS1307 Real Time Clock functions
#include <ds1307.h>

// Alphanumeric LCD Module functions
#include <alcd.h>

#asm
   .equ __i2c_port=0x12 ;PORTD
   .equ __sda_bit=1
   .equ __scl_bit=0
#endasm
#include <i2c.h>
                                  
#ifndef RXB8
#define RXB8 1
#endif

#ifndef TXB8
#define TXB8 0
#endif

#ifndef UPE
#define UPE 2
#endif

#ifndef DOR
#define DOR 3
#endif

#ifndef FE
#define FE 4
#endif

#ifndef UDRE
#define UDRE 5
#endif

#ifndef RXC
#define RXC 7
#endif

#define FRAMING_ERROR (1<<FE)
#define PARITY_ERROR (1<<UPE)
#define DATA_OVERRUN (1<<DOR)
#define DATA_REGISTER_EMPTY (1<<UDRE)
#define RX_COMPLETE (1<<RXC)

#define _BV	(bit)  (1 << (bit))
#define bit_is_clear    (sfr,bit ) (!(_SFR_BYTE(sfr) & _BV(bit)))
#define bit_is_set	(sfr,bit )	(_SFR_BYTE(sfr) & _BV(bit))


#define setb(port,pin)    port |= 1<<pin    //nastav bit
#define clrb(port,pin)    port &= ~(1<<pin) //nuluj bit
#define negb(port,pin)    port ^= 1<<pin    //neguj bit

#define KEY_PRESSED !(PINB&=0x80) 
#define KEY_OPEN (PINB&=0x80)

#define KEY_PRESSED_PLUS !(PINB&=0x40) 
#define KEY_OPEN_PLUS (PINB&=0x40)

#define KEY_PRESSED_OK !(PINB&=0x20) 
#define KEY_OPEN_OK (PINB&=0x20)

#define KEY_PRESSED_MINUS !(PINB&=0x10) 
#define KEY_OPEN_MINUS (PINB&=0x10)

unsigned int TMR1H;   
unsigned int TMR1L;   
unsigned int TMR2;
unsigned int ADC_A1;
unsigned int ADC_A2;

unsigned int TSHTH;
unsigned int TSHTL;
unsigned int TSHT2;

unsigned int HSHTH;
unsigned int HSHTL;
unsigned int HSHT2;
        
//--------------------------------------------------------
// Definice proměnných
float counter_0=0, frekvence=0;
char lcd_buffer[33];
char lcd_buffer_Date[33];
char lcd_buffer_Time[33];
char lcd_buffer_Frek[33];
char lcd_buffer_Toper[33];
char lcd_buffer_Tokoli[33];
char lcd_buffer_Trad[33];
char lcd_buffer_Proudeni[33];
char lcd_buffer_NapetiTokoli[33];
char lcd_buffer_NapetiTrad[33];
char lcd_buffer_ADC_A2[33];
char lcd_buffer_ADC_A1[33];
char lcd_buffer_SHTTep[33];
char lcd_buffer_SHTHum[33];
char lcd_buffer_ADC_SHT[33];
char lcd_buffer_Hodiny[33];
char lcd_buffer_Minuty[33];
char lcd_buffer_Sekundy[33];
char lcd_buffer_Datum[33];
char lcd_buffer_Mesic[33];
char lcd_buffer_Rok[33];
char Frekstring[33];
char Tradstring[33];
char Tokolistring[33];
char Proudenistring[33];
char NapetiA2string[33];
char NapetiA1string[33];
char SHTTeplotastring[33];
char SHTHumiditystring[33];
char Tostring[33];
char uart_buffer[63];
int citac=0, nacteno;
char znak;
float napeti_A1, napeti_A2;
float A , B , G=180.50;
unsigned char h, m, s; 
unsigned char date, month, year; 
char old; 
float Trad;
float Tokoli;
float To;
float konstADC=32768;
float Vref=2.0480;    
float proudeni;
int vyber=10;
float AP;
unsigned int konst_Rok_Max = 50;
unsigned int konst_Rok_Min = 0;
float SHT_Teplota=0.0;
unsigned int SHT_Teplota_ADC=0;  
float SHT_Humidity=0.0;
unsigned int SHT_Humidity_ADC=0;
float konst_A= -46.85;
float konst_B=175.72;
float konst_C= -6.0;
float konst_D=125.0;
float konstADCSHT=65536;  
unsigned int ToDAC;
float konst=65535; 
float frekcpu=14745600;
int k;
 
// USART0 Receiver buffer
#define RX_BUFFER_SIZE0 8
char rx_buffer0[RX_BUFFER_SIZE0];

#if RX_BUFFER_SIZE0 <= 256
unsigned char rx_wr_index0,rx_rd_index0,rx_counter0;
#else
unsigned int rx_wr_index0,rx_rd_index0,rx_counter0;
#endif

// This flag is set on USART0 Receiver buffer overflow
bit rx_buffer_overflow0;

// USART0 Receiver interrupt service routine
interrupt [USART0_RXC] void usart0_rx_isr(void)
{
char status,data;
status=UCSR0A;
data=UDR0;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
   {
   rx_buffer0[rx_wr_index0++]=data;
#if RX_BUFFER_SIZE0 == 256
   // special case for receiver buffer size=256
   if (++rx_counter0 == 0)
      {
#else
   if (rx_wr_index0 == RX_BUFFER_SIZE0) rx_wr_index0=0;
   if (++rx_counter0 == RX_BUFFER_SIZE0)
      {
      rx_counter0=0;
#endif
      rx_buffer_overflow0=1;
      }
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART0 Receiver buffer
#define _ALTERNATE_GETCHAR_
#pragma used+
char getchar(void)
{
char data;
while (rx_counter0==0);
data=rx_buffer0[rx_rd_index0++];
#if RX_BUFFER_SIZE0 != 256
if (rx_rd_index0 == RX_BUFFER_SIZE0) rx_rd_index0=0;
#endif
#asm("cli")
--rx_counter0;
#asm("sei")
return data;
}
#pragma used-
#endif

// USART0 Transmitter buffer
#define TX_BUFFER_SIZE0 64
char tx_buffer0[TX_BUFFER_SIZE0];

#if TX_BUFFER_SIZE0 <= 256
unsigned char tx_wr_index0,tx_rd_index0,tx_counter0;
#else
unsigned int tx_wr_index0,tx_rd_index0,tx_counter0;
#endif

// USART0 Transmitter interrupt service routine
interrupt [USART0_TXC] void usart0_tx_isr(void)
{
if (tx_counter0)
   {
   --tx_counter0;
   UDR0=tx_buffer0[tx_rd_index0++];
#if TX_BUFFER_SIZE0 != 256
   if (tx_rd_index0 == TX_BUFFER_SIZE0) tx_rd_index0=0;
#endif
   }
}

#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART0 Transmitter buffer
#define _ALTERNATE_PUTCHAR_
#pragma used+
void putchar(char c)
{
while (tx_counter0 == TX_BUFFER_SIZE0);
#asm("cli")
if (tx_counter0 || ((UCSR0A & DATA_REGISTER_EMPTY)==0))
   {
   tx_buffer0[tx_wr_index0++]=c;
#if TX_BUFFER_SIZE0 != 256
   if (tx_wr_index0 == TX_BUFFER_SIZE0) tx_wr_index0=0;
#endif
   ++tx_counter0;
   }
else
   UDR0=c;
#asm("sei")
}
#pragma used-
#endif

// Timer1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
citac++ ;
}

// Timer2 output compare interrupt service routine
interrupt [TIM2_COMP] void timer2_comp_isr(void)
{
counter_0 = TCNT1;
nacteno=citac;
citac=0;
TCNT1=0x0000;
TCNT2=0x00;
}

void ADS110A1_Write(unsigned char outdat)   
{   
    i2c_start();   
    i2c_write(0x92);
    i2c_write(outdat);   
    i2c_stop();   
}   
   
void ADS110A1_Read(void)   
{   
    i2c_start();  
    i2c_write(0x93);   
    TMR1H=i2c_read(1);       
    TMR1L=i2c_read(1);       
    TMR2=i2c_read(0);
    i2c_stop();   
}   
   

void ADS110A2_Write(unsigned char outdat)   
{   
    i2c_start();   
    i2c_write(0x94);
    i2c_write(outdat);   
    i2c_stop();   
}   
   
void ADS110A2_Read(void)   
{   
    i2c_start();  
    i2c_write(0x95);   
    TMR1H=i2c_read(1);       
    TMR1L=i2c_read(1);       
    TMR2=i2c_read(0);       
    i2c_stop();   
}   


void SHT25_Temp(void)   
{   
    i2c_start();   
    i2c_write(0x80);
    i2c_write(0xE3);   
    i2c_start();      
    i2c_write(0x81);
    TSHTH=i2c_read(1);       
    TSHTL=i2c_read(1);       
    TSHT2=i2c_read(0);       
    i2c_stop();   
}


void SHT25_Hum(void)   
{   
    i2c_start();   
    i2c_write(0x80);
    i2c_write(0xE5);   
    i2c_start();      
    i2c_write(0x81);
    HSHTH=i2c_read(1);       
    HSHTL=i2c_read(1);       
    HSHT2=i2c_read(0);       
    i2c_stop();   
}

void DAC(unsigned int data)   
{ 
    char lVout = data;
	char hVout = (data>>8) & 0x0F;
    i2c_start();   
    i2c_write(0xC0);
    i2c_write(hVout);   
    i2c_write(lVout);
    i2c_write(hVout);   
    i2c_write(lVout);   
    i2c_stop();   
}

void main(void)
{
delay_ms(500);

PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
PORTE=0x00;
DDRE=0x00;
PORTF=0x00;
DDRF=0x00;
PORTG=0x00;
DDRG=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
ASSR=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 16000,000 kHz
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x01;
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: T2 pin Rising Edge
// Clock value: Timer2 Stopped
// Mode: CTC top=OCR2
// OC2 output: Disconnected
TCCR2=0x0F;
TCNT2=0x00;
OCR2=0x01;

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

ETIMSK=0x00;

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

// 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 disabled
ADCSRA=0x00;
                    
// SPI initialization
// SPI disabled
SPCR=0x00;

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

// I2C Bus initialization
i2c_init();

// DS1307 Real Time Clock initialization
// Square wave output on pin SQW/OUT: Off
// SQW/OUT pin state: 0
rtc_init(0,0,0);

//rtc_set_time(17,23,0); 
//rtc_set_date(24,06,12); 
         
// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 6
// D6 - PORTC Bit 7
// D7 - PORTC Bit 5
// Characters/line: 16

lcd_init(16);

// Global enable interrupts
#asm("sei")
  
 lcd_gotoxy(0,0);
 lcd_puts("Kompaktni snimac"); 
 lcd_gotoxy(0,1);
 lcd_puts("---Operativni---");
 lcd_gotoxy(0,2);
 lcd_puts("----teploty-----");
 for(k=20;k>0;k--)
 {
 sprintf(lcd_buffer," Cekejte... %d   ",k);
 lcd_gotoxy(0,3);
 lcd_puts(lcd_buffer);
  delay_ms(1000); 
 } 
 
  lcd_clear();
  
  ADS110A1_Write(0x8C);   
  delay_ms(5);  
  ADS110A2_Write(0x8C);   
  delay_ms(5);  
  
     
while (1)
      {
          
      float pom_frek=0, pom_ADC_A1=0 ; 
      int i=0, j=0;
       
        if  (KEY_PRESSED | KEY_PRESSED_OK)
       {
      	vyber=vyber+1;
       	 if (vyber>10)
         {
         vyber=1;
         delay_ms(100);
         }
       }
      
         if (counter_0 || nacteno > 0)
         {
         for(i=0;i<20;i++)
             if(i<19)
               {
                if (frekvence>150 & frekvence<820)   // pridano overit!!!
                {
                frekvence=(frekcpu/(counter_0+(nacteno*konst)))*2;
                pom_frek=frekvence+pom_frek ;
                delay_ms(5);
                }
                }                                                       
                if(i>18)
             {
               frekvence=(pom_frek/19);
             }
                
              ftoa(frekvence,0,Frekstring);
         
           if (frekvence<180)
           {
            proudeni=0.00;
           } 
           
           if (frekvence>800)
           {
            proudeni=6.54;
           }
           
           if (frekvence>180 & frekvence<800)
           {
           proudeni=3.80242-0.0706151*frekvence + 0.000503032*frekvence*frekvence-0.00000177436*frekvence*frekvence*frekvence + 0.00000000337857*frekvence*frekvence*frekvence*frekvence-0.00000000000326827*frekvence*frekvence*frekvence*frekvence*frekvence + 0.00000000000000126988*frekvence*frekvence*frekvence*frekvence*frekvence*frekvence;
           }
           ftoa(proudeni,2,Proudenistring);
         }                
           
       // Reálný čas z DS1307   
       rtc_get_time(&h, &m, &s); 
       rtc_get_date(&date, &month, &year); 
             
       // cteni ADS1110 A1 - radiační teplota
       
         for(j=0;j<10;j++)
             if(j<9)
               {
                ADS110A1_Read();
                ADC_A1=(TMR1H*256)+TMR1L;
                pom_ADC_A1=ADC_A1+pom_ADC_A1;
                delay_ms(10);
                }                                                       
                if(j>8)
             {
               ADC_A1=(pom_ADC_A1/9);
             }
       
        napeti_A1=(ADC_A1/konstADC)*Vref;
        napeti_A1=napeti_A1*2.5; 
        
        //Radiační teplota pro snímač 60°C
        // Trad= -0.002603*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1 + 0.04802*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1 - 0.38431*napeti_A1*napeti_A1*napeti_A1*napeti_A1 + 1.8498*napeti_A1*napeti_A1*napeti_A1 - 6.835*napeti_A1*napeti_A1 + 32.71*napeti_A1 - 26.75;   
         
        //Radiační snímač pro 180°C
         Trad= -0.477130*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1 + 8.48270*napeti_A1*napeti_A1*napeti_A1*napeti_A1*napeti_A1 - 61.41728*napeti_A1*napeti_A1*napeti_A1*napeti_A1 + 232.9302*napeti_A1*napeti_A1*napeti_A1 - 496.803*napeti_A1*napeti_A1 + 624.76*napeti_A1 - 306.35;
         Trad=Trad-2.0;   
        
         ftoa(napeti_A1,2,NapetiA1string);
         ftoa(Trad,1,Tradstring);
         
        
         // cteni ADS1110 A2 - okolní teplota
         
        ADS110A2_Read();
        ADC_A2=(TMR1H*256)+TMR1L;   
         
        napeti_A2=(ADC_A2/konstADC)*Vref;
        napeti_A2=napeti_A2*2.5; 
        A = -1.523563 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2 + 20.52003 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2 - 112.09588 * napeti_A2 * napeti_A2 * napeti_A2 * napeti_A2;
        B = 319.2295 * napeti_A2 * napeti_A2 * napeti_A2 - 508.327 * napeti_A2 * napeti_A2 + 475.52 * napeti_A2 - G;
        
        Tokoli = A + B -1.4;
                    
        ftoa(napeti_A2,2,NapetiA2string);
        ftoa(Tokoli,1,Tokolistring);
                 
      // Výpočet operativní teploty  
       // To = A . ta + (1 - A) tr  
        
       AP=0.34584+1.65437*proudeni-3.32182*proudeni*proudeni+3.70809*proudeni*proudeni*proudeni-2.34848*proudeni*proudeni*proudeni*proudeni+0.870849*proudeni*proudeni*proudeni*proudeni*proudeni-0.187175*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni+0.0215784*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni-0.00103157*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni*proudeni;;
       To=AP*Tokoli+(1.0-AP)*Trad;
        
       ftoa(To,1,Tostring);
       SHT25_Temp();
       SHT_Teplota_ADC =(TSHTH*256)+TSHTL;
       SHT_Teplota=(konst_A+(konst_B*(SHT_Teplota_ADC/konstADCSHT)))-3.5;
       ftoa(SHT_Teplota,1,SHTTeplotastring);
      
       SHT25_Hum();
       SHT_Humidity_ADC =(HSHTH*256)+HSHTL;
       SHT_Humidity=(konst_C+(konst_D*(SHT_Humidity_ADC/konstADCSHT)));
       ftoa(SHT_Humidity,1,SHTHumiditystring); 
      
      // Analogový výstup operativní teploty
       ToDAC=102.4*To;
       DAC(ToDAC);
                
     if (vyber==10)
      {
       if (s != old) 
       {  
           sprintf(lcd_buffer_Date, "Date %02d/%02d/20%02d ", date, month, year); 
           lcd_gotoxy(0, 0);
           lcd_puts(lcd_buffer_Date); 
          
           old = s; 
          
           sprintf(lcd_buffer_Time, "Time %02d:%02d:%02d   ", h, m, s); 
           lcd_gotoxy(0, 1);
           lcd_puts(lcd_buffer_Time); 
       } 
        
            lcd_gotoxy(0,2);
            lcd_puts("Operativni tepl ");
       
            sprintf(lcd_buffer_Toper,"   To: %s     ",Tostring);                                                  
            lcd_gotoxy(0,3); 
            lcd_puts(lcd_buffer_Toper); 
      }       
        
        
        if (vyber==1)
      {
        sprintf(lcd_buffer_Trad,"Trad: %s C    ",Tradstring);                                                  
        lcd_gotoxy(0,0); 
        lcd_puts(lcd_buffer_Trad);
        sprintf(lcd_buffer_Tokoli,"Tokol: %s C   ",SHTTeplotastring);                                                  
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Tokoli); 
        sprintf(lcd_buffer_Proudeni,"Proud: %s m/s ",Proudenistring);                                                  
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Proudeni);
        sprintf(lcd_buffer_SHTTep,"Vlhkost: %s %% ",SHTHumiditystring);                                                  
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_SHTTep);
      }
      
             
        
        if (vyber==2)
      {
      
      
        sprintf(lcd_buffer_Frek,"F: %s Hz   ",Frekstring);                                                  
        lcd_gotoxy(0,0); 
        lcd_puts(lcd_buffer_Frek);
        sprintf(lcd_buffer_NapetiTokoli,"Napeti_ok:%s V ",NapetiA2string); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_NapetiTokoli);
        sprintf(lcd_buffer_NapetiTrad,"Napeti_ra:%s V",NapetiA1string); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_NapetiTrad);
        sprintf(lcd_buffer_SHTHum,"Teplokoli: %s ",Tokolistring);                                                  
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_SHTHum);
      }
        
       if (vyber==3)
      {
        sprintf(lcd_buffer_ADC_A2,"ADC_A2: %d    ",ADC_A2);                                                  
        lcd_gotoxy(0,0); 
        lcd_puts(lcd_buffer_ADC_A2);
        
        sprintf(lcd_buffer_ADC_A1,"ADC_A1: %d    ",ADC_A1); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_ADC_A1);
        
        sprintf(lcd_buffer_ADC_SHT,"SHT_T: %d    ",SHT_Teplota_ADC); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_ADC_SHT);
        
        sprintf(lcd_buffer_ADC_SHT,"SHT_H: %d    ",SHT_Humidity_ADC); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_ADC_SHT);
      } 
       
      
      
      if (vyber==4)
        { 
        
        if (KEY_PRESSED_PLUS & h<23)
        {
        h=h+1;
        delay_ms(1);
        rtc_set_time(h,m,s);
        
        }
        
        if (KEY_PRESSED_MINUS & h>0)
        {
        h=h-1;
        delay_ms(1);
        rtc_set_time(h,m,s); 
        }
        
                                                  
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni casu");
        
        sprintf(lcd_buffer_Hodiny,"Hodiny: %d <    ",h); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Hodiny);
        
        sprintf(lcd_buffer_Minuty,"Minuty: %d       ",m); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Minuty);
        
        sprintf(lcd_buffer_Sekundy,"Sekundy: %d     ",s); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Sekundy);
        } 
        
         if (vyber==5)
        { 
        
        if (KEY_PRESSED_PLUS & m<59)
        {
        m=m+1;
        delay_ms(1);
        rtc_set_time(h,m,s);
        
        }
        
        if (KEY_PRESSED_MINUS & m>0)
        {
        m=m-1;
        delay_ms(1);
        rtc_set_time(h,m,s); 
        }
                                                          
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni casu");
        
        sprintf(lcd_buffer_Hodiny,"Hodiny: %d      ",h); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Hodiny);
        
        sprintf(lcd_buffer_Minuty,"Minuty: %d <     ",m); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Minuty);
        
        sprintf(lcd_buffer_Sekundy,"Sekundy: %d     ",s); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Sekundy);
        
      } 
       
      if (vyber==6)
        { 
        
        if (KEY_PRESSED_PLUS & s<59)
        {
        s=s+1;
        delay_ms(1);
        rtc_set_time(h,m,s);
        
        }
        
        if (KEY_PRESSED_MINUS & s>0)
        {
        s=s-1;
        delay_ms(1);
        rtc_set_time(h,m,s); 
        }
        
                                                  
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni casu");
        
        sprintf(lcd_buffer_Hodiny,"Hodiny: %d      ",h); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Hodiny);
        
        sprintf(lcd_buffer_Minuty,"Minuty: %d       ",m); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Minuty);
        
        sprintf(lcd_buffer_Sekundy,"Sekundy: %d <   ",s); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Sekundy);
        
      } 
         
      if (vyber==7)
        { 
        
        if (KEY_PRESSED_PLUS & date<30)
        {
        date=date+1;
        delay_ms(1);
        rtc_set_date(date,month,year);
        
        }
        
        if (KEY_PRESSED_MINUS & date>1)
        {
        date=date-1;
        delay_ms(1);
        rtc_set_date(date,month,year); 
        }
        
                                                  
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni data");
        
        sprintf(lcd_buffer_Datum,"Datum: %d <     ",date); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Datum);
        
        sprintf(lcd_buffer_Mesic,"Mesic: %d       ",month); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Mesic);
        
        sprintf(lcd_buffer_Rok,"Rok: 20%d       ",year); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Rok);
        
      } 
       
       if (vyber==8)
        { 
        
        if (KEY_PRESSED_PLUS & month<11)
        {
        month=month+1;
        delay_ms(1);
        rtc_set_date(date,month,year);
        
        }
        
        if (KEY_PRESSED_MINUS & month>1)
        {
        month=month-1;
        delay_ms(1);
        rtc_set_date(date,month,year); 
        }
        
                                                  
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni data");
        
        sprintf(lcd_buffer_Datum,"Datum: %d       ",date); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Datum);
        
        sprintf(lcd_buffer_Mesic,"Mesic: %d <      ",month); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Mesic);
        
        sprintf(lcd_buffer_Rok,"Rok: 20%d       ",year); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Rok);
        
      } 
      
      if (vyber==9)
        { 
        
        if (KEY_PRESSED_PLUS & year<konst_Rok_Max)
        {
        year=year+1;
        delay_ms(1);
        rtc_set_date(date,month,year);
        
        }
        
        if (KEY_PRESSED_MINUS & month>konst_Rok_Min)
        {
        year=year-1;
        delay_ms(1);
        rtc_set_date(date,month,year); 
        }
        
                                                  
        lcd_gotoxy(0,0); 
        lcd_puts("Nastaveni data");
        
        sprintf(lcd_buffer_Datum,"Datum: %d       ",date); 
        lcd_gotoxy(0,1); 
        lcd_puts(lcd_buffer_Datum);
        
        sprintf(lcd_buffer_Mesic,"Mesic: %d        ",month); 
        lcd_gotoxy(0,2); 
        lcd_puts(lcd_buffer_Mesic);
        
        sprintf(lcd_buffer_Rok,"Rok: 20%d <     ",year); 
        lcd_gotoxy(0,3); 
        lcd_puts(lcd_buffer_Rok);
        
      } 
                      
        if(rx_counter0)
        {
          znak=getchar();
          if(znak == 'a')
          {
           sprintf(uart_buffer,"Data:To:%s:Tr:%s:Tok:%s:w:%s:Tok2:%s:Hum:%s",Tostring,Tradstring,Tokolistring,Proudenistring,SHTTeplotastring,SHTHumiditystring);
           puts(uart_buffer);
          }
        }
      }
}
