/*******************************************************************
* FileName:        (change filename of template).c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file does the following....
*                                                                     
*
* Creation and Revisions:
*      Author               Date			Comments
*   Matt Boutell			18 Nov 2009     
********************************************************************/

/** Header Files ***************************************************/     
#include <p18f4520.h>
#include <delays.h> 

/** Configuration Bits *********************************************/     
#pragma config OSC = INTIO67  // EC = External 4MHz Crystal for PICDEM board only
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF

/** Define Constants Here ******************************************/
#define PRESSED 0
#define UNPRESSED 1

/** Local Function Prototypes **************************************/
void sampleFunction(void);

/** Global Variables ***********************************************/
int sampleVariable = 0;
	
/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void)
{
	OSCCONbits.IRCF2 = 0; // sets to 125 kHz
	OSCCONbits.IRCF1 = 0;
	OSCCONbits.IRCF0 = 1;
	ADCON1 = 0x0F; // all digital
	TRISB = 0b11111110; // 0xFE

	// 1 sec = 125000/(4 ticks/IC) = 31250
	// 1/4 sec = 7812.5 IC
	// 3/4 sec = 23437.5 IC
	
	while (1)
    {
		if (PORTBbits.RB1 == PRESSED ) {
			PORTBbits.RB0 = 0;
			Delay100TCYx(78);
			Delay10TCYx(1);
			Delay1TCY();
			Delay1TCY();
			PORTBbits.RB0 = 1;
			Delay100TCYx(234);
			// it's a pain that TCY doesn't take a parameter
			// Parameter passing takes extra cycles...
			// so we need to repeat manually if we really care that much.
			Delay1TCY(); 
			Delay1TCY();
			Delay1TCY();
			Delay1TCY();
			Delay1TCY();
			Delay1TCY();
			Delay1TCY();
		} else {
			PORTBbits.RB0 = 0;
		}
    }
}

/*******************************************************************
* Additional Helper Functions
********************************************************************/

/*******************************************************************
* Function:			void sample(void)
* Input Variables:	none
* Output Return:	none
* Overview:			Use a comment block like this before functions
********************************************************************/
void sampleFunction()
{
	// Some function that does a specific task
}