/********************************************************************
* FileName:        Lab Exam Problem 1.c
* Processor:       PIC18F4520
* Compiler:        MPLAB C18 v.3.06 
*
* This file counts down the LEDs from 12 then plays a boom using the buzzer
*                                                                     
*
* Author               Date        Comment
*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
// David Fisher 10/23/07


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

/** Define Constants Here ******************************************/
#define sample 100

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

// ============================================================
// Configuration Bits 
// ============================================================
#pragma config OSC = EC  // External 4MHz crystal
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF

/** 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 *********************************************/
	int i=12;
	
/*****************************************************************
* Function:        void main(void)
******************************************************************/
#pragma code
void main (void)
{
	ADCON1 = 0x0F;
	TRISB = 0x00;
	OpenTimer2(TIMER_INT_OFF & T2_PS_1_16);
	OpenPWM1(207);
	// 1/300 = (period + 1) * 4 * 1/4000000 * 16
	// period
	SetDCPWM1(0);
	
	for(i=12;i>0;i--)
	{
		PORTB = i;
		
		Delay10KTCYx(50);
		// 0.000 001 second per instruction
		//   500 000 instructions needed)
	}
	PORTB = 0;
	
	SetDCPWM1(200);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	Delay10KTCYx(50);
	SetDCPWM1(0);
	
	while (1)
    {

    }
}

/*****************************************************************
* Local functions go after main but before the interrupts if needed
******************************************************************/

/*****************************************************************
* Function:		void sample(void)
* Input:
* Output:
* Overview:		You change this template of a function if needed
******************************************************************/
void sampleFunction(){
}	


/*****************************************************************
* Function:        void high_isr(void)
* Input:
* Output:
* Overview:
******************************************************************/
#pragma interrupt high_isr			// declare function as high priority isr
void high_isr(void)
{
}

/******************************************************************
* Function:        void low_isr(void)
* Input:
* Output:
* Overview:
********************************************************************/
#pragma interruptlow low_isr		// declare function as low priority isr
void low_isr(void)
{
}