uint8_t groupPin[] = {19, 18, 17, 20};
// Порти розрядів DIG1,DIG2,DIG3,DIG4
uint8_t segmPin[] = {12, 13, 7, 8, 9, 11, 10, 6};
// Сегменти: A, B, C, D, E, F, G, DP
uint8_t code[] = {63, 6, 91, 79, 102, 109, 125, 39, 127, 111, 0};
// Порти підключення клавіатури:
byte colPort[] = {A11, A10, A9, A8};
// Стовпці С1, С2, С3, С4
byte rowPort[] = {A15, A14, A13, A12};
// Рядки R1, R2, R3, R4
char keys[4][4] = {{'1', '2', '3', 'A'},
{'4', '5', '6', 'B'},
{'7', '8', '9', 'C'},
{'*', '0', '#', 'D'}
};
void setup() {
// put your setup code here, to run once:
for (uint8_t segm = 0; segm < 8; segm++) {
pinMode(segmPin[segm], OUTPUT);
}
for (uint8_t group = 0; group < 4; group++) {
pinMode(groupPin[group], OUTPUT);
}
for (uint8_t col = 0; col < 4; col++) {
pinMode(colPort[col], INPUT);
}
for (uint8_t row = 0; row < 4; row++) {
pinMode(rowPort[row], INPUT_PULLUP);
}
}
void loop() {
char key = getKey();
uint8_t numbers[] = {0, 0, 0, key - '0'};
displayArray(numbers);
}
void display(uint8_t symbol) {
for (uint8_t segm = 0; segm < 7; segm++) {
bool segmState = bitRead(code[symbol], segm);
digitalWrite(segmPin[segm], segmState);
}
}
void clearSegments() { // Процедура вимкнення сегментів
for (uint8_t segm = 0; segm < 7; segm++) {
// Портам усіх сегментів задаємо низький рівень
digitalWrite(segmPin[segm], LOW);
}
}
void setGroup(uint8_t newGroup) {
for (uint8_t group = 0; group < 4; group++) {
if (group == newGroup) // На порті заданої групи
// встановлюємо низький рівень, щоб активувати групу
digitalWrite(groupPin[group], LOW);
else // На всіх інших портах груп – високий рівень
digitalWrite(groupPin[group], HIGH);
}
}
void displayArray(uint8_t displayValue[]) {
for (uint8_t group = 0; group < 4; group++) {
clearSegments();
setGroup(group); // Активуємо групу (розряд)
display(displayValue[group]); // Виводимо значення
delay(10); // Затримка відображення символа
}
}
void setCol(uint8_t newCol) {
for (uint8_t col = 0; col < 4; col++)
// Переводимо усі порти у режим введення
pinMode(colPort[col], INPUT);
// Порт заданої групи налаштовуємо на виведення
pinMode(colPort[newCol], OUTPUT);
// Встановлюємо низький рівень на виході
digitalWrite(colPort[newCol], LOW);
}
char getKey() {
for (uint8_t col = 0; col < 4; col++) {
setCol(col); // Активуємо стовпчик
delay(1); // Затримка на перехідні процеси
for (uint8_t row = 0; row < 4; row++)
// Перевіряємо усі рядки
if (digitalRead(rowPort[row]) == LOW)
// Якщо кнопка натиснена - повертаємо її символ
return keys[row][col];
}
// Якщо нічого не натиснено - поветаємо нуль
return 0;
}