/******************************************************************* * FileName: LEDs Following Buttons.c * Processor: PIC18F4520 * Compiler: MPLAB C18 v.3.06 * * Program makes the PORTB pins 0-4 follow the state of RA4 * You will be changing the code to have each LED be controlled differently * * Creation and Revisions: * Author Date Comments * (your name) ********************************************************************/ /** Header Files ***************************************************/ #include /** Constant #defines **********************************************/ #define BUTTON_PRESSED 0 #define BUTTON_NOT_PRESSED 1 #define LED_ON 1 #define LED_OFF 0 /** Configuration Bits *********************************************/ #pragma config OSC = INTIO67 //Default 1MHz Internal clock #pragma config WDT = OFF #pragma config LVP = OFF #pragma config BOREN = OFF #pragma config XINST = OFF /******************************************************************* * Function: void main(void) ********************************************************************/ #pragma code void main (void) { ADCON1 = 0x0F; TRISA = 0xFF; TRISB = 0x00; while (1) { // RB0 & RB1 control if( PORTAbits.RA4 == BUTTON_PRESSED) { PORTBbits.RB0 = LED_ON; PORTBbits.RB1 = LED_OFF; } else { PORTBbits.RB0 = LED_OFF; PORTBbits.RB1 = LED_ON; } // RB2 & RB3 control if( PORTAbits.RA4 == BUTTON_PRESSED) { PORTBbits.RB2 = LED_ON; PORTBbits.RB3 = LED_OFF; } else { PORTBbits.RB2 = LED_OFF; PORTBbits.RB3 = LED_ON; } // RB4 control if( PORTAbits.RA4 == BUTTON_PRESSED) { PORTBbits.RB4 = LED_ON; } else { PORTBbits.RB4 = LED_OFF; } } }