/** Header Files ************************/     
#include <p18f4520.h> 
#include <timers.h>
#include <pwm.h>
#include "LCD Module.h"

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

/** Define Constants Here ****************/
#define VOLUME 100         // Part B
#define NO_VOLUME 0        // Part C
#define A_MID_440_HZ 141   // Part D
#define G_MID_392_HZ 158   // Part D
#define C_MID_262_HZ 237   // Part B

/****************************************/
#pragma code
void main (void)
{
  // Part A
  XLCDInit();
  XLCDClear();
  XLCDL1home();
  XLCDPutRomString("Twinkle Twinkle");
  XLCDL2home();
  XLCDPutRomString("4 4 0 0 40 40 0");

  ADCON1 = 0x0F;        // Part C
  TRISBbits.TRISB0 = 1; // Part D
  TRISAbits.TRISA4 = 1; // Part C
  
  OpenTimer2(TIMER_INT_OFF & T2_PS_1_16); // B
  OpenPWM1(C_MID_262_HZ);  // Part B
  //SetDCPWM1(VOLUME);     // Part B
  SetDCPWM1(NO_VOLUME);    // Part C
  while (1) {
    if (PORTAbits.RA4 == 0 && PORTBbits.RB0 == 0) {
      OpenPWM1(A_MID_440_HZ); // Part D
      SetDCPWM1(VOLUME);      // Part D
    } else if (PORTAbits.RA4 == 0) {
      OpenPWM1(C_MID_262_HZ); // Part C
      SetDCPWM1(VOLUME);      // Part C
    } else if (PORTBbits.RB0 == 0) {
      OpenPWM1(G_MID_392_HZ); // Part D
      SetDCPWM1(VOLUME);      // Part D
    } else {
      SetDCPWM1(NO_VOLUME);   // Part C
    }
  }
}