<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-3X8HLBGM49"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag('js', new Date());

  gtag('config', 'G-3X8HLBGM49');
</script>/*
 * File:   main.c
 * Author: Dudas Levente
 * MPLAB-X
 * Created on 2016. marcius 19., 12:54
 */

#include <stdio.h>
#include <stdlib.h>
#include <pic.h>

// ez volt a régi config beállítás, évekkel ezelőtti mplab idé-ben.
//__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_ON & MCLRE_OFF & CP_OFF & BOREN_ON & CLKOUTEN_OFF & IESO_ON & FCMEN_ON);
//__CONFIG(WRT_OFF & STVREN_ON & BORV_LO & LVP_OFF);

// a friss verzióban így kell megadni!!!
// CONFIG1
#pragma config FOSC = INTOSC    // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset disabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is disabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config VCAPEN = OFF     // Voltage Regulator Capacitor Enable bit (VCAP pin function disabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>


unsigned int ADC(unsigned char ch);
void delay(unsigned int k);
void villam(unsigned int d);

int main(int argc, char** argv)
{
    unsigned int s = 0;
    unsigned int sm = 0;
    PORTA  = 0;
    PORTB  = 0;
    PORTC  = 0;
    LATA   = 0;
    LATB   = 0;
    LATC   = 0;
    TRISA  = 3;
    TRISB  = 0;
    TRISC  = 0;
    ADCON0 = 1;
    ADCON1 = 0b10000000;
    ANSELA = 3;

    while(1){
        sm = s;
        s = ADC(0);
        if( sm < s - 10 ) villam(120);
    }

    return (EXIT_SUCCESS);
}

unsigned int ADC(unsigned char ch){
    ADCON0 = (unsigned char)(1 | ( ch << 2 ));
    GO = 1;
    while(GO);
    return ADRESH * 256 + ADRESL;
}

void delay(unsigned int k)
{
    while(k>0) k--;
}

void villam(unsigned int d)
{
    unsigned char i;
    unsigned char t = 1;
    for( i = 0; i < 8; i++ ){
        LATC |= t;
        t 