/*
 * File:   main.c
 * Author: Vojta
 *
 * Created on 12. ?ervence 2022, 0:44
 */

#include "hlava.h"


void main(void)
{
    poprve = 1; 
    load_dia();
    reg_init();
    load_EEPROM_data();
    //uart_init();
           
    while(1)
    {
        //********************sleep mode**************************************
        if((!tl_min || !tl_plus || !tl_sel ||!tl_fire) && bcr_on_flag ) sleep_timer = 0;

        if(sleep_timer >= 60000)
        {
           fire = 0;
           bcr_off;
           dig1=0;
           dig2=0;
           dig3=0;
           sleep_timer = 60001;
        }

        if((!tl_min || !tl_plus || !tl_sel) && sleep_timer >=60000 )
        {
            sleep_timer = 0;
            butt_press = 1;
            butt_count = 0;

        }

        if((sleep_timer <= 60000) && !(bcr_on_flag))
        {
            sleep_timer = 0;

            if(butt_press == 1 && butt_count >= 250)
                butt_watch = 1;
            if((tl_min || tl_plus || tl_sel) && butt_watch == 1)
            {
                bcr_on;
                butt_press = 0;
                butt_count = 0;
                //butt_watch = 0;
                seg_adc_count = 1000;
                __delay_ms(10);
            }
        }

       //****************************************************************************************
       if(bcr_on_flag)
       {
          if(!battery_check)
          {
              if(TC_ON && fire) //pri zapnutem TC a zhaveni kazdych 10 ms mer odpor
              {    
                  if(meas_count >= 10 )
                  {
                      ohm_read(); // kazdych 10 ms vypne PWM a zmerí odpor a zase ho zapne, 
                  }        
              } 
              else if(!TC_ON && fire) //TC off, kazdou 0.5sekundu check jestli neni zkrat
              {
                  if(meas_count >= 200 )
                  {
                      ohm_read(); // kazdych 500 ms vypne PWM a zmerí odpor a zase ho zapne, 
                   }          
              }else
              {
                  ohm_read(); //meri normalne odpor jenom kazdou sekundu
              }
           }


          if(!tl_fire)
          {

               fire = 1; 
               batt_read_count = 0;

               if(n < 1) //aby to dalo (new_meas_ready =1) jen jednou
               {
                   set_vykon();
                   n=1;
                   new_meas_ready =1 ; // flag pro vypnuti pwm a zacnuti merit
               }   

          }else
          {
               fire = 0; 
               CCP1CONbits.MODE = 0; //PWM off

               if( n == 1 )
               {
                  batt_read_count = 1700;
                  n=0;
               }
           }

           if(battery_check || batt_read_count >= 2000) //volani funkce pro mereni baterie (bud kazdych 2s nebo po kazdem ukonceni zhaveni)
           {
               battery_read();
               batt_read_count = 0;
           }

           error_check();

           if(!error && !poprve ) menu();

       }
    }
    return ;
}

void reg_init(void)
{
    //*******Oscilator*******
    OSCCON1 = 0b01100000; //HFINTOSC, clk divider 1
    OSCCON3 = 0x00;
    OSCEN = 0;
    OSCFRQ = 0b110; //focs = 32 MHz
    while(!OSCSTATbits.HFOR);// pockej dokud není HFINTOSC ready
  
    //*******PORTS**********
    ANSELA = 0; // analogs A OFF
    ANSELB = 0; // analogs B OFF
    ANSELC = 0b00110000; // analogs C 
    
    TRISA = 0b00111000; // I/O
    TRISC = 0b10110111; // I/O
    TRISB = 0b10000000;    // I/O
    LATA = 0;
    LATB = 0;
    
   //*********zapnuti proudového zdroje a mereni baterii*****     
    bcr_on; bat_on;
    
   //*******pull-ups*************
    WPUA = 0b00000000; //weak pull-ups
    WPUC = 0b00000111; //weak pull-ups
    WPUB = 0b10000000; //weak pull-ups
    

    //*******interrupts**********
    GIE = 1; // global interrupts ON
    PEIE = 1; // Peripheral Interrupt ON
    PIE0bits.TMR0IE = 1; // Timer0 Interrupt ON
   
    
    //*******TMR0*******************
    T0CON0bits.T0EN = 1;
    T0CON0bits.T016BIT = 0; // 8-bit Timer0 Counter bits
    T0CON0bits.T0OUTPS = 0; // 1:1 Postscaler
    
    T0CON1bits.T0CS = 0b010; //Timer0 Clock Source Selections: FOSC/4
    T0CON1bits.T0ASYNC = 0; //The input to the TMR0 counter is synchronized to FOSC/4
    T0CON1bits.T0CKPS = 0b0101; // 1:32 prescaler (delicka) => overflow(delay) = 1.024 ms
    
    
    //********TMR4*******************
    T4CONbits.CKPS = 0b101; //1:32 Prescaler 
    T4CONbits.OUTPS = 0; // 1:1 Postscaler
    
    T4HLTbits.PSYNC = 1; //Timer Prescaler Output is synchronized to FOSC/4
    T4HLTbits.CKSYNC = 1; //ON bit is synchronized to timer clock input
    T4HLTbits.MODE = 0; // free running period mode
    T4CLKCON = 0x01; // clk source fosc/4 => kazdou 4us: +1 TMR4
    T4PR = 255; //restart kdyz T4PR = T4TMR
    T4CONbits.ON = 1;
 
    
    //********ADC + temp sensor*********************
    adc_init(); // viz. ADC.c
    
    //*********TMR2+PWM*****************
    PWM_init();

    
    /**vynulovani pocitadel*/
    seg_count1 = 0;
    seg_adc_count = 0;
    adc_switch = 0;
    batt_read_count = 0;

}

void __interrupt() jou()
{ 
    if(PIR0bits.TMR0IF) //TMR0 interrupt kazdych 1.024 ms
    {
        PIR0bits.TMR0IF = 0;
        seg_count1++;
        seg_adc_count++;
        adc_switch++;
        meas_count++;
        butt_count++; 
        batt_read_count++;
        error_count++;
        sleep_timer++;
    } 
}


