// LCD module connections
sbit LCD_RS at RD0_bit;
sbit LCD_EN at RD1_bit;
sbit LCD_D4 at RD2_bit;
sbit LCD_D5 at RD3_bit;
sbit LCD_D6 at RD4_bit;
sbit LCD_D7 at RD5_bit;
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_EN_Direction at TRISD1_bit;
sbit LCD_D4_Direction at TRISD2_bit;
sbit LCD_D5_Direction at TRISD3_bit;
sbit LCD_D6_Direction at TRISD4_bit;
sbit LCD_D7_Direction at TRISD5_bit;
// End LCD module connections
// Pushbutton connections
sbit Increment_Button at RB0_bit;
sbit Decrement_Button at RB1_bit;
// Global variables
unsigned short count = 10; // Start counter at 10
unsigned short max_count = 10; // Maximum counter value
// Function prototypes
void Update_Display();
void Delay_ms(unsigned int time);
void main() {
// Configure ports
TRISB = 0xFF; // Set PORTB as input
// Initialize LCD
Lcd_Init();
Lcd_Cmd(_LCD_CLEAR);
Lcd_Cmd(_LCD_CURSOR_OFF);
// Display initial count
Update_Display();
// Main loop
while(1) {
// Check if increment button is pressed
if(Increment_Button == 0) {
while(Increment_Button == 0); // Wait for button release
count++; // Increment count
if(count > max_count) {
count = max_count; // Limit count to maximum value
}
Update_Display(); // Update display
Delay_ms(50); // Debounce delay
}
// Check if decrement button is pressed
if(Decrement_Button == 0) {
while(Decrement_Button == 0); // Wait for button release
count--; // Decrement count
if(count == 0) {
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1, 3, "PARKING FULL"); // Display "FULL" message
while(1); // Stop program execution
}
Update_Display(); // Update display
Delay_ms(50); // Debounce delay
}
}
}
// Update display with current count value
void Update_Display() {
char str[4]; // String buffer for number conversion
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1, 1, "P.SLOT LEFT: "); // Display label
IntToStr(count, str); // Convert count to string
Lcd_Out(1, 13, str); // Display count value
}
// Delay function in milliseconds
void Delay_ms(unsigned int time) {
unsigned int i, j;
for(i = 0; i < time; i++) {
for(j = 0; j < 165; j++);
}
}