diff options
| author | Sadeep Madurange <16175248+smadurange@users.noreply.github.com> | 2025-08-18 17:40:31 +0800 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-08-18 17:40:31 +0800 |
| commit | 240990305089770785af0fc9d20ce9bf38053eb5 (patch) | |
| tree | d00fd091b34f9b320fd8c4b235216dbaae1cb966 | |
| parent | d7edbdf11b419f152d627405fe9a368e6c1e81dd (diff) | |
| parent | e051246c659122f6b633b3c6fe93c8d4e5007662 (diff) | |
| download | fpm-door-lock-240990305089770785af0fc9d20ce9bf38053eb5.tar.gz | |
Flash LED using CTC.
Flash LED using CTC.
| -rw-r--r-- | main.c | 193 |
1 files changed, 102 insertions, 91 deletions
@@ -1,5 +1,4 @@ #include <stdint.h> -#include <stdlib.h> #include <avr/wdt.h> #include <avr/sleep.h> #include <avr/interrupt.h> @@ -8,55 +7,65 @@ #include "fpm.h" #include "uart.h" -#define BATMIN 5100 - -#define SERVO_PIN PB1 -#define SERVO_DDR DDRB - -#define PWM_MIN 500 -#define PWM_MID 1600 -#define PWM_MAX 2550 -#define PWM_TOP 19999 - -#define LED_PIN PB5 -#define LED_DDR DDRB -#define LED_PORT PORTB - -#define PWR_BATCHK PB2 -#define PWR_SERVO PB3 -#define PWR_FPM PB4 -#define PWR_DDR DDRB -#define PWR_PORT PORTB - -#define FRONT_UNLOCK_PIN PD2 -#define FRONT_LOCK_PIN PD3 -#define ENROLL_PIN PD4 -#define BACK_LOCK_PIN PD5 -#define BACK_UNLOCK_PIN PD6 - -#define INPUT_DDR DDRD -#define INPUT_PORT PORTD - -#define FPM_UNLOCK_INT INT0 -#define FPM_INT_VEC INT0_vect - -#define FRONT_LOCK_INT PCINT19 -#define ENROLL_INT PCINT20 -#define BACK_LOCK_INT PCINT21 -#define BACK_UNLOCK_INT PCINT22 -#define BTN_INT_VEC PCINT2_vect +#define BAT_MIN 5100 + +#define SRVO_PIN PB1 +#define SRVO_DDR DDRB + +#define PWM_MIN 500 +#define PWM_MID 1600 +#define PWM_MAX 2550 +#define PWM_TOP 19999 + +#define LED_PIN PD6 +#define LED_DDR DDRD +#define LED_PORT PORTD + +#define PWR_BAT PB2 +#define PWR_SRVO PB3 +#define PWR_FPM PB4 +#define PWR_DDR DDRB +#define PWR_PORT PORTB + +#define FPM_UNLOCK_PIN PC1 /*back up for FPM's touch sensor */ +#define FPM_LOCK_PIN PC2 +#define ENROLL_PIN PC3 +#define BACK_LOCK_PIN PC4 +#define BACK_UNLOCK_PIN PC5 +#define INPUT_DDR DDRC +#define INPUT_PORT PORTC + +#define FPM_UNLOCK_INT PCINT9 +#define FPM_LOCK_INT PCINT10 +#define ENROLL_INT PCINT11 +#define BACK_LOCK_INT PCINT12 +#define BACK_UNLOCK_INT PCINT13 +#define INPUT_INT PCIE1 +#define INPUT_INT_MSK PCMSK1 +#define INPUT_INT_VEC PCINT1_vect enum CTRL { NONE = 0, - LOCK_FRONT = 1, + LOCK_FPM = 1, LOCK_BACK = 2, - UNLOCK_FRONT = 3, - UNLOCK_BACK = 4, - ENROLL = 5 + UNLOCK_FPM = 3, + UNLOCK_BACK = 4, + UNLOCK_FPM_2 = 5, + ENROLL = 6 }; static volatile enum CTRL cmd = NONE; +static inline void pwron_bat(void) +{ + PWR_PORT &= ~(1 << PWR_BAT); +} + +static inline void pwroff_bat(void) +{ + PWR_PORT |= (1 << PWR_BAT); +} + static inline void pwron_fpm(void) { PWR_PORT &= ~(1 << PWR_FPM); @@ -68,57 +77,51 @@ static inline void pwroff_fpm(void) PWR_PORT |= (1 << PWR_FPM); } -static inline void pwron_servo(void) +static inline void pwron_srvo(void) { - PWR_PORT |= (1 << PWR_SERVO); + PWR_PORT |= (1 << PWR_SRVO); } -static inline void pwroff_servo(void) +static inline void pwroff_srvo(void) { - PWR_PORT &= ~(1 << PWR_SERVO); + PWR_PORT &= ~(1 << PWR_SRVO); } static inline void lock(void) { - pwron_servo(); + pwron_srvo(); OCR1A = PWM_MID; _delay_ms(500); - pwroff_servo(); + pwroff_srvo(); } static inline void unlock(void) { - pwron_servo(); + pwron_srvo(); OCR1A = PWM_MAX; _delay_ms(500); - pwroff_servo(); + pwroff_srvo(); } static inline void flash_led(void) { - for (int i = 0; i < 4; i++) { - LED_PORT |= (1 << LED_PIN); - _delay_ms(70); - LED_PORT &= ~(1 << LED_PIN); - _delay_ms(70); - } -} - -static inline void pwron_batchk(void) -{ - PWR_PORT &= ~(1 << PWR_BATCHK); + TCCR0A = (1 << COM0A0) | (1 << WGM01); + OCR0A = 255; + TCCR0B = (1 << CS02) | (1 << CS00); } -static inline void pwroff_batchk(void) +static inline void stop_led(void) { - PWR_PORT |= (1 << PWR_BATCHK); + TCCR0B = 0; + TCCR0A = 0; + LED_PORT &= ~(1 << LED_PIN); } static void check_bat(void) { uint16_t vbg, vcc; - pwron_batchk(); + pwron_bat(); ADMUX |= (1 << REFS1) | (1 << REFS0); ADCSRA |= (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); /* clk: 50-200 kHz */ @@ -133,10 +136,10 @@ static void check_bat(void) vbg = (1100UL * ADC) / 1024; ADCSRA &= ~(1 << ADEN); - pwroff_batchk(); + pwroff_bat(); vcc = (vbg * 66) / 10; /* 56k/10k divider */ - if (vcc < BATMIN) + if (vcc < BAT_MIN) flash_led(); } @@ -151,8 +154,8 @@ int main(void) WDTCSR |= (1 << WDCE) | (1 << WDE); WDTCSR = 0x00; - PWR_DDR |= (1 << PWR_BATCHK) | (1 << PWR_FPM) | (1 << PWR_SERVO); - pwroff_batchk(); + PWR_DDR |= (1 << PWR_BAT) | (1 << PWR_FPM) | (1 << PWR_SRVO); + pwroff_bat(); uart_init(); pwron_fpm(); @@ -164,40 +167,43 @@ int main(void) TCCR1B |= (1 << CS11); ICR1 = PWM_TOP; TCCR1A |= (1 << COM1A1); - SERVO_DDR |= (1 << SERVO_PIN); + SRVO_DDR |= (1 << SRVO_PIN); - /* battery check */ + /* bat check */ LED_DDR |= (1 << LED_PIN); LED_PORT &= ~(1 << LED_PIN); - /* input ports */ - INPUT_DDR &= ~((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | - (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | - (1 << ENROLL_PIN)); + DDRD &= ~(1 << PD2); /* FPM unlock pin */ + PORTD |= (1 << PD2); /* FPM unlock pin internal pull-up */ + EICRA = 0b00000000; + EIMSK = (1 << INT0); /* FPM unlock interrupt */ - INPUT_PORT |= ((1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | - (1 << FRONT_LOCK_PIN) | (1 << FRONT_UNLOCK_PIN) | + INPUT_DDR &= ~((1 << FPM_LOCK_PIN) | (1 << FPM_UNLOCK_PIN) | + (1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | (1 << ENROLL_PIN)); - EICRA = 0b00000000; - EIMSK = (1 << FPM_UNLOCK_INT); + INPUT_PORT |= ((1 << FPM_LOCK_PIN) | (1 << FPM_UNLOCK_PIN) | + (1 << BACK_LOCK_PIN) | (1 << BACK_UNLOCK_PIN) | + (1 << ENROLL_PIN)); - PCICR |= (1 << PCIE2); - PCMSK2 |= ((1 << FRONT_LOCK_INT) | (1 << ENROLL_INT) | - (1 << BACK_LOCK_INT) | (1 << BACK_UNLOCK_INT)); + PCICR |= (1 << INPUT_INT); + INPUT_INT_MSK |= ((1 << FPM_LOCK_INT) | (1 << FPM_UNLOCK_INT) | + (1 << BACK_LOCK_INT) | (1 << BACK_UNLOCK_INT) | + (1 << ENROLL_INT)); for (;;) { check_bat(); switch(cmd) { - case LOCK_FRONT: + case LOCK_FPM: lock(); fpm_led(FLASH, RED, 1); break; case LOCK_BACK: lock(); break; - case UNLOCK_FRONT: + case UNLOCK_FPM: + case UNLOCK_FPM_2: if (fpm_match()) { fpm_led(BREATHE, BLUE, 1); unlock(); @@ -228,6 +234,8 @@ int main(void) _delay_ms(500); pwroff_fpm(); + stop_led(); + set_sleep_mode(SLEEP_MODE_PWR_DOWN); sleep_enable(); sleep_bod_disable(); @@ -239,36 +247,39 @@ int main(void) pwron_fpm(); fpm_init(); } - return 0; } static inline int is_pressed(uint8_t btn) { - if (!((PIND >> btn) & 0x01)) { + if (!((PINC >> btn) & 0x01)) { _delay_ms(50); - return !((PIND >> btn) & 0x01); + return !((PINC >> btn) & 0x01); } return 0; } -ISR(FPM_INT_VEC) +ISR(INT0_vect) { - cmd = UNLOCK_FRONT; + cmd = UNLOCK_FPM; } -ISR(BTN_INT_VEC) +ISR(INPUT_INT_VEC) { cli(); - if (is_pressed(FRONT_LOCK_PIN)) - cmd = LOCK_FRONT; + if (is_pressed(FPM_LOCK_PIN)) + cmd = LOCK_FPM; + else if (is_pressed(FPM_UNLOCK_PIN)) + cmd = UNLOCK_FPM_2; else if (is_pressed(BACK_LOCK_PIN)) cmd = LOCK_BACK; else if (is_pressed(BACK_UNLOCK_PIN)) cmd = UNLOCK_BACK; else if (is_pressed(ENROLL_PIN)) cmd = ENROLL; + else + cmd = NONE; sei(); } |
