unsigned int adc_val;
void checkMode() {
if (RA2_bit == 0) {
PORTB = 0b00000010;
}
else {
PORTB = 0b00000100;
}
}
void driveLED(unsigned int adc_val) {
if (adc_val >= 0 && adc_val <= 40) {
PORTD = 0b00000000;
PORTC = 0;
}
else if (adc_val > 41 && adc_val <= 91) {
PORTD = 0b10000000;
PORTC = 0;
}
else if (adc_val > 92 && adc_val <= 153) {
PORTD = 0b11000000;
PORTC = 0;
}
else if (adc_val > 154 && adc_val <= 215) {
PORTD = 0b11100000;
PORTC = 0;
}
else if (adc_val > 216 && adc_val <= 276) {
PORTD = 0b11110000;
PORTC = 0;
}
else if (adc_val > 277 && adc_val <= 337) {
PORTD = 0b11111000;
PORTC = 0;
}
else if (adc_val > 338 && adc_val <= 399) {
PORTD = 0b11111100;
PORTC = 0;
}
else if (adc_val > 400 && adc_val <= 460) {
PORTD = 0b11111110;
PORTC = 0;
}
else if (adc_val > 461 && adc_val <= 521) {
PORTD = 0b11111111;
PORTC = 0;
}
else if (adc_val > 522 && adc_val <= 582) {
PORTD = 0b11111111;
PORTC = 0b10000000;
}
else if (adc_val > 583 && adc_val <= 644) {
PORTD = 0b11111111;
PORTC = 0b11000000;
}
else if (adc_val > 645 && adc_val <= 705) {
PORTD = 0b11111111;
PORTC = 0b11100000;
}
else if (adc_val > 706 && adc_val <= 767) {
PORTD = 0b11111111;
PORTC = 0b11110000;
}
else if (adc_val > 768 && adc_val <= 828) {
PORTD = 0b11111111;
PORTC = 0b11111000;
}
else if (adc_val > 829 && adc_val <= 890) {
PORTD = 0b11111111;
PORTC = 0b11111100;
}
else if (adc_val > 891 && adc_val <= 951) {
PORTD = 0b11111111;
PORTC = 0b11111110;
}
else {
PORTD = 0;
PORTC = 0;
delay_ms(30);
PORTD = 0b11111111;
PORTC = 0b11111110;
delay_ms(30);
}
}
void main() {
TRISA = 0b000111;
PORTA = 0b000000;
ADCON1 = 0b111110; // varying voltage input
TRISD = 0;
PORTD = 0;
TRISC = 0;
PORTC = 0;
TRISB = 0;
PORTB = 0;
TRISE = 0b000;
PORTE = 0b000;
INTCON.GIE = 1;
INTCON.INTE =1 ;
INTCON.INTF = 0;
TRISB0_bit = 1;
OPTION_REG.INTEDG = 1;
checkMode();
do {
adc_val = ADC_read(0);
driveLED(adc_val);
}
while(1);
}
void interrupt(void) {
do {
PORTD = 0;
PORTC = 0;
delay_ms(30);
PORTD = 0b11111111;
PORTC = 0b11111111;
delay_ms(30);
}
while(INTCON.INTF==0);
}