unsigned short OUTM[2], OUTM1;
unsigned short ph, cap, cap2, rep, ADH_byte, ADL_byte, STET, POR;
unsigned int ADR, ADRES ;
const signed int REFC[9] = {0, 311, 0, 0, 381, 485, 311, 381, 485}; // shift 2 to right
signed int comp;
interrupt(void) {
if (INTCON.INT0IF){
rep=0;
rep=PORTB & 192;
PORTD = OUTM[rep>>6] | rep;}
INTCON.INTF=0;
}
unsigned int ADREAD(unsigned char channel){
if (channel>7) channel=0; // number of channels for 3phase system 10
GO_DONE_BIT=1;
// GO_DONE_BIT=0;
while(GO_DONE_BIT);
ADL_byte=ADRESL;
ADH_byte=ADRESH;
ADR=(ADH_byte<<8)| ADL_byte;
//ADR=ADRESH;
// if(channel>7){
// channel-=8;
//ADCON0=0x83 | (channel<<3); //activate if number of channels is higher than 7
// }
// else {
ADCON0=0x81 | (channel<<3);
//}
return ADR;}
void main() {
OPTION_REG= 0x47;
CMCON=7;
ADCON0=0x81;
ADCON1=0xB7; //first digit determines number of channels
ADCON2=0x00; //0x00
INTCON=0x90;
TRISA=0xFF;
TRISB=0xFF;
TRISD=0x00;
PORTD=0x00;
TRISC=0xFF;
TRISE=0x0F;
while(1){
cap2=1;
STET=1;
OUTM1=0;
for (cap=0; cap<3; cap++)
{ if (cap==1) cap2=cap2+2;
ADRES=ADREAD(cap2); //ahead a loop
if(PORTB.B4) comp=REFC[cap2]-ADRES;
else comp=ADRES-REFC[cap2];
if(comp>6) OUTM1 |= STET;
else if(comp>-7) OUTM1 |= (STET<<3);
STET=STET<<1;
cap2+=1; } // can introduce a variable and shift using it sht<<1
OUTM[0]=OUTM1;
STET=1;
OUTM1=0;
for (cap=0; cap<3; cap++)
{ ADRES=ADREAD(cap2); //ahead a loop
if(PORTB.B4) comp=ADRES-REFC[cap2];
else comp=REFC[cap2]-ADRES;
if(comp>6) OUTM1 |= STET;
else if(comp>-7) OUTM1 |= (STET<<3);
STET=STET<<1;
cap2+=1; } // can introduce a variable and shift using it sht<<1
OUTM[1]=OUTM1; //LSB is smallest cap.
} }