/********************************************************************
* FileName:        (change filename of template).c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file does the following....                                                                 
*
*       Author               Date              Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// (Your name here) 

/**  Header Files **************************************************/     
#include <p18f4520.h> 
#include <timers.h>
#include <portb.h>
#include <pwm.h>

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

/** Define Constants Here ******************************************/
#define G4 158
#define C5 119

/** Local Function Prototypes **************************************/
void low_isr(void);
void high_isr(void);

/** Declare Interrupt Vector Sections ****************************/
#pragma code high_vector=0x08
void interrupt_at_high_vector(void) {
   _asm goto high_isr _endasm
}

#pragma code low_vector=0x18
void interrupt_at_low_vector(void) {
   _asm goto low_isr _endasm
}

/** Global Variables ***********************************************/
unsigned int valueTMR3 = 34286;
char noteToggle = 1;
char timeToggle = 1;


	
/*******************************************************************
* Function:        void main(void)
********************************************************************/
#pragma code
void main (void) {
	// PART C
	ADCON1 = 0x0F;	// Set all pins to digital
	TRISB = 0x01;	// Set RB0 as an input

	// PART A - Configure PWM1 using Timer2
	OpenTimer2( TIMER_INT_OFF & T2_PS_1_16 );
	noteToggle = 1;   // 1 for Amid, 0 for Cmid
	OpenPWM1( 158 );
	SetDCPWM1( 204 );

	// PART B
	RCONbits.IPEN = 1;		// Use Priority Mode INTs
	INTCONbits.GIEH = 1;	// Enable High Priority INTs
	// Set up Timer 3 with interrupts
	OpenTimer3( TIMER_INT_ON & T3_16BIT_RW &
				T3_SOURCE_INT & T3_PS_1_8 );
	IPR2bits.TMR3IP = 1; // Set Timer 3 as High Priority INTs
	WriteTimer3( valueTMR3 );
	
	// PART C - Set up RB0 interrupt
	OpenRB0INT( PORTB_CHANGE_INT_ON &
				FALLING_EDGE_INT &
				PORTB_PULLUPS_OFF );
	// Alternative to OpenRB0INT for PART C
	// INTCONbits.INT0IE = 1;    // Enable RBO interrupt
	// INTCON2bits.INTEDG0 = 0;	 // Set falling edges

	while (1){ }
}


















/*****************************************************************
* Function:        void high_isr(void)
* Possible sources of interrupt - none
* Overview:
******************************************************************/
#pragma interrupt high_isr
void high_isr(void){
    // PART B
	if( PIR2bits.TMR3IF ) {
		PIR2bits.TMR3IF = 0;
				
		// toggle between notes
		noteToggle ^= 1;
		if ( noteToggle == 0 )
			OpenPWM1( 119 );
		else
			OpenPWM1( 158 );
		WriteTimer3( valueTMR3 );
	}
	// PART C
	if( INTCONbits.INT0IF ) {
		INTCONbits.INT0IF = 0;

		// update valueTMR3 change timing between notes
		timeToggle ^= 1;
		if ( timeToggle == 0 )
			valueTMR3 = 34286;
		else
			valueTMR3 = 3036;
	}
}

/******************************************************************
* Function:        void low_isr(void)
* Possible sources of interrupt - none
* Overview:
********************************************************************/
#pragma interruptlow low_isr
void low_isr(void){

}