summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--door_lock/Makefile11
-rw-r--r--door_lock/main.c20
-rw-r--r--door_lock/serial.c32
-rw-r--r--door_lock/serial.h7
4 files changed, 55 insertions, 15 deletions
diff --git a/door_lock/Makefile b/door_lock/Makefile
index 3214ccf..c188507 100644
--- a/door_lock/Makefile
+++ b/door_lock/Makefile
@@ -2,15 +2,16 @@ CC = avr-gcc
MCU = atmega328p
TARGET = app
-SRC = main.c
+SRC = main.c serial.c
OBJ = $(SRC:.c=.o)
CFLAGS = -std=gnu99
CFLAGS += -Os
-CFLAGS+= -Wall
-CFLAGS+= -mmcu=$(MCU)
-CFLAGS+= -DF_CPU=16000000UL
-CFLAGS+= -ffunction-sections -fdata-sections
+CFLAGS += -Wall
+CFLAGS += -mmcu=$(MCU)
+CFLAGS += -DBAUD=115200
+CFLAGS += -DF_CPU=16000000UL
+CFLAGS += -ffunction-sections -fdata-sections
LDFLAGS = -mmcu=$(MCU)
LDFLAGS += -Wl,--gc-sections
diff --git a/door_lock/main.c b/door_lock/main.c
index 7308d35..4ee0e64 100644
--- a/door_lock/main.c
+++ b/door_lock/main.c
@@ -1,25 +1,25 @@
#include <avr/io.h>
#include <util/delay.h>
-int main(void) {
- DDRB |= 1 << PINB1; // Set pin 9 on arduino to output
+#include "serial.h"
- /* 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 */
+int main(void) {
+ // pin 9
+ DDRB |= 1 << PINB1;
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) {
+ serial_init();
+
+ for(;;) {
+ serial_write("hello, world!");
+
OCR1A = 3999 + offset;
_delay_ms(5000);
+
OCR1A = 1999 - offset;
_delay_ms(5000);
}
diff --git a/door_lock/serial.c b/door_lock/serial.c
new file mode 100644
index 0000000..4dba0e5
--- /dev/null
+++ b/door_lock/serial.c
@@ -0,0 +1,32 @@
+#include <avr/io.h>
+#include <util/setbaud.h>
+
+#include "serial.h"
+
+void serial_init(void)
+{
+ UBRR0H = UBRRH_VALUE;
+ UBRR0L = UBRRL_VALUE;
+#if USE_2X
+ UCSR0A |= (1 << U2X0);
+#else
+ UCSR0A &= ~(1 << U2X0);
+#endif
+ UCSR0B = (1 << TXEN0) | (1 << RXEN0);
+ UCSR0C = (1 << UCSZ01) | (1 << UCSZ00);
+}
+
+static void serial_write_raw(unsigned char data)
+{
+ while (!(UCSR0A & (1 << UDRE0)))
+ ;
+ UDR0 = data;
+}
+
+void serial_write(const char *s)
+{
+ for (; *s; s++)
+ serial_write_raw(*s);
+ serial_write_raw('\r');
+ serial_write_raw('\n');
+}
diff --git a/door_lock/serial.h b/door_lock/serial.h
new file mode 100644
index 0000000..8db8037
--- /dev/null
+++ b/door_lock/serial.h
@@ -0,0 +1,7 @@
+#ifndef SA_SERIAL_H
+#define SA_SERIAL_H
+
+void serial_init(void);
+void serial_write(const char *s);
+
+#endif /* SA_SERIAL_H */