From 92807aed3d3dc17fdf94d35d8b26c1d894aab9f2 Mon Sep 17 00:00:00 2001 From: Sadeep Madurange Date: Sun, 3 Nov 2024 12:36:54 +0800 Subject: Basic servo control. --- door_lock/Makefile | 41 +++++++++++++++++++++++++++++++++++++++++ door_lock/main.c | 28 ++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 door_lock/Makefile create mode 100644 door_lock/main.c (limited to 'door_lock') 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 +#include + +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; +} -- cgit v1.2.3