#include <stdio.h>
#include <stdint.h>
// Add your microcontroller's specific header file
// Add a header file for delay functions
// --- Pin Definitions (Adapt to your hardware - 8-bit LCD Mode) ---
#define LCD_RS_PIN PD0
#define LCD_EN_PIN PA13
#define LCD_D0_PIN PB0
#define LCD_D1_PIN PB1
#define LCD_D2_PIN PB2
#define LCD_D3_PIN PB3
#define LCD_D4_PIN PB4
#define LCD_D5_PIN PB5
#define LCD_D6_PIN PB6
#define LCD_D7_PIN PB7
#define BUTTON_PIN PC13
#define BUTTON_PRESSED_LEVEL 0 // Adjust based on your wiring
// Keypad Row Pins
#define ROW1_PIN PD3
#define ROW2_PIN PA5
#define ROW3_PIN PA6
#define ROW4_PIN PA7
// Keypad Column Pins
#define COL1_PIN PC7
#define COL2_PIN PA9
#define COL3_PIN PA15
#define COL4_PIN PA10
// --- LCD Command Definitions ---
#define LCD_CLEAR 0x01
#define LCD_HOME 0x02
#define LCD_ENTRY_MODE_INC 0x06
#define LCD_CURSOR_OFF 0x0C
#define LCD_CURSOR_ON 0x0E
#define LCD_FUNCTION_8BIT 0x38 // For 8-bit interface, 2 lines, 5x8 font
// --- Global Variables ---
uint8_t display_on = 1;
uint8_t button_press_count = 0;
uint32_t last_press_time = 0;
char display_buffer[20 + 1]; // Buffer for 20 characters + null terminator
uint8_t buffer_index = 0;
// --- Function Prototypes ---
void initialize_ports(); // Configure GPIO pins
void lcd_init();
void lcd_command(unsigned char command);
void lcd_data(unsigned char data);
void lcd_string(char *str);
void lcd_display_buffer();
unsigned char keypad_scan();
uint8_t button_read_debounced();
uint8_t button_raw_read();
void delay_ms(uint16_t ms); // Implement your delay function
uint32_t get_time_ms(); // Implement a function to get current time in milliseconds
int main(void) {
initialize_ports();
lcd_init();
lcd_string("Keypad Input:");
while (1) {
// Handle Button Press with Debouncing
if (button_read_debounced()) {
uint32_t current_time = get_time_ms();
if (current_time - last_press_time < 3000) {
button_press_count++;
if (button_press_count >= 3) {
lcd_command(LCD_CLEAR);
for (int i = 0; i < 20; i++) {
display_buffer[i] = ' ';
}
display_buffer[20] = '\0';
buffer_index = 0;
button_press_count = 0;
}
} else {
button_press_count = 1;
}
last_press_time = current_time;
display_on = !display_on;
if (display_on) {
lcd_command(LCD_CURSOR_ON);
lcd_display_buffer();
} else {
lcd_command(LCD_CURSOR_OFF);
lcd_command(LCD_CLEAR); // Optionally clear when off
}
}
// Handle Keypad Input and Display (if display is on)
if (display_on) {
unsigned char key = keypad_scan();
if (key != '\0') {
display_buffer[buffer_index % 20] = key;
buffer_index++;
lcd_command(LCD_CLEAR);
lcd_string("Keypad Input:");
// Display the rolling buffer
for (int i = 0; i < 20; i++) {
lcd_data(display_buffer[(buffer_index - 20 + i + 20) % 20]);
}
delay_ms(200); // Simple delay for key press feedback
}
}
delay_ms(10); // Small delay for the main loop
}
return 0;
}
// --- Implement your microcontroller's GPIO initialization here ---
void initialize_ports() {
pinMode(LCD_RS_PIN, OUTPUT);
pinMode(LCD_EN_PIN, OUTPUT);
pinMode(LCD_D0_PIN, OUTPUT);
pinMode(LCD_D1_PIN, OUTPUT);
pinMode(LCD_D2_PIN, OUTPUT);
pinMode(LCD_D3_PIN, OUTPUT);
pinMode(LCD_D4_PIN, OUTPUT);
pinMode(LCD_D5_PIN, OUTPUT);
pinMode(LCD_D6_PIN, OUTPUT);
pinMode(LCD_D7_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(ROW1_PIN, OUTPUT);
pinMode(ROW2_PIN, OUTPUT);
pinMode(ROW3_PIN, OUTPUT);
pinMode(ROW4_PIN, OUTPUT);
pinMode(COL1_PIN, INPUT_PULLUP);
pinMode(COL2_PIN, INPUT_PULLUP);
pinMode(COL3_PIN, INPUT_PULLUP);
pinMode(COL4_PIN, INPUT_PULLUP);
}
// --- Implement LCD functions (8-bit Mode) ---
void lcd_init() {
delay(50); // Initial delay
lcd_command(0x38); // Function set: 8-bit interface, 2 lines, 5x8 font
delay(5);
lcd_command(0x38); // Function set again
delay(1);
lcd_command(0x38); // Function set a third time
lcd_command(LCD_CURSOR_OFF); // Display control: Display ON, Cursor OFF, Blink OFF
lcd_command(LCD_ENTRY_MODE_INC); // Entry mode set: Increment cursor, No display shift
lcd_command(LCD_CLEAR); // Clear display
delay(2);
}
void lcd_command(unsigned char command) {
digitalWrite(LCD_RS_PIN, LOW);
// Output the command to the 8 data lines
digitalWrite(LCD_EN_PIN, HIGH);
delay(1);
digitalWrite(LCD_EN_PIN,LOW);
delay(1);
}
void lcd_data(unsigned char data) {
// Set RS HIGH for data
// Output the data to the 8 data lines
// Set EN HIGH, delay, set EN LOW
delay_ms(1);
}
void lcd_string(char *str) {
while (*str) {
lcd_data(*str++);
}
}
void lcd_display_buffer() {
lcd_command(LCD_CLEAR);
lcd_string("Keypad Input:");
for (int i = 0; i < 20; i++) {
lcd_data(display_buffer[i]);
}
}
// --- Implement Keypad scanning function ---
unsigned char keypad_scan() {
char keys[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
// Set row pins as output, one at a time, low
// Read column pins
// Implement basic debouncing within the scan function
// Return the pressed key or '\0' if none
return '\0'; // Placeholder
}