unsigned int ADRES;
unsigned short OUTM[3];
unsigned short ph, cap, cap2;
unsigned short REFC[3][3];
unsigned char ADL_byte, ADH_byte;
unsigned int ADR;
interrupt(void) {
unsigned short rep;
if (INTCON.INTF)
{
INTCON.INTF=0;
rep=0;
rep=(PORTB.B7<<1)|PORTB.B6;
PORTD= (OUTM[rep]<<2) | rep;
}
}
unsigned int ADREAD(unsigned char channel){
if(channel.B3 != 0){
channel.B3=0;
ADCON0=0x83 | (channel<<3);
}
else
{ ADCON0=0x81 | (channel<<3);
}
// delay_us(20)
GO_DONE_BIT=1;
while(GO_DONE_BIT);
ADL_byte=ADRESL;
ADH_byte=ADRESH;
ADR=(ADH_byte<<8)| ADL_byte;
return ADR;
}
void main() {
OPTION_REG= 0xc7;
INTCON=0x90;
CMCON=7;
ADCON1=0xB0;
ADCON2=0x30;
TRISD=0x00;
PORTD=0x00;
while(1)
{
cap2=0 ;
for (ph=0; ph<3; ph++)
{
for (cap=0; cap<3; cap++)
{ if (ph==0 || cap>1) cap2=cap2+2;
ADRES=ADREAD(cap2);
ADRES=ADRES>>2;
if(ADRES<REFC[ph][cap]) OUTM[ph] |= (1<<cap);
else if(ADRES>REFC[ph][cap]) OUTM[ph]&= ~(1<<cap) ;
else OUTM[ph] |= (1<<(cap+3))=1;
cap2+=1 ;
}
}
}
}