//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#define str_vers "CENTR 2024.12.23"
//+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
#include "dclr.h"
#include "lcd1602.h"
#include "PinChangeInterrupt.h"
// Пины, к которым подключены кнопки
const byte button1Pin = 7;
const byte button2Pin = 6;
// Флаги для определения нажатия кнопок
volatile bool button1Pressed = false;
volatile bool button2Pressed = false;
// Время задержки для фильтрации дребезга (в миллисекундах)
const uint16_t debounceDelay = 50;
// Переменные для хранения времени последнего изменения состояния кнопок
uint32_t lastButton1ChangeTime = 0;
uint32_t lastButton2ChangeTime = 0;
void handleInterrupt_1Pin() {
// Код, выполняемый при срабатывании прерывания
Serial.println("Прерывание button 1Pin сработало!");
}
void handleInterrupt_2Pin() {
// Код, выполняемый при срабатывании прерывания
Serial.println("Прерывание button 2Pin сработало!");
}
void setup() {
Serial.begin(9600);
Serial.println(str_vers);
lcd.init(); lcd.backlight();
lcd_prn_top(str_vers);
// Настраиваем пины на ввод
pinMode(button1Pin, INPUT_PULLUP);
pinMode(button2Pin, INPUT_PULLUP);
// Настраиваем прерывание на пинах D6 и D7 (PCINT6 и PCINT7)
//PCICR |= bit(PCIE1); // Включить прерывание по изменению состояния пинов группы B
//PCMSK1 |= bit(PCINT6) | bit(PCINT7); // Включить прерывание на пинах PCINT6 (D6) и PCINT7 (D7)
//PCintPort::attachInterrupt(button1Pin, handleInterrupt, RISING); // Подключение прерывания к пину PD7
// Attach the new PinChangeInterrupt and enable event function below
attachPCINT(digitalPinToPCINT(button1Pin), handleInterrupt_1Pin, CHANGE);
attachPinChangeInterrupt(digitalPinToPCINT(button2Pin), handleInterrupt_2Pin, RISING);
}
void loop() {
/*
gl_d.sec = millis() / 1000ul; // полное количество секунд
gl_d.timeHours = (gl_d.sec / 3600ul); // часы
gl_d.timeMins = (gl_d.sec % 3600ul) / 60ul; // минуты
gl_d.timeSecs = (gl_d.sec % 3600ul) % 60ul; // секунды
lcd_prn_clock();
delay(100);
*/
if (button1Pressed) {
// Первая кнопка была нажата, выполняем нужные действия
Serial.println("Первая кнопка нажата!");
// Сбрасываем флаг
button1Pressed = false;
}
if (button2Pressed) {
// Вторая кнопка была нажата, выполняем нужные действия
Serial.println("Вторая кнопка нажата!");
// Сбрасываем флаг
button2Pressed = false;
}
}
ISR(PCINT1_vect) {
Serial.println("PCINT1_vect");
uint32_t currentTime = millis();
// Проверяем состояние первой кнопки
if ((currentTime - lastButton1ChangeTime) >= debounceDelay) {
if (!digitalRead(button1Pin)) {
button1Pressed = true; // Устанавливаем флаг нажатия первой кнопки
}
lastButton1ChangeTime = currentTime;
}
// Проверяем состояние второй кнопки
if ((currentTime - lastButton2ChangeTime) >= debounceDelay) {
if (!digitalRead(button2Pin)) {
button2Pressed = true; // Устанавливаем флаг нажатия второй кнопки
}
lastButton2ChangeTime = currentTime;
}
}