/** Header Files ***************************************************/ #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 /** Global Variables ***********************************************/ int RA3result; /******************************************************************* * Function: void main(void) ********************************************************************/ #pragma code void main(void) { // Set the clock to 4 MHz OSCCONbits.IRCF2 = 1; OSCCONbits.IRCF1 = 1; 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 outputs // This area happens once // Good for initializing and things that need to happen once while (1) { SetChanADC( ADC_CH3 ); // Select the pin ConvertADC(); // Start conversion while( BusyADC() ); // Wait for completion RA3result = ReadADC(); // Read result if (RA3result < 100) { PORTC = 0b00001111; } else if (RA3result < 200) { PORTC = 0b00000111; } else if (RA3result < 300) { PORTC = 0b00000011; } else if (RA3result < 400) { PORTC = 0b00000001; } else if (RA3result < 600) { PORTC = 0b00000000; } else if (RA3result < 700) { PORTC = 0b00010000; } else if (RA3result < 800) { PORTC = 0b00110000; } else if (RA3result < 900) { PORTC = 0b01110000; } else { PORTC = 0b11110000; } // or // You could divide by 100 then look at the hundreds digit. // You could even use a switch statement with that approach } }