// 18F14K50 BLINK EXAMPLE

#include <18F14K50.h>

#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG,CPUDIV1,BROWNOUT
#use delay(clock=24000000)

// Bootloader settings
   /****** Remapped Vectors ********************
    *   _____________________
    *   |       RESET       |   0x000000
    *   |      LOW_INT      |   0x000008
    *   |      HIGH_INT     |   0x000018
    *   |       TRAP        |   0x000028
    *   |     Bootloader    |   0x00002E
    *   .                   .
    *   .                   .
    *   |     USER_RESET    |   0x000800
    *   |    USER_LOW_INT   |   0x000808
    *   |    USER_HIGH_INT  |   0x000818
    *   |      USER_TRAP    |   0x000828
    *   |                   |
    *   |   Program Memory  |
    *   .                   .
    *   |___________________|   0x0005FFF / 0x0007FFF
    */
    
// INIDIRIZZAMENTO VETTORI RESET ED INTERRUPT
#build(reset=0x800, interrupt=0x808)

// LE LOCAZIONI DA 0x000 A 0x7FF SONO RISERVATE AL BOOTLOADER
#org 0x000, 0x7FF { }

// Configure leds and switches

#define LED_GREEN    PIN_C6
#define LED_RED      PIN_C7

#define SW_DOWN      PIN_C4
#define SW_UP        PIN_C5

#define LED_ON       output_high
#define LED_OFF      output_low

// Global variables
int8 old_sw_up, old_sw_down;

// Functions prototypes
int8 SwitchUpIsPressed(void);
int8 SwitchDownIsPressed(void);


// *** Main *******************************************************************
void main(void)
{
   LED_OFF(LED_GREEN);
   LED_OFF(LED_RED);
   
   while (1)
   {
      LED_ON(LED_GREEN);
      delay_ms(200);
      
      LED_ON(LED_RED);
      delay_ms(200);
      
      LED_OFF(LED_GREEN);
      delay_ms(200);
      
      LED_OFF(LED_RED);
      delay_ms(200);
   }
}

// *** Functions **************************************************************

int8 SwitchUpIsPressed(void)
{
   if (input(SW_UP) != old_sw_up)
   {
      old_sw_up = input(SW_UP);

      if (input(SW_UP) == 0)
      {
         return 1;
      }
   }

   return 0;
}

int8 SwitchDownIsPressed(void)
{
   if (input(SW_DOWN) != old_sw_down)
   {
      old_sw_down = input(SW_DOWN);

      if (input(SW_DOWN) == 0)
      {
         return 1;
      }
   }

   return 0;
}
