////////////////////////////////////////////////////////////////////////////////
// Programa.....: PEB07_Prueba_pulsadores.c                                   //
// Plataforma hw: Placa PLUMABOT 18F4550(Xtal:20Mhz)+ placa periféricos PEB07 //
// Fecha........: julio-2012                                                  //
// Programador..: Domingo Llorente                                            //
// Descripción..: Programa que mustra en el lcd el estado de los pulsadores   //
// de la placa RB0, RA3 y RA4.                                                //
////////////////////////////////////////////////////////////////////////////////
#include <p18F4550.h>
#include <delays.h>       // para la función de retardo
#include <xlcd_pluma.h>   // xlcd.h modificado para el hardware de la placa
//--------------------  FUSES DEL PIC 18F4550  ------------------------------//
#pragma config PLLDIV = 5         // Divide by 5 (20 MHz oscillator input)    /
#pragma config CPUDIV = OSC1_PLL2 // [OSC1/OSC2 Src: /1][96 MHz PLL Src: /2]  /
#pragma config USBDIV = 2         // USB clock source from the 96 MHz PLL/2   /
#pragma config FOSC   = HSPLL_HS  // Oscilador HS,PLL enabled,HS used by USB  /
#pragma config FCMEN  = OFF       // Fail-Safe Clock Monitor disabled         /
#pragma config IESO   = OFF       // Oscillator Switchover mode disabled      /
#pragma config PWRT   = OFF       // Power-up timer enable bit disabled       /
#pragma config BOR    = OFF       // Brown-out Reset disabled hard. and soft  /
#pragma config BORV   = 3         // Brown-out voltage bit (Minimum setting)  /
#pragma config VREGEN = ON        // USB Voltage Regulator                    /
#pragma config WDT    = OFF       // Watchdog hard.disabled-soft.controlled   /
#pragma config WDTPS  = 32768     // Watchdog timer postscale 1:32768         /
#pragma config MCLRE  = ON        // MCLRE enabled, RE3 input pin disabled    /
#pragma config LPT1OSC= OFF       // Timer1 config.for higher power operation /
#pragma config PBADEN = OFF       // PORTB<4:0> pins as digital I/O on Reset  /
#pragma config CCP2MX = ON        // CCP2 I/O is multiplexed with RB3         /
#pragma config STVREN = OFF       // Stack full/underflow WILL NOT Reset      /
#pragma config LVP    = OFF       // Single-Supply ICSP disabled              /
#pragma config ICPRT  = OFF       // ICPORT disabled                          /
#pragma config XINST  = OFF       // Extended Instruction Set disabled        /
#pragma config CP0    = OFF       // (000800-001FFFh) not code-protected      /
#pragma config CP1    = OFF       // (002000-003FFFh) not code-protected      /
#pragma config CP2    = OFF       // (004000-005FFFh) not code-protected      /
#pragma config CP3    = OFF       // (006000-007FFFh) not code-protected      /
#pragma config CPB    = OFF       // Boot block(000-7FFh)not code-protected   /
#pragma config CPD    = OFF       // Data EEPROM not code-protected           /
#pragma config WRT0   = OFF       // Block 0 (0800-1FFFh) not write-protected /
#pragma config WRT1   = OFF       // Block 1 (2000-3FFFh) not write-protected /
#pragma config WRT2   = OFF       // Block 2 (4000-5FFFh) not write-protected /
#pragma config WRT3   = OFF       // Block 3 (6000-7FFFh) not write-protected /
#pragma config WRTB   = OFF       // Boot block(000-7FFh) not write-protected /
#pragma config WRTC   = OFF       // Registers(300000-3000FFh)not w.protected /
#pragma config WRTD   = OFF       // Data EEPROM not write-protected          /
#pragma config EBTR0  = OFF       // Block 0 (000800-001FFFh) not protected   /
#pragma config EBTR1  = OFF       // Block 1 (002000-003FFFh) not protected   /
#pragma config EBTR2  = OFF       // Block 2 (004000-005FFFh) not protected   /
#pragma config EBTR3  = OFF       // Block 3 (006000-007FFFh) not protected   /
#pragma config EBTRB  = OFF       // Boot block (000000-0007FFh) not protected/
//----------------------------------------------------------------------------/

//--- REMAPEO DE LOS VECTORES DE INTERRUPCIÓN PARA NO BORRAR EL BOOTLOADER ---/
// Este bloque es obligatorio si se utiliza el Botloader USB para cargar el   /
// programa en el pic, de lo contrario se puede eliminar.                     /
//----------------------------------------------------------------------------/
#define NEW_RESET_VECTOR_ADDRESS		0x1000
#define NEW_HIGH_INTERRUPT_VECTOR_ADDRESS	0x1008
#define NEW_LOW_INTERRUPT_VECTOR_ADDRESS	0x1018

void UserHighPriorityISRCode();
void UserLowPriorityISRCode();

extern void _startup (void);
#pragma code NEW_RESET_VECTOR = NEW_RESET_VECTOR_ADDRESS
void _reset (void){
    _asm goto _startup _endasm
}
#pragma code NEW_HIGH_INTERRUPT_VECTOR = NEW_HIGH_INTERRUPT_VECTOR_ADDRESS
void New_High_ISR (void)	{
     _asm goto UserHighPriorityISRCode _endasm
}
#pragma code NEW_LOW_INTERRUPT_VECTOR = NEW_LOW_INTERRUPT_VECTOR_ADDRESS
void Remapped_Low_ISR (void){
     _asm goto UserLowPriorityISRCode _endasm
}

#pragma code HIGH_INTERRUPT_VECTOR = 0x08
void High_ISR (void)	{
     _asm goto NEW_HIGH_INTERRUPT_VECTOR_ADDRESS _endasm
}
#pragma code LOW_INTERRUPT_VECTOR = 0x18
void Low_ISR (void){
     _asm goto NEW_LOW_INTERRUPT_VECTOR_ADDRESS _endasm
}

#pragma code
#pragma interrupt UserHighPriorityISRCode
void UserHighPriorityISRCode()
{
}

#pragma interruptlow UserLowPriorityISRCode
void UserLowPriorityISRCode()
{
}
#pragma code
//------------ FIN DEL BLOQUE DE REMAPEO DE INTERRUPCIONES ------------------//


void main(void)
{
ADCON1=0x0F;  // Todos entrada/salida digitales.
TRISA=0xFF;   // PORTA entradas
TRISB=0X01;   // RB0 entrada, resto salidas
TRISC&=0xF9;  // RC1 y RC2 salidas (led bicolor)
LATB=0x01;    // Leds/disp apagados.
LATC=0x00;    // Led bicolor apagado
lcd_init();   // Inicializa el lcd a 5x7dots, 2líneas
while(1)
 {
    if (PORTBbits.RB0==1 && PORTAbits.RA3==0 && PORTAbits.RA4==0)
    {
        borrar_lcd();
        escribe_cadena("PEB07 Pulsadores");
        cambio_linea();
        escribe_cadena("Todos: OFF");
        Delay10KTCYx(50);
    }
    if (PORTBbits.RB0==0)
    {
        borrar_lcd();
        escribe_cadena("PEB07 Pulsadores");
        cambio_linea();
        escribe_cadena("RB0: On ");
        Delay10KTCYx(50);
    }
    
    else if (PORTAbits.RA3==1)
    {
        borrar_lcd();
        escribe_cadena("PEB07 Pulsadores");
        cambio_linea();
        escribe_cadena("RA3: On ");
        Delay10KTCYx(50);
    }
    
    else if (PORTAbits.RA4==1)
    {
        borrar_lcd();
        escribe_cadena("PEB07 Pulsadores");
        cambio_linea();
        escribe_cadena("RA4: On ");
        Delay10KTCYx(50);
    }
 }
}
