#include ".\main.h"
//#include <LCD.C>

#define LED1 PIN_C6  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED2 PIN_C5  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED3 PIN_C4  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5
#define LED4 PIN_D3  //CHANGE PIN_XX TO YOUR LED PIN NUMBER, EX: PIN_A5


#int_EXT
void EXT_isr(void)
{
   output_toggle(LED3);
}

//#define LCD_TYPE 1
//#include <lcd.c>
void main()
{
unsigned int setpoint;
unsigned int napeti;   
unsigned int16 plneni=0;

/*   lcd_init();

   lcd_putc("\fReady...\n");

   while (TRUE) {
      k=kbd_getc();
      if(k!=0)
        if(k=='*')
          lcd_putc('\f');
        else
          lcd_putc(k);
   }
*/

   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_32);
   setup_spi(SPI_SS_DISABLED);
   setup_timer_0(RTCC_EXT_L_TO_H|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_1,255,1);
   setup_ccp1(CCP_PWM);
   setup_ccp2(CCP_PWM);
   setup_comparator(NC_NC_NC_NC);// This device COMP currently not supported by the PICWizard
   setup_oscillator(OSC_8MHZ);

   set_pwm1_duty(0);
   set_pwm2_duty(0);


   output_high(LED1);
   output_high(LED2);
   output_high(LED3);
   output_high(LED4);

   ext_int_edge( L_TO_H );   // Sets up EXT
   enable_interrupts(INT_EXT);
   enable_interrupts(GLOBAL);

   //   lcd_init();
   
setpoint = 43;   
   
   while(true)
   {
      set_adc_channel(1);
      delay_us(100);
      napeti = read_adc();

      if (napeti <= setpoint) 
      {
         if (plneni < 300) plneni++;
         output_low(LED2);
      }
      else
      {
         if (plneni > 0) plneni--;
         output_high(LED2);
      }

         set_pwm1_duty(plneni);
         set_pwm2_duty(1023-plneni);

      output_toggle(LED1);
      delay_ms(10);
    }

}