#include <stdint.h>
unsigned char i;
unsigned char j;
unsigned char pv;
const uint8_t pow2[8] = {1,2,4,8,16,32,64,128}; //allows fast way to get a power of two value
uint8_t readCalliper()
{
OPTION_REG.NOT_RBPU = 0; //enable weak pull-ups on PORTB
TRISB = 0xFF; //all PBO-1(8pin) floating high
PORTA&=0xF0; //set the output value of pins on PEO-1 to 0 these pins are kept as inputs,excepts when one is driven low
//get ready to pull low on PEO-1
//first try pulling each pin of PEO-1 low in succession
for (i=0;i<4;i++)
{
TRISA = (TRISA&0xF0) | (~pow2&0x0F); //set PEO-1(4pin) as output so that it can be driven low
//set a single bit as output (forced low)
delay_us(5);
pv=PORTB;
if (pv != 0xFF)
{
for(j=0;j<8;j++)
{
if(!(pv&1))
return i*16+j;
pv>>=1;
}
}
}
TRISA |= 0x0F; //all PEO-1 floating high
PORTB=0; //get ready to pull low on PBO-1
for (j=0;j<8;j++)
{
TRISB=~pow2[j]; //set PBO-1 pins as output so that it can be driven low
delay_us(5);
pv=PORTA;
if (pv&0x0F !=0x0F) //just checking if one of the PEO-1 pins have been pulled low
{
for(i=0;i<4;i++)
{
if(!(pv&1))
return i*16+8*j;
pv>>=1;
}
}
}
return 0; //no valid reading
}
//LCD Module Connections
sbit LCD_RS at RD4_bit;
sbit LCD_EN at RD5_bit;
sbit LCD_D4 at RD0_bit;
sbit LCD_D5 at RD1_bit;
sbit LCD_D6 at RD2_bit;
sbit LCD_D7 at RD3_bit;
sbit LCD_RS_Direction at TRISD4_bit;
sbit LCD_EN_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD0_bit;
sbit LCD_D5_Direction at TRISD1_bit;
sbit LCD_D6_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD3_bit;
unsigned char value=readCalliper();
char valueText[4]={'0'+(value/100),'0'+((value/10)%10),'0'+(value%10)};
void main()
{
ANSEL = 0; // Configure AN pins as digital I/O
ANSELH = 0;
C1ON_bit = 0; // Disable comparators
C2ON_bit = 0;
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,2,"25");
Lcd_Out(1,6,"F");
Lcd_Out(1,13,"BF%");
Lcd_Out(2,8,valueText);
}