If you connect PB0-1 to PORTB and PE0-1 to PORTA bits 0..3 then code similar to the following can be used to read the calliper value. PE0-1 will need pull-up resistors.
Code:
#include <stdint.h>
const uint8_t pow2[8] = {1, 2, 4, 8, 16, 32, 64, 128};
uint8_t readCalliper()
{
nRBPU = 0; // enable weak pull-ups on PORTB (register bit name may be different)
TRISB = 0xFF; // all PB0-1 floating high
PORTA &= 0xF0; // get ready to pull low on PE0-1
// first try pulling each pin of PE0-1 low in succession
for(uint8_t i = 0; i < 4; i++)
{
TRISA = (TRISA & 0xF0) | ~pow2[i]; // set a single bit as output (forced low)
delayus(5);
uint8_t pv = PORTB;
if(pv != 0xFF)
{
for(uint8_t j = 0; j < 8; j++)
{
if(!(pv & 1))
return i * 16 + j;
pv >>= 1;
}
}
}
TRISA |= 0x0F; // all PE0-1 floating high
PORTB = 0; // get ready to pull low on PB0-1
// now try pulling each pin of PB0-1 low in succession
for(uint8_t j = 0; j < 8; j++)
{
TRISB = 0xFF & ~pow2[j];
delayus(5);
uint8_t pv = PORTA;
if(pv & 0x0F != 0x0F)
{
for(uint8_t i = 0; i < 4; i++)
{
if(!(pv & 1))
return i * 16 + 8 + j;
pv >>= 1;
}
}
}
return 0; // no valid reading
}