/**** Analog to Digital Converter ****/
#include "main.h"

#define  LED1  PIN_A2
#define  LED2  PIN_A3               // LEDky
#define  LED3  PIN_A5
#define  CIDLO 8                    // Port A/D prevodniku
#define  TAD   8                    // Doba na prevod jednoho bitu

void main()
{
   int8 t;                          // Promenna pro nacteni hodnoty z potenciometru
   
   setup_adc_ports(AN0);            // Povolime analogovy vstup 0
   setup_adc(ADC_CLOCK_DIV_8);      // Delicka hodin pro prevodnik
// setup_psp(PSP_DISABLED);         // Tento HW ma pouze PIC16F877A
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   set_adc_channel(CIDLO);          // Pripojime cidlo na A/D prevodnik
   delay_us(20);                    // Pockame na nabiti kapacit po prepnuti
   //t=read_adc();                    // Prvni precteni cidla
   
   while(TRUE){
   t=read_adc();                    // Prvni precteni cidla   
     
      
      if(t>00&&t<50){
         output_high(LED1);
         //rozsvit modrou
      }
      else if(t>50&&t<80){
         output_high (LED1);   // Zhasneme LED
         delay_us(100);
         output_low (LED1);    // Rozsvitime LED
         delay_us(100);
         output_low (LED2);    // Rozsvitime LED
         delay_us(100);
         output_high (LED2);   // Zhasneme LED
         delay_us(100);
         //rozsvit modrou+zelenou
      }
      else if(t>80&&t<120){
         //output_high(LED2);
         //rozvit zelenou
         output_low (LED2);    // Rozsvitime LED
         delay_us(100);
         output_high (LED2);   // Zhasneme LED
         delay_us(100);
      }
      else if(t>120&&t<160){
         output_high (LED3);   // Zhasneme LED
         delay_us(100);
         output_low (LED3);    // Rozsvitime LED
         delay_us(100);
         output_low (LED2);    // Rozsvitime LED
         delay_us(100);
         output_high (LED2);   // Zhasneme LED
         delay_us(100);
         //rozsvit zelenou+cervenou
      }
      else if(t>160){
         output_high(LED3);
         //rozsvit cervenou
      }
     
   }}