From cbb3c9832bbe31d741930d43d3e21a04abeb12f8 Mon Sep 17 00:00:00 2001 From: VG Date: Wed, 28 Jan 2015 00:03:35 +0100 Subject: first commit --- .gitignore | 2 ++ Makefile | 35 +++++++++++++++++++++++++++++ main.c | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ readme.rst | 12 ++++++++++ uart.h | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 190 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c create mode 100644 readme.rst create mode 100644 uart.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..597ad3c --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +main.elf +main.hex diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..22606ad --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +# settings for arduino nano v3 +DEVICE = atmega328p +CLOCK = 16000000 +PORT=/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A9SBZLHD-if00-port0 +#AVRDUDE=avrdude -V -F -p m328p -c arduino -b 115200 -P$(PORT) +#AVRDUDE=avrdude -p $(DEVICE) -c arduino -b 115200 -P $(PORT) +AVRDUDE=avrdude -p $(DEVICE) -c arduino -b 57600 -P $(PORT) + +DEFS = -DF_CPU=$(CLOCK)UL +LIBS = -Wl,--relax,--gc-sections,--print-gc-sections,--entry=main +LIBS = -Wl,--entry=main,--gc-sections,--rela +CFLAGS = -Wall -Werror -pedantic -std=c99 +CFLAGS += -O2 -funsigned-bitfields -fpack-struct -fshort-enums +CFLAGS += -mmcu=$(DEVICE) $(DEFS) +#LDFLAGS = $(LIBS) + +all: main.hex size + +main.hex: main.c + avr-gcc -o main.elf $? $(CFLAGS) $(LDFLAGS) + avr-objcopy -j .text -j .data -O ihex main.elf main.hex + +f: flash +flash: all + $(AVRDUDE) -U flash:w:main.hex:i + +clean: + rm -f *.o *.elf *.hex + +size: + avr-size --mcu=$(DEVICE) -t -A main.elf + avr-size --mcu=$(DEVICE) -C main.elf + avr-nm --size-sort main.elf + + diff --git a/main.c b/main.c new file mode 100644 index 0000000..cd3cf6e --- /dev/null +++ b/main.c @@ -0,0 +1,65 @@ +#include +#include +#include +#include "uart.h" + +#define SET_PWM(a) (OCR0A = (a)) + +void setup(void); + +int main(void) +{ + uint8_t output = 0, c; + + setup(); + + for (;;) { + switch((c = uart_getchar())) { + case '?': break; + case 'n': + case '0': output = 0; break; + case 'y': + case '1': output = 1; break; + case 't': + case 'T': output ^= 1; break; + } + + uart_putchar(c); + + if (output) + SET_PWM(40); + else + SET_PWM(1); + + _delay_ms(1000); + SET_PWM(0); + } + + return 0; +} + +void setup(void) +{ + /* serial */ + uart_init(); + + /* Initialize timer for PWM + *** PWM with timer1 management: + * TCCR1A-COM1A1: + * TCCR1A-WGM10: + * TOP = 0x00FF + * frequency is : 12 MHz(fclk) / 256(prscl) / 255 (top)/ 2 = 91.91Hz + * frequency is : 18.432 MHz(fclk) / 256(prscl) / 255 (top)/ 2 = 141.18Hz + * frequency is : 18.432 MHz(fclk) / 1024(prscl) / 255 (top)/ 2 = 35.29Hz + * frequency is : 16 MHz(fclk) / 1024(prscl) / 255 (top)/ 2 = 30.64Hz + * TCCR1B CS12 = 256 prescaler, CS12 + CS10 = 1024 prescaler + * + * 23/24 =~ 1.5ms + */ + TCCR0A |= _BV(COM1A1) | _BV(WGM10); + TCCR0B |= _BV(CS12) | _BV(CS10); + /* TIMSK0 |= _BV(TOIE0); */ + + /* pwm output; */ + DDRD |= 0xFF; +} diff --git a/readme.rst b/readme.rst new file mode 100644 index 0000000..4737ef5 --- /dev/null +++ b/readme.rst @@ -0,0 +1,12 @@ +Simple project to switch lights on/off with a servo (for fun) +############################################################# + +This is a two night project to allow a little linux board to control the +lights of the home. It does it by flapping a switch with a servo going from +a position to another. + +Simple control is done on the serial line. Exemple with python: + + import serial + s = serial.Serial('/dev/serial/device', baudrate=9600) + s.write(b't') # toggle switch diff --git a/uart.h b/uart.h new file mode 100644 index 0000000..22e0655 --- /dev/null +++ b/uart.h @@ -0,0 +1,76 @@ +/* #define F_CPU 16000000UL <= defined in Makefile */ +#define BAUD 9600 + +/* This file is *highly* inspired from: + * http://www.appelsiini.net/2011/simple-usart-with-avr-libc + */ + +#include + +#if 0 +static inline void uart_init() +{ + UCSRB |= (1 << RXEN) | (1 << TXEN); + UCSRC |= (1 << UCSZ0) | (1 << UCSZ1); +#define BAUD 115200 +#include + UBRRH = UBRRH_VALUE; + UBRRL = UBRRL_VALUE; +#if USE_2X + UCSRA |= (1 << U2X); +#else + UCSRA &= ~(1 << U2X); +#endif +} +#endif + +void uart_init(void) +{ + UBRR0H = UBRRH_VALUE; + UBRR0L = UBRRL_VALUE; + +#if USE_2X + UCSR0A |= _BV(U2X0); +#else + UCSR0A &= ~(_BV(U2X0)); +#endif + + UCSR0C = _BV(UCSZ01) | _BV(UCSZ00); /* 8-bit data */ + UCSR0B = _BV(RXEN0) | _BV(TXEN0); /* Enable RX and TX */ +} + +void uart_putchar(char c) +{ + UDR0 = c; + loop_until_bit_is_set(UCSR0A, TXC0); /* Wait until transmission ready. */ +} + +char uart_getchar(void) +{ + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ + return UDR0; +} + +#if 0 +/* alternatives for stream uses */ +void uart_putchar(char c, FILE *stream) +{ + if (c == '\n') { + uart_putchar('\r', stream); + } + loop_until_bit_is_set(UCSR0A, UDRE0); + UDR0 = c; +} + +char uart_getchar(FILE *stream) +{ + loop_until_bit_is_set(UCSR0A, RXC0); /* Wait until data exists. */ + return UDR0; +} +#endif + +#if 0 +FILE uart_output = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); +FILE uart_input = FDEV_SETUP_STREAM(NULL, uart_getchar, _FDEV_SETUP_READ); +FILE uart_io FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW); +#endif -- cgit v1.2.3