#include<stdio.h>
//#include"ReadKeyPad.h"
//#define I2C_ADDR 0x27 << 1 // LCD I2C address (shifted for STM32)
volatile uint32_t button_press_count = 0;
volatile uint32_t last_button_time = 0;
char display_buffer[21] = {0};
uint8_t buffer_index = 0;
uint8_t display_on = 1;
void delay_ms(uint32_t ms) {
for (uint32_t i = 0; i < ms * 800; i++) {
__NOP();
}
}
void process_key(char key)
{
display_buffer[buffer_index++] = key;
if (buffer_index >= 20) buffer_index = 0;
// lcd_update_display();
}
const char keypad_map[4][4] = {
{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void Keypad_Init(void)
{
// Enable GPIOA clock
RCC->IOPENR |= (1 << 0);
// PA0–PA3 as output (rows)
GPIOA->MODER &= ~(0xFFF << 0); // Clear MODER[7:0]
GPIOA->MODER |= (0x555 << 0); // Set MODER0-3 to output (01)
GPIOA->OTYPER &= ~(0x0F); // Push-pull for PA0–PA3
GPIOA->PUPDR &= ~(0xFF << 0);// No pull-up/down for PA0–PA3
// PA4–PA7 as input (columns)
GPIOA->MODER &= ~(0xFF << 8); // Set MODER4-7 to input (00)
GPIOA->PUPDR &= ~(0xFF << 8); // Clear PUPDR
GPIOA->PUPDR |= (0xAA << 8); // Pull-down for PA4–PA7
}
char Keypad_Read(void)
{
for (int row = 0; row < 4; row++) {
GPIOA->ODR &= ~(0x0F); // Clear all rows (PA0–PA3)
GPIOA->ODR |= (1 << row); // Set current row HIGH
delay_ms(1); // Allow signals to settle
uint8_t col_data = (GPIOA->IDR >> 4) & 0x0F; // Read PA4–PA7
delay_ms(20);
if (col_data) { // If any column is HIGH
delay_ms(20); // Debounce
// Check which column is active manually (avoid second for-loop)
int col = -1;
if (col_data & 0x01) col = 0;
else if (col_data & 0x02) col = 1;
else if (col_data & 0x04) col = 2;
else if (col_data & 0x08) col = 3;
if (col != -1) {
// Wait for key release
while ((GPIOA->IDR >> (col + 4)) & 0x01);
return keypad_map[row][col];
}
}
}
return '\0'; // No key pressed
}
void setup() {
Keypad_Init();
Serial.begin(9600);
Serial.print("Hey stm32: \n");
}
void loop() {
char key = Keypad_Read();
if(key)
{
Serial.print("Key Pressed: \n");
Serial.println(key);
//delay(1000);
}
}