/*
 * File:   newmain.c
 * Author: Stevo
 *
 * Created on 9. b?ezna 2018, 18:01
 */


// PIC18F4480 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
//#pragma config OSC = IRCIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config OSC = HS         // Oscillator Selection bits (XT oscillator)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = BOHW     // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown-out Reset Voltage bits (VBOR set to 2.1V)

// CONFIG2H
#pragma config WDT = ON         // Watchdog Timer Enable bit (WDT enabled)
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config PBADEN = OFF      // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer 1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config BBSIZ = 1024     // Boot Block Size Select bit (1K words (2K bytes) boot block)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include "SPI.h"
#include "CAN.h"




unsigned int prud1[8]=0;
int CurrentFuse[8]={0b01100100,0b01100100,0b01100100,0b01100100,0b01100100,0b01100100,0b01100100,0b01100100} ;
int a=0;
int teplota1=0;
int teplota2=0;
int teplota_limit=0x359;
int error=0;
int enable=0;
int emergency=0;
int Tripped[8]=0;
unsigned int Init_state=0b00001010;
int temp_trip=0;



void interrupt ISR(){

   if(INTCONbits.TMR0IF==1){
        INTCONbits.TMR0IF=0;
        INTCONbits.GIE=1;
        
        if(LATBbits.LATB5==1){
            PORTBbits.RB5=0;
        }
        else{
            PORTBbits.RB5=1;
        
        }

    }
    
   
   for(int i=0;i<8;i++){
        prud1[i]=ReadSPI(i);

        if(prud1[i] >> 15 == 1 && a > 0){
            LATD |= ( 1 << i );                   //vypne daný výstup
            error |= ( 1 << i );                     //zapí?e problem do can spravy
        }else{
            prud1[i]=(prud1[i] & 0x1fff) | (000 & 0xE000);
            if(prud1[i] >> 12 == 0){
                prud1[i]= 0b1000000000000 - prud1[i];           
            }else{
                prud1[i]=(prud1[i] & 0x0fff) | (000 & 0xF000);           
            }
            if(prud1[i] > CurrentFuse[i]){
                LATD |= ( 1 << i );                   //vypne daný výstup
                Tripped[i]=1;
            } 
        }
    }
    
    if(a==25){
        teplota1=(ADRESH << 8) | (ADRESL & 0xff);    //pre?ítanie teploty zo senzora 1 a ulo?enie do premennej teplota
        ADCON0bits.CHS=0b0111;                      //prepnutie prevodnika na senzor 2
        ADCON0bits.GODONE=1;                        //spustenie konverzie   
    }
    
    
    if(a==75){
        teplota2=((ADRESH << 8) | (ADRESL & 0xff)); //pripo?ítanie teploty zo senzora 2 k senzoru 1
        ADCON0bits.CHS=0b0110;                      //prepnutie prevodnika na senzor 1
        ADCON0bits.GODONE=1;                        //spustenie konverzie   
        if(teplota1 >= teplota_limit || teplota2 >= teplota_limit){
            LATD=0xFF;
            for(int i=0;i<8;i++){
                Tripped[i]=1;
            }
            temp_trip=1;
        }
    }
    
    
    if(a==85){
        TXB1EIDH = 0x00;
        TXB1EIDL = 0x00;
    
        
        TXB1SIDH = 0x7B;
        TXB1SIDL = 0xC0;

        TXB1DLC = 0x08;
        TXB1D0 = (teplota1+teplota2)/0b1000;
        TXB1D1 = ~ LATD;
        TXB1D2 = error;

    
        TXB1CONbits.TXREQ = 1; //Set the buffer to transmit
      
    }
    
    
    if(a==100){
        TXB0EIDH = 0x00;
        TXB0EIDL = 0x00;
    
        //0x35E    0110 1011 110
        TXB0SIDH = 0x6B;
        TXB0SIDL = 0xC0;

        TXB0DLC = 0x08;
        TXB0D0 = prud1[0]/0b10000;
        TXB0D1 = prud1[1]/0b10000;
        TXB0D2 = prud1[2]/0b10000;
        TXB0D3 = prud1[3]/0b10000;
        TXB0D4 = prud1[4]/0b10000;
        TXB0D5 = prud1[5]/0b10000;
        TXB0D6 = prud1[6]/0b10000;
        TXB0D7 = prud1[7]/0b10000;
    
        TXB0CONbits.TXREQ = 1; //Set the buffer to transmit
        a=0;
        enable=1;
        
        if(error != 0 || temp_trip == 1){
            LATBbits.LB6=1;                             //dioda svieti
        }else{
            if(LATBbits.LATB6==1){                      //dioda blika
                LATBbits.LB6=0;
            }
            else{
                LATBbits.LB6=1;
            }      
        }
    }
    a++;
}
void main(void) {

    TRISA=0; //configuring PORTA as output
    LATAbits.LA5=1;
    LATAbits.LA4=1;
    LATAbits.LA3=1;
    LATAbits.LA2=1;
    LATAbits.LA1=1;
    LATAbits.LA0=1;
    TRISBbits.RB5=0;
    TRISBbits.RB4=0;
    TRISBbits.RB6=0;          //Stavova dioda
   // LATBbits.LB6=1;           //Stavova dioda
    LATBbits.LB4=1;  
    LATBbits.LB5=1;
    TRISBbits.RB1=0;
    LATBbits.LB1=1;
    TRISD=0x00;                 //Ovladanie výstupov
    LATD=Init_state;            //Ovladnie vystupov
    TRISC=0;
    TRISEbits.RE0=0;
    LATEbits.LE0=1;
    TRISCbits.RC0=1;        //Input 1
    TRISCbits.RC1=1;        //Input 2
    TRISCbits.RC2=1;        //Input 3
    TRISCbits.RC5=1;        //Input 4  
    TRISEbits.RE1=1;
    TRISEbits.RE2=1;
    InitCAN();
    T0CON=0b01000011;
    TMR0H = 0;
    TMR0L = 0;
    INTCONbits.TMR0IE = 1;
    T0CONbits.TMR0ON = 1;
    INTCONbits.PEIE=1;
    INTCONbits.GIE=1;
    InitSPI();


    ADCON1=0b00000111;
    ADCON0=0b00011000;
    ADCON2=0b10101101;
    ADCON0bits.ADON=1;


    while(1){ 
        if(RXB0CONbits.RXFUL){
            if(RXB0D0 == 1){                                        //Emergency CAN
                emergency=1;
            }
            if(RXB0D1 == 1 && Tripped[3] == 0){                     //Fan
                LATDbits.LD3=0;
            }else{
                LATDbits.LD3=1;
            }          
            RXB0CONbits.RXFUL=0;        
        }
        
        if(PORTCbits.RC5==0 && enable==1){                          //Emergency analog
            emergency=1;
            LATDbits.LD0=1;
            LATDbits.LD1=1;
            LATDbits.LD2=1;       
        }
        
        if(PORTCbits.RC0==0 && enable==1 && Tripped[1]==0 && emergency==0){         //Fuel pump
            LATDbits.LD1=0;
        }else{
            LATDbits.LD1=1;
        }
        
        if(PORTCbits.RC1==0 ){           //Reset
            if(emergency == 0){
                LATD=Init_state;   
                for(int i=0;i<8;i++){
                    Tripped[i]=0;
                }
            }else{
                LATDbits.LD3=0;
                Tripped[3]=0;
                LATDbits.LD4=0;
                Tripped[4]=0;
                LATDbits.LD5=0;
                Tripped[5]=0;
                LATDbits.LD6=0;
                Tripped[6]=0;
                LATDbits.LD7=0;
                Tripped[7]=0;
            }
        }
        
    }
}
