#include "C:\Program Files\PICC\ds1820\ds01.h"
#include<lcd_sam3_b.c>
#define seconds_reg 0
#define minutes_reg 1
#define hours_reg 2
#define day_of_week_reg 3
#define date_reg 4
#define month_reg 5
#define year_reg 6
#define control_reg 7
#define ds1307_nvram_start_address 8
#define control_init_val 0x90
//-------------------------------------------------------
#define sw_up pin_a1
#define sw_down pin_a2
#define sw_set pin_a3
#define relay pin_c0
//--------------------------------------------------------------
#define write_cntrl 0xd0
#define read_cntrl  0xd1
void selectmode(void);
char tobcd(char bin_val);
void set_time(char sec,char min,char hour,char dat1,char month1,char year1);
void read_time(void);
void get_sec(char sec);
void get_min(char min);
void get_hr(char hr);
void get_date(char dat);
void get_month(char month);
void get_year(char year);
char read_byte(char addr);
void write_byte(char addr,char value);
void set_on(void);
void set_OFF(void);
void display_hours(void);
void display_min(void);
void display_sec(void);
char ds1307_regs[7];
VOID read_temp(void);
int8 sec00;
int8 sec10;
int8 min00;
int8 min10;
//=========min on_off ==
int8 min_on;
int8 min_off;
int8 hr00;
int8 hr10;
//=========hr_on_off
int8 hr_on;
int8 hr_off;


int8 dat00;
int8 dat10;

int8 month00;
int8 month10;

int8 year00;
int8 year10;

int1 flg_set_on;
int1 flg_set_oFF;

int8 set_count;
int8 a,b;
#include<ds1307_sam1.c>
byte const num[12] = {'0','1','2','3','4','5','6','7','8','9','*','o'};
void hex_bcd(int8 k);

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_spi(FALSE);
   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);
   //--------------input------
   output_high(sw_up);
   output_high(sw_down);
   output_high(sw_set);
   //--------------output--------
   output_low(relay);

   flg_set_on=0;
   flg_set_off=0;

   set_count=0;
  // min_on=30;
  // min_off=30;
  // hr_on=12;
  // hr_off=6;
    min_on = read_eeprom (0);
    min_off = read_eeprom (2);
    hr_on = read_eeprom (1);
    hr_off = read_eeprom (3);
   
   
   //=======SEC/MIN/HOURS/DATE/MONT/YEAR
   set_time(15,33,13,28,12,04);
   lcd_init();
   lcd_gotoxy(1,0);
   lcd_putc("TIME :");
   while(1)
   {

     read_time();
     get_sec(ds1307_regs[0]);
     get_min(ds1307_regs[1]);
     get_hr(ds1307_regs[2]);
     get_date(ds1307_regs[4]);
     get_month(ds1307_regs[5]);
     get_year(ds1307_regs[6]);

      display_hours();
      //=====================
      lcd_putc(":");

      display_min();
     //=======================
      display_sec();

          if(!input(sw_set))
          {
            while(!input(sw_set));
                        
            flg_set_on=1;
            set_on();
            flg_set_off=1;
            set_OFF();
          }
   }
}
void set_on(void)
{        set_count=0;  
         lcd_gotoxy(1,0);
         lcd_putc("TIME ON");
         lcd_gotoxy(5,0);
         lcd_putc("  :      ");
                    //=====================show pre value==============
                      hex_bcd(min_on);
                      lcd_gotoxy(5,3);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      
                      hex_bcd(hr_on);
                      lcd_gotoxy(5,0);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
        while(flg_set_on)
        {
          //=============set position========
          if(!input(sw_set))
          {  delay_ms(101);
            if(!input(sw_set))
            {            
             while(!input(sw_set));
             
             

               if(set_count==0)
               {
                lcd_gotoxy(5,0);
               }
               if(set_count==1)
               {
                lcd_gotoxy(5,3);
               }
                set_count++;
                if(set_count>1)set_count=0;
            }    
          }
          //========================
                  if(set_count==0 && !input(sw_up) && input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==0 && !input(sw_up) && input(sw_down))
                    {
                     
                     if(min_on<60)
                     {min_on++;
                      if(input(sw_set))delay_ms(300);
                      hex_bcd(min_on);
                      lcd_gotoxy(5,3);
                       lcd_putc(num[a]);
                       lcd_putc(num[b]);
                       write_eeprom (0,min_on);
                     }
                    }
                  }
          //=========================        
                  if(set_count==0 && input(sw_up) && !input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==0 && input(sw_up) && !input(sw_down))
                    {
                     if(min_on>0)
                     {
                      min_on--;
                      if(input(sw_set))delay_ms(300);
                      hex_bcd(min_on);
                      lcd_gotoxy(5,3);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      write_eeprom (0,min_on);
                     } 
                    
                    }
                  } 
          
          //========================
                  if(set_count==1 && !input(sw_up) && input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==1 && !input(sw_up) && input(sw_down))
                    {
                     if(hr_on<24)
                     {
                       hr_on++;
                       if(input(sw_set))delay_ms(300);
                       hex_bcd(hr_on);
                      lcd_gotoxy(5,0);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      write_eeprom (1,hr_on);
                     }
                    
                    }
                  }
          //=========================
                  if(set_count==1 && input(sw_up) && !input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==1 && input(sw_up) && !input(sw_down))
                    {
                      if(hr_on>0)
                      {
                      hr_on--;
                      if(input(sw_set))delay_ms(300);
                      hex_bcd(hr_on);
                      lcd_gotoxy(5,0);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      write_eeprom (1,hr_on);
                      }
                    
                    }
                  }
          //===================exit set time on=====================
                 if(!input(sw_up) && !input(sw_down))
                 {
                   delay_ms(105);
                   if(!input(sw_up) && !input(sw_down))
                   {
                     while(!input(sw_up) && !input(sw_down));
                     flg_set_on=0;
                   }
                 }


                                  

        }
}

void set_OFF(void)
{
         set_count=0;  
         lcd_gotoxy(1,0);
         lcd_putc("TIMEoff");
         lcd_gotoxy(5,0);
         lcd_putc("  :      ");
         //=====================show pre value==============
                      hex_bcd(min_off);
                      lcd_gotoxy(5,3);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      
                      hex_bcd(hr_off);
                      lcd_gotoxy(5,0);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]); 
        while(flg_set_off)
        {
          //=============set position========
          if(!input(sw_set))
          {  delay_ms(101);
            if(!input(sw_set))
            {            
             while(!input(sw_set));
             
             

               if(set_count==0)
               {
                lcd_gotoxy(5,0);
               }
               if(set_count==1)
               {
                lcd_gotoxy(5,3);
               }
                set_count++;
                if(set_count>1)set_count=0;
            }    
          }
          //========================
                  if(set_count==0 && !input(sw_up) && input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==0 && !input(sw_up) && input(sw_down))
                    {
                     
                     if(min_off<60)
                     { min_off++;
                       if(input(sw_set))delay_ms(300);
                       hex_bcd(min_off);
                       lcd_gotoxy(5,3);
                       lcd_putc(num[a]);
                       lcd_putc(num[b]);
                       write_eeprom (2,min_off);
                     }
                    }
                  }
          //=========================        
                  if(set_count==0 && input(sw_up) && !input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==0 && input(sw_up) && !input(sw_down))
                    {
                     if(min_off>0)
                     {
                      min_off--;
                      if(input(sw_set))delay_ms(300);
                      hex_bcd(min_off);
                      lcd_gotoxy(5,3);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      write_eeprom (2,min_off);
                     } 
                    
                    }
                  } 
          
          //========================
                  if(set_count==1 && !input(sw_up) && input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==1 && !input(sw_up) && input(sw_down))
                    {
                     if(hr_off<24)
                     {
                       hr_off++;
                       if(input(sw_set))delay_ms(300);
                       hex_bcd(hr_off);
                       lcd_gotoxy(5,0);
                       lcd_putc(num[a]);
                       lcd_putc(num[b]);
                       write_eeprom (3,hr_off);
                     }
                    
                    }
                  }
          //=========================
                  if(set_count==1 && input(sw_up) && !input(sw_down))
                  {
                    delay_ms(101);
                    if(set_count==1 && input(sw_up) && !input(sw_down))
                    {
                      if(hr_off>0)
                      {
                      hr_off--;
                      if(input(sw_set))delay_ms(300);
                      hex_bcd(hr_off);
                      lcd_gotoxy(5,0);
                      lcd_putc(num[a]);
                      lcd_putc(num[b]);
                      write_eeprom (3,hr_off);
                      }
                    
                    }
                  }
                    //===================exit set time off=====================
                 if(!input(sw_up) && !input(sw_down))
                 {
                   delay_ms(105);
                   if(!input(sw_up) && !input(sw_down))
                   {
                     while(!input(sw_up) && !input(sw_down));
                     flg_set_off=0;
                   }
                 }
                 
        

        }
}
void display_hours(void)
{
      lcd_gotoxy(5,0);

      lcd_putc(NUM[hr10]);
      lcd_gotoxy(5,1);
      lcd_putc(NUM[hr00]);
}
void display_min(void)
{
      lcd_gotoxy(5,3);
      lcd_putc(num[min10]);
      lcd_putc(num[min00]);
}
void display_sec(void)
{
        lcd_gotoxy(5,5);
       lcd_putc(":");
      lcd_putc(num[sec10]);
      lcd_putc(num[sec00]);

}
void hex_bcd(int8 k)
{//int8 t;
 k%=100;
 a=0;b=0;
 a=k/10;
 b=k%10;
}
