/******************************************************************* * Processor: PIC18F4520 * Compiler: MPLAB C18 * * This file does the following.... * * Author (Your name here) ********************************************************************/ /** Header Files ***************************************************/ #include #include #include #include #include /** 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 PRESSED 0 #define TICKS_FOR_300HZ 103 #define TICKS_FOR_500HZ 62 #define TICKS_FOR_700HZ 44 // If they used 4 MHz the numbers would've been //#define TICKS_FOR_300HZ 207 //#define TICKS_FOR_500HZ 124 //#define TICKS_FOR_700HZ 88 #define VOLUME 102 #define NO_VOLUME 0 /******************************************************************* * Function: void main(void) ********************************************************************/ #pragma code void main(void) { // Set the clock to 2 MHz OSCCONbits.IRCF2 = 1; OSCCONbits.IRCF1 = 0; OSCCONbits.IRCF0 = 1; // 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 outputs OpenTimer2(TIMER_INT_OFF & T2_PS_1_16); OpenPWM1(TICKS_FOR_300HZ); // Optional SetDCPWM1(NO_VOLUME); while (1) { if (PORTBbits.RB2 == PRESSED) { OpenPWM1(TICKS_FOR_700HZ); SetDCPWM1(VOLUME); } else if (PORTBbits.RB1 == PRESSED) { OpenPWM1(TICKS_FOR_500HZ); SetDCPWM1(VOLUME); } else if (PORTBbits.RB0 == PRESSED) { OpenPWM1(TICKS_FOR_300HZ); SetDCPWM1(VOLUME); } else { SetDCPWM1(NO_VOLUME); } } }