// PIC32MX530F128H Configuration Bit Settings

// DEVCFG3
#pragma config USERID = 0xFFFF          // Enter Hexadecimal value (Enter Hexadecimal value)
#pragma config PMDL1WAY = OFF           // Peripheral Module Disable Configuration (Allow multiple reconfigurations)
#pragma config IOL1WAY = OFF            // Peripheral Pin Select Configuration (Allow multiple reconfigurations)
#pragma config FUSBIDIO = OFF           // USB USID Selection (Controlled by Port Function)
#pragma config FVBUSONIO = OFF          // USB VBUS ON Selection (Controlled by Port Function)

// DEVCFG2
#pragma config FPLLIDIV = DIV_2         // PLL Input Divider (2x Divider)
#pragma config FPLLMUL = MUL_16         // PLL Multiplier (16x Multiplier)
#pragma config UPLLIDIV = DIV_2         // USB PLL Input Divider (2x Divider)
#pragma config UPLLEN = ON              // USB PLL Enable (Enabled)
#pragma config FPLLODIV = DIV_8       // System PLL Output Clock Divider (PLL Divide by 8)

// DEVCFG1
#pragma config FNOSC = PRI              // Oscillator Selection Bits (Primary Osc (XT,HS,EC))
#pragma config FSOSCEN = ON             // Secondary Oscillator Enable (Enabled)
#pragma config IESO = ON                // Internal/External Switch Over (Enabled)
#pragma config POSCMOD = HS             // Primary Oscillator Configuration (HS osc mode)
#pragma config OSCIOFNC = OFF           // CLKO Output Signal Active on the OSCO Pin (Disabled)
#pragma config FPBDIV = DIV_8           // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/8)
#pragma config FCKSM = CSDCMD           // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled)
#pragma config WDTPS = PS1048576        // Watchdog Timer Postscaler (1:1048576)
#pragma config WINDIS = OFF             // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode)
#pragma config FWDTEN = OFF             // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls))
#pragma config FWDTWINSZ = WINSZ_25     // Watchdog Timer Window Size (Window Size is 25%)

// DEVCFG0
#pragma config JTAGEN = OFF             // JTAG Enable (JTAG Disabled)
#pragma config ICESEL = ICS_PGx2        // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2)
#pragma config PWP = OFF                // Program Flash Write Protect (Disable)
#pragma config BWP = OFF                // Boot Flash Write Protect bit (Protection Disabled)
#pragma config CP = OFF                 // Code Protect (Protection Disabled)

#include <xc.h> //XC32 prekladac
#include "oneWire.h"
#include "delay.h"
#include "lcd.h"
#include <stdio.h>

unsigned char i, carry, initFlagOneWire;
unsigned char pole[16];
float temp;

void main() {
    initLcd();
    writeLineLcd("Teplota [^C]:");
    
    initOneWire();

    while(1) {
        //mereni teploty
        initFlagOneWire = 1;
        while(initFlagOneWire) {
            initFlagOneWire = resetOneWire();
        }
        
        skipRomOneWire();
        convertTOneWire();
        delayMs(500); //12bit prevod trva max. 750 ms
        delayMs(300);

        //cteni teploty
        initFlagOneWire = 1;
        while(initFlagOneWire) {
            initFlagOneWire = resetOneWire();
        }
        
        skipRomOneWire();
        readScratchpadOneWire();

        for(i = 0; i < 12; i++) { //bez kontroly, pouze teplota
            pole[i] = readOneWire();
        }
        
        //zpracovani dat
        setAddrLcd(1, 0);
        
        if(pole[11]) { //zaporna cisla, prevod z dvojkoveho doplnku
            dataLcd('-');
            for(i = 0; i < 12; i++) { //negace
                if(pole[i]) {
                    pole[i] = 0;
                }

                else {
                    pole[i] = 1;
                }
            }

            carry = 1;

            for(i = 0; i < 12; i++) { //+1
                if(carry) {
                    if(pole[i]) {
                        pole[i] = 0;
                    }

                    else {
                        pole[i] = 1;
                        carry = 0;
                    }
                }
            }
        }
        
        else { //kladna cisla
            dataLcd('+');
        }
        
        dataLcd(' ');

        temp = 0.0;
        temp += 0.0625 * pole[0];
        temp += 0.125 * pole[1];
        temp += 0.25 * pole[2];
        temp += 0.5 * pole[3];
        temp += 1.0 * pole[4];
        temp += 2.0 * pole[5];
        temp += 4.0 * pole[6];
        temp += 8.0 * pole[7];
        temp += 16.0 * pole[8];
        temp += 32.0 * pole[9];
        temp += 64.0 * pole[10];

        sprintf(pole, "%.4f", temp);
        writeStringLcd(pole, 14);
    }
}