CMPCR2 = 0x10; //Output comparator result after 16 debounce clocks
CMPCR1 = 0x00;
CMPCR1 &= ~0x02; //Disable comparator output
CMPCR1 |= 0x80; //Enable comparator module
while (1)
{
v = 0x00; //Voltage <2.5V
P1 = 0xfe; //P1.0 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x01; //Voltage>2.5V
P1 = 0xfd; //P1.1 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x03; //Voltage>3.0V
P1 = 0xfb; //P1.2 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x07; //Voltage>3.5V
P1 = 0xf7; //P1.3 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x0f; //Voltage>4.0V
P1 = 0xef; //P1.4 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x1f; //Voltage>4.5V
P1 = 0xdf; //P1.5 outputs 0
delay();
if (!(CMPCR1 & 0x01)) goto ShowVol;
v = 0x3f; //Voltage>5.0V
ShowVol:
P1 = 0xff;
P0 = ~v;
}
}
Assembly code
;Operating frequency for test is 11.0592MHz
CMPEXCFG EQU 0FEFAH
P_SW2 DATA 0BAH
CMPCR1 DATA 0E6H
CMPCR2 DATA 0E7H
P0M1 DATA 093H
P0M0 DATA 094H
P1M1 DATA 091H
P1M0 DATA 092H
P2M0 DATA 096H
P2M1 DATA 095H
P2M1 DATA 095H
P2M0 DATA 096H
P3M1 DATA 0B1H
P3M0 DATA 0B2H