/********************************************************************
 * 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 <stdio.h>
#include <adc.h>
#include <timers.h>
#include <pwm.h>

/** Configuration Bits *********************************************/
#pragma config OSC = INTIO67
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config BOREN = OFF
#pragma config XINST = OFF

/** Define Constants Here ******************************************/
#define DUTY_CYCLE_30 307
#define TIMER1_START 42098


/** 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 ***********************************************/
int isFlashing = 1;

/*******************************************************************
 * Function:        void main(void)
 ********************************************************************/
#pragma code

void main(void) {
    // Set the clock to 31kHz
    OSCCONbits.IRCF2 = 0;
    OSCCONbits.IRCF1 = 0;
    OSCCONbits.IRCF0 = 0;

    // Pin IO Setup
    OpenADC(ADC_FOSC_8 & ADC_RIGHT_JUST & ADC_12_TAD,
            ADC_CH0 & ADC_INT_OFF & ADC_REF_VDD_VSS,
            0x0B); // Four analog pins
    TRISA = 0xFF; // All of PORTA input
    TRISB = 0xFF; // All of PORTB input
    TRISC = 0x00; // All of PORTC output
    TRISD = 0x00; // All of PORTD output
    PORTC = 0x00; // Turn off all 8 Port C LEDs


    OpenTimer2(TIMER_INT_OFF & T2_PS_1_16);

    OpenPWM1(180);
    SetDCPWM1(DUTY_CYCLE_30);

    // Interrupt setup
    RCONbits.IPEN = 1; // Put the interrupts into Priority Mode

    OpenTimer1(TIMER_INT_ON & T1_16BIT_RW & T1_SOURCE_INT & T1_PS_1_1 & T1_OSC1EN_OFF);
    IPR1bits.TMR1IP = 1;
    WriteTimer1(TIMER1_START);

    INTCONbits.GIEH = 1; // Turn on high priority interrupts

    // This area happens once
    // Good for initializing and things that need to happen once

    while (1) {
        // This area loops forever
    }
}

/*****************************************************************
 * Function:        void high_isr(void)
 ******************************************************************/
#pragma interrupt high_isr

void high_isr(void) {
    if (PIR1bits.TMR1IF) { // Timer 1 interrupt
        PIR1bits.TMR1IF = 0;
        WriteTimer1(TIMER1_START);
        if (isFlashing) {
            isFlashing = 0;
            SetDCPWM1(0);
        } else {
            isFlashing = 1;
            SetDCPWM1(DUTY_CYCLE_30);
        }
    }
}

/******************************************************************
 * Function:        void low_isr(void)
 ********************************************************************/
#pragma interruptlow low_isr

void low_isr(void) {
    // Add code here for the low priority Interrupt Service Routine (ISR)
}

#pragma code