/* When you make a thing, a thing that is new, 
it is so complicated making it that it is bound 
to be ugly. But those that make it after you, 
they don’t have to worry about making it. 
And they can make it pretty, and so everybody 
can like it when others make it after you */


/* CONFIG1H ************************/
#pragma config	IESO = OFF				// Internal/External Oscillator Switchover.
#pragma config	FCMEN = OFF			// Fail-Safe Clock Monitor.
#pragma config	PRICLKEN = OFF		// Primary Clock can be disabled by software.
#pragma config	PLLCFG = OFF			// 4X PLL.
#pragma config	FOSC = INTIO67		// Oscillator.

/* CONFIG2L ************************/
#pragma config	BORV = 0x03			// Brown-out Reset Voltage.
#pragma config	BOREN = SBORDIS		// Brown-out Reset Enable.
#pragma config	PWRTEN = ON			// Power-up Timer.

/* CONFIG2H ************************/
#pragma config	WDTPS = 0x01			// Watchdog Timer Postscale.
#pragma config	WDTEN = OFF			// Watchdog Timer Enable.

/* CONFIG3H ************************/
#pragma config	MCLRE = EXTMCLR		// MCLR Pin Enable.
#pragma config	P2BMX = PORTB5		// P2B Input MUX bit.
#pragma config	T3CMX = PORTC0		// Timer3 Clock Input MUX bit.
#pragma config	HFOFST = OFF			// HFINTOSC Fast Start-up bit.
#pragma config	CCP3MX = PORTC6		// CCP3 MUX bit.
#pragma config	PBADEN =OFF			// PORTB A/D Enable bit.
#pragma config	CCP2MX =PORTC1		// CCP2 MUX bit.

/* CONFIG4L ************************/
#pragma config	XINST = OFF			// Boot Block Size.
#pragma config	LVP = ON				// Boot Block Size.
#pragma config	STVREN = OFF			// Stack Full/Underflow Reset.

/* CONFIG5L ************************/
#pragma config	CP1 = OFF				// Code Protection.
#pragma config	CP0 = OFF				// Code Protection.

/* CONFIG5H ************************/
#pragma config	CPD = OFF				// Data EEPROM Code Protection.
#pragma config	CPB = OFF				// Boot Block Code Protection.

/* CONFIG6L ************************/
#pragma config	WRT1 = OFF				// Write Protection.
#pragma config	WRT0 = OFF			// Write Protection.

/* CONFIG6H ************************/
#pragma config	WRTD = OFF			// Data EEPROM Write.
#pragma config	WRTB = OFF			// Boot Block Write Protection.
#pragma config	WRTC = OFF			// Configuration Register Write.

/* CONFIG7L ************************/
#pragma config	EBTR1 = OFF			// Table Read Protection.
#pragma config	EBTR0 = OFF			// Table Read Protection.

/* CONFIG7H ************************/
#pragma config	EBTRB = OFF			// Boot Block Table Read Protection.



#if	defined ( _PIC18F24K22_H_ )
	#define	FREQ_64MHz	64000000
//	#define	FREQ_16MHz	16000000
//	#define	FREQ_8MHz	8000000
//	#define	FREQ_4MHz	4000000
//	#define	FREQ_1MHz	1000000
#endif


#define TRUE		0x01
#define FALSE		0x00

#include "Oscillator.c"
#include "UART.c"
#include "Interrupts.c"
#include "PullUps.c"
#include "Timer1-3-5.c"
#include "Timer2-4-6.c"


#define	TRUE	0x01
#define	FALSE	0x00

#define SET		0x01
#define CLEAR	0x00

#define	ON		0x01
#define	OFF		0x00


#define	OpAmp_POWER_SUPPLY_ON	LATCbits  . LATC0 = ON;
#define	SAWTOOTH_LED_ON			LATCbits  . LATC1 = ON;
#define	TRIANGULAR_LED_ON		LATCbits  . LATC2 = ON;
#define	SQUARE_LED_ON			LATCbits  . LATC3 = ON;
#define	SINEWAVE_LED_ON			LATCbits  . LATC4 = ON;

#define	OpAmp_POWER_SUPPLY_OFF	LATCbits  . LATC0 = OFF;
#define	SAWTOOTH_LED_OFF		LATCbits  . LATC1 = OFF;
#define	TRIANGULAR_LED_OFF		LATCbits  . LATC2 = OFF;
#define	SQUARE_LED_OFF			LATCbits  . LATC3 = OFF;
#define	SINEWAVE_LED_OFF		LATCbits  . LATC4 = OFF;

#define	ALL_LEDS_ON		LATC  = 0x1F;
#define	ALL_LEDS_OFF	LATC  = 0x00;


void SetUp ( void )
{
/* Set Up the Internal Oscillator. */
	SetUpOscillator ( OSC_64MHz );

/* Setting Up the  PORTC as "output", but the Rx pin. */
	TRISA = 0x00;
	PORTA = 0x00;
	LATA = 0x0F;

	TRISB = 0xFF;
	EnablePullUps ( ALL_WPU );

/* Setting Up the  PORTC as "output", but the Rx pin. */
	TRISC = 0x80;
	PORTC = 0x80;
	LATC = 0x80;

	ALL_LEDS_OFF;

/* Set Up the USART Comunication. */
	SetUpUART ( BAUD_RATE_57600 );
	
/* SetUp the Timer 1 */
	SetUpTimer1 ( T1_FOSC_CLOCK, T1_PreS_1_1, T1_16_BIT_COUNTER );

/* SetUp the Timer 2 */
	SetUpTimer2 ( T2_PreS_1_1, 250, T2_PostS_1_16 );
	
/* Enabling Interrupts, no priorities. */
	EnableInterrupt ( PORTB_IOC );
//	EnableInterrupt ( TMR1_INTERRUPT );

	EnableInterrupt ( GLOBAL_INTERRUPT );

	}