summaryrefslogtreecommitdiffstats
path: root/door_lock
diff options
context:
space:
mode:
authorSadeep Madurange <sadeep@asciimx.com>2024-11-03 12:36:54 +0800
committerSadeep Madurange <sadeep@asciimx.com>2024-11-03 12:36:54 +0800
commit92807aed3d3dc17fdf94d35d8b26c1d894aab9f2 (patch)
treea2877f6983cebd51a74fa03aa08779af7833e4e0 /door_lock
parentd41e70ce3463f6b239da415c5d69773fce55f9e9 (diff)
downloadsmart-home-92807aed3d3dc17fdf94d35d8b26c1d894aab9f2.tar.gz
Basic servo control.
Diffstat (limited to 'door_lock')
-rw-r--r--door_lock/Makefile41
-rw-r--r--door_lock/main.c28
2 files changed, 69 insertions, 0 deletions
diff --git a/door_lock/Makefile b/door_lock/Makefile
new file mode 100644
index 0000000..3214ccf
--- /dev/null
+++ b/door_lock/Makefile
@@ -0,0 +1,41 @@
+CC = avr-gcc
+MCU = atmega328p
+TARGET = app
+
+SRC = main.c
+OBJ = $(SRC:.c=.o)
+
+CFLAGS = -std=gnu99
+CFLAGS += -Os
+CFLAGS+= -Wall
+CFLAGS+= -mmcu=$(MCU)
+CFLAGS+= -DF_CPU=16000000UL
+CFLAGS+= -ffunction-sections -fdata-sections
+
+LDFLAGS = -mmcu=$(MCU)
+LDFLAGS += -Wl,--gc-sections
+
+HEX_FLAGS = -O ihex
+HEX_FLAGS += -j .text -j .data
+
+AVRDUDE_FLAGS = -p $(MCU)
+AVRDUDE_FLAGS += -c arduino
+AVRDUDE_FLAGS += -P /dev/cuaU0
+AVRDUDE_FLAGS += -D -U
+
+%.o: %.c
+ $(CC) $(CFLAGS) -c -o $@ $<
+
+elf: $(OBJ)
+ $(CC) $(LDFLAGS) $(OBJ) -o $(TARGET).elf
+
+hex: elf
+ avr-objcopy $(HEX_FLAGS) $(TARGET).elf $(TARGET).hex
+
+upload: hex
+ avrdude $(AVRDUDE_FLAGS) flash:w:$(TARGET).hex:i
+
+.PHONY: clean
+
+clean:
+ rm *.o *.elf *.hex
diff --git a/door_lock/main.c b/door_lock/main.c
new file mode 100644
index 0000000..7308d35
--- /dev/null
+++ b/door_lock/main.c
@@ -0,0 +1,28 @@
+#include <avr/io.h>
+#include <util/delay.h>
+
+int main(void) {
+ DDRB |= 1 << PINB1; // Set pin 9 on arduino to output
+
+ /* 1. Set Fast PWM mode 14: set WGM11, WGM12, WGM13 to 1 */
+ /* 3. Set pre-scaler of 8 */
+ /* 4. Set Fast PWM non-inverting mode */
+ TCCR1A |= (1 << WGM11) | (1 << COM1A1);
+ TCCR1B |= (1 << WGM12) | (1 << WGM13) | (1 << CS11);
+
+ /* 2. Set ICR1 register: PWM period */
+ ICR1 = 39999;
+
+ /* Offset for correction */
+ int offset = 800;
+
+ /* 5. Set duty cycle */
+ while(1) {
+ OCR1A = 3999 + offset;
+ _delay_ms(5000);
+ OCR1A = 1999 - offset;
+ _delay_ms(5000);
+ }
+
+ return 0;
+}