sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
//__CONFIG(0x3FF1);
#define sA PORTA.F0
#define sB PORTA.F1
#define sL PORTA.F2
//assuming sensors are variables sA and sB, and lights switch is sL
unsigned int people = 0; // counter for people inside the room
char AllowNextCount = 0; // boolean indicating if allowing next count or not
short int InOut;
unsigned char str_people[17], str_sL[17]; // 1 = entering; -1 = exiting
void main(void){
// TRIS registers
TRISA = 0b00000011; // RA0 and RA1 are inputs
TRISB = 0x00;
ADCON1 = 0b10000111;
CMCON = 0x07;
// Initializing LCD
LCD_Init();
// Set data on display the first time
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
Lcd_Out(1,1,"Counter: ");
IntToStr(people, str_people);
Lcd_Out(1,11,str_people);
LCD_Out(2,1,"Lights: ");
IntToStr(sL ,str_sL);
LCD_Out(2,11,str_sL);
while (1){
if ((sA==0) && (sB==1)){
// entering
InOut = 1;
}
if ((sA==1) && (sB==0)){
// exiting
InOut = -1;
}
if ((sA==1) && (sB==1) && (AllowNextCount==1)){
// only when both sensors are touched validate the people counter
people += InOut;
if (people > 0) sL = 1; // Turn/keep lights on as long as People greather than 0
else sL = 0; // otherwise, turn them off
// count once then block counting until the same person has finished
//entering/exiting
AllowNextCount=0;
// Update data on display
Lcd_Out(1,1,"Counter: ");
IntToStr(people, str_people);
Lcd_Out(1,11,str_people);
LCD_Out(2,1,"Lights: ");
IntToStr(sL ,str_sL);
LCD_Out(2,11,str_sL);
}
if ((sA==0) && (sB==0)){
// it gets 0;0 only when someone has finished entering/exiting
// or at turn on; so now allow to counting again
AllowNextCount=1;
}
}
}