From 6d8764f090c6d82fae01fe3840d0f7c287b8119b Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sat, 16 Nov 2024 18:53:20 +0800 Subject: Working servo. --- door_lock/servo.c | 49 ++++++++++++++++++++++--------------------------- 1 file changed, 22 insertions(+), 27 deletions(-) (limited to 'door_lock') diff --git a/door_lock/servo.c b/door_lock/servo.c index 495f9b1..5144c28 100644 --- a/door_lock/servo.c +++ b/door_lock/servo.c @@ -5,21 +5,38 @@ #include "cmd.h" #include "serial.h" -#define PWM_MIN 1050 -#define PWM_MID 3000 +#define PWM_MIN 999 +#define PWM_MID 2999 +#define PWM_MAX 4999 + +#define PWM_LOCK PWM_MID +#define PWM_UNLOCK PWM_MAX - 100 #define SERVO_PIN PB1 #define LOCK_BTN PD6 #define UNLOCK_BTN PD7 +static inline void servo_init(void) +{ + DDRB |= (1 << SERVO_PIN); + + TCCR1A |= (1 << WGM11) | (1 << COM1A1); + TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11); + + ICR1 = 39999; + + DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN)); + PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); +} + static inline void lock(void) { - OCR1A = PWM_MID; + OCR1A = PWM_LOCK; } static inline void unlock(void) { - OCR1A = PWM_MIN; + OCR1A = PWM_UNLOCK; } static inline int is_btn_pressed(unsigned char btn) @@ -38,21 +55,9 @@ static inline void pcint2_init(void) PCMSK2 |= ((1 << PCINT22) | (1 << PCINT23)); } -static inline void servo_init(void) -{ - DDRB |= 1 << SERVO_PIN; - TCCR1A |= (1 << WGM11) | (1 << COM1A1); - TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11); - - ICR1 = 20000; - - DDRD &= ~((1 << LOCK_BTN) | (1 << UNLOCK_BTN)); - PORTD |= (1 << LOCK_BTN) | (1 << UNLOCK_BTN); -} - int main(void) { - char *s; + cli(); servo_init(); pcint2_init(); @@ -61,16 +66,6 @@ int main(void) sei(); for(;;) { - s = cmd_hash(DOOR_UNLOCK); - - if (cmd_cmp(s, DOOR_LOCK)) - serial_write_line("lock"); - else if (cmd_cmp(s, DOOR_UNLOCK)) - serial_write_line("unlock"); - else - serial_write_line("do nothing"); - - _delay_ms(1000); } return 0; -- cgit v1.2.3