From ea30b232f637282dfa87ded8c7da7596430f1de4 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Tue, 5 Nov 2024 16:51:55 +0800 Subject: Function to check btn press. --- door_lock/main.c | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) (limited to 'door_lock/main.c') diff --git a/door_lock/main.c b/door_lock/main.c index e02849c..fa8b046 100644 --- a/door_lock/main.c +++ b/door_lock/main.c @@ -1,6 +1,8 @@ #include + #include #include +#include #define PWM_MIN 1200 #define PWM_MID 3000 @@ -22,12 +24,21 @@ static inline void servo_init(void) PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); } +static inline void int0_init(void) +{ + EIMSK |= (1 << INT0); + EICRA |= (1 << ISC00); +} + +static inline uint8_t is_btn_pressed(uint8_t btn) +{ + return !((PIND >> btn) & 0x01); +} + int main(void) { servo_init(); - - EICRA |= (1 << ISC00); - EIMSK |= (1 << INT0); + int0_init(); sei(); @@ -37,11 +48,11 @@ int main(void) return 0; } -ISR (INT0_vect) +ISR(INT0_vect) { - if (!((PIND >> LOCK_BTN) & 0x01)) + if (is_btn_pressed(LOCK_BTN)) OCR1A = PWM_MID; - if (!((PIND >> UNLOCK_BTN) & 0x01)) + if (is_btn_pressed(UNLOCK_BTN)) OCR1A = PWM_MIN; } -- cgit v1.2.3