#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 32000000

#include "lcd_pic16.c"


// PIC16F1939 Configuration Bit Settings

// 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 = ON       // Power-up Timer Enable (PWRT enabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Memory Code Protection (Data memory code protection is disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable (Brown-out Reset enabled)
#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 (All VCAP pin functionality is disabled)
#pragma config PLLEN = ON       // PLL Enable (4x PLL enabled)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = HI        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), high trip point selected.)
#pragma config LVP = OFF        // Low-Voltage Programming Enable (High-voltage on MCLR/VPP must be used for programming)

//variables globales
unsigned int contador=0;
unsigned char buffer[20];
bit flag_modo=0;

//funciones prototipo
void init(void);
void muestra_LCD (void);

void main(void)
{

    init();
    while(1)
    {
        muestra_LCD();
        if (RA0==1)
	{
            if(flag_modo==0)
		contador=contador+1;
            else
		contador=contador-1;
            muestra_LCD();
            __delay_ms(2);
            while(RA0==1);
            __delay_ms(2);
	}
	if (RA1==1)
	{
            flag_modo=!flag_modo;
            muestra_LCD();
            __delay_ms(2);
            while(RA1==1);
            __delay_ms(2);
	}

    }
}

void init(void)
{
    //configuramos el oscilador
    SCS0=0;//seleccion del oscilador a cargo de los fusibles de configuracion
    SCS1=0;
    IRCF0=0;//seteamos el postcaler para 8 MHZ
    IRCF1=1;
    IRCF2=1;
    IRCF3=1;
    SPLLEN=1;//activamos el PLL
    ANSELA=0;   //todo el PORTA como digital
    ANSELB=0;   //todo el PORTB como digital
    ANSELE=0;
    ANSELD=0;
    TRISB=0;    //todo el PORTB como salidas digitales
    PORTB=0;
 //   OpenXLCD(EIGHT_BIT & LINES_5X7 );
     OpenXLCD(FOUR_BIT & LINES_5X7 );
}

void muestra_LCD (void)
{
    SetDDRamAddr(0x03);
    putrsXLCD("modo:");
    if (flag_modo==0)
	putrsXLCD("UP  ");
    else
	putrsXLCD("DOWN");
    SetDDRamAddr(0x43);
    sprintf(buffer,"cuenta:%05u",contador);
    putsXLCD(buffer);
}

