#include <xc.h>
#define _XTAL_FREQ 4000000
#include <stdio.h>
#include <stdlib.h>


#include "lcd_pic16.c"

// PIC16F887 Configuration Bit Settings


// CONFIG1
#pragma config FOSC =XT        // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = OFF        // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = OFF       // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF         // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)

// CONFIG2
#pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)


//variables globales
unsigned char buffer[20],i;
unsigned int contador=0;
//funciones prototipo
void init(void);


void main(void)
{

	init();
while(1)
	{
            for(i=0;i<18;i++)
            {
   
		SetDDRamAddr(0x00);
		putrsXLCD("Conta= ");
                sprintf(buffer,"%05u",contador);
                putsXLCD(buffer);
                SetDDRamAddr(0x40+i);
		putrsXLCD(" prueba 887 ");
                __delay_ms(200);
          //      CLS_Line2();
                
                contador=contador +1;
                 if (i==2)
                {  RB0=1;
                    RB7=0;
                }
                if (i==4)
                {   RB1=1;
                    RB0=0;
                }

                if (i==6)
                {    RB2=1;
                    RB1=0;
                }
              if (i==8)
              {  RB3=1;
                    RB2=0;
              }

               if (i==10)
               {     RB4=1;
                    RB3=0;
               }
               if (i==12)
               {    RB5=1;
                    RB4=0;
               }
               if (i==14)
               {    RB6=1;
                    RB5=0;
               }

                if (i==16)
                {   RB7=1;
                    RB6=0;
                }
            }

	}
}

void init(void)
{
    //configuramos el oscilador
 
  
    ANSEL=0;   //todo el PORT como digital
   ANSELH=0;   //todo el PORT como digital
  
    TRISB=0;    //todo el PORTB como salidas digitales
    PORTB=0;
    OpenXLCD(FOUR_BIT & LINES_5X7 );

    
}
/*void mover_led (void)
{            if (i==2)
                {  RB0=1;
                    RB7=0;
                }
                if (i==4)
                {   RB1=1;
                    RB0=0;
                }

                if (i==6)
                {    RB2=1;
                    RB1=0;
                }
              if (i==8)
              {  RB3=1;
                    RB2=0;
              }

               if (i==10)
               {     RB4=1;
                    RB3=0;
               }
               if (i==12)
               {    RB5=1;
                    RB4=0;
               }
               if (i==14)
               {    RB6=1;
                    RB5=0;
               }

                if (i==16)
                {   RB7=1;
                    RB6=0;
                }
}*/
