From c9f2f6463634d7fcebdfc0e611d7e8305d6ce188 Mon Sep 17 00:00:00 2001 From: Olaf Rempel Date: Mon, 14 Nov 2011 20:23:01 +0100 Subject: [PATCH] working 16ch RGB version --- .gitignore | 6 ++ Makefile | 52 ++++++++++++ main.c | 236 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 294 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6d36148 --- /dev/null +++ b/.gitignore @@ -0,0 +1,6 @@ +*.o +*.elf +*.bin +*.hex +*.lst +*.map diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d74ea36 --- /dev/null +++ b/Makefile @@ -0,0 +1,52 @@ +PRG = 16ch_pwm +OBJ = main.o +MCU_TARGET = atmega16 +OPTIMIZE = -O2 + +DEFS = +LIBS = + +# You should not have to change anything below here. + +CC = avr-gcc + +# Override is only needed by avr-lib build system. + +override CFLAGS = -g -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS) +override LDFLAGS = -Wl,-Map,$(PRG).map + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size + +all: $(PRG).elf lst text + $(SIZE) -x -A $(PRG).elf + +$(PRG).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -rf *.o *.lst *.map $(PRG).elf *.hex *.bin + +lst: $(PRG).lst + +%.lst: %.elf + $(OBJDUMP) -h -S $< > $@ + +# Rules for building the .text rom images + +text: hex bin + +hex: $(PRG).hex +bin: $(PRG).bin + +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -O ihex $< $@ + +%.bin: %.elf + $(OBJCOPY) -j .text -j .data -O binary $< $@ + +install: text +# uisp -dprog=stk200 --erase --upload if=$(PRG).hex --verify +# avrdude -p m16 -c butterfly -b 19200 -P /dev/ttyUSB0 -U flash:w:$(PRG).hex + avrdude -p m16 -c dragon_isp -P usb -U flash:w:$(PRG).hex diff --git a/main.c b/main.c new file mode 100644 index 0000000..725544b --- /dev/null +++ b/main.c @@ -0,0 +1,236 @@ +/*************************************************************************** + * C based avr910 / avr109 ISP Adapter * + * * + * Copyright (C) 2006 - 20011 by Olaf Rempel * + * razzor AT kopf MINUS tisch DOT de * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; version 2 of the License, * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ +#include +#include + +#include + +/* + * using ATmega16 @7.3728MHz: + * Fuse H: 0xDA (512 words bootloader, jtag disabled) + * Fuse L: 0xFF (ext. Crystal) + */ + +#define F_CPU 7372800 +#include + +#define BAUDRATE 9600 +#define UART_CALC_BAUDRATE(baudRate) (((uint32_t)F_CPU) / (((uint32_t)baudRate)*16) -1) + +#if 0 +static int uart_putchar(char c, FILE *stream) +{ + if (c == '\n') + uart_putchar('\r', stream); + + loop_until_bit_is_set(UCSRA, UDRE); + UDR = c; + return 0; +} + +static FILE uart = FDEV_SETUP_STREAM(uart_putchar, NULL, _FDEV_SETUP_WRITE); +#endif + +static uint8_t valueR[16] = { + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, +// 0x00, 0x02, 0x04, 0x06, 0x08, 0x0A, 0x0C, 0x0E, +// 0x01, 0x03, 0x05, 0x07, 0x09, 0x0B, 0x0D, 0x0F, +}; + +static uint8_t valueG[16] = { + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, + 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, +// 0x40, 0x50, 0x60, 0x70, 0x80, 0x90, 0xA0, 0xB0, +// 0x48, 0x58, 0x68, 0x78, 0x88, 0x98, 0xA8, 0xB8, +}; + +static uint8_t valueB[16] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +// 0xF1, 0xF3, 0xF5, 0xF7, 0xF9, 0xFB, 0xFD, 0xFF, +// 0xF0, 0xF2, 0xF4, 0xF6, 0xF8, 0xFA, 0xFC, 0xFE, +}; + +static uint8_t *current_comp_reg; +static uint8_t comp_regR[16]; +static uint8_t comp_regG[16]; +static uint8_t comp_regB[16]; + +static uint16_t *current_port_reg; +static uint16_t port_regR[16 +1]; +static uint16_t port_regG[16 +1]; +static uint16_t port_regB[16 +1]; + +static uint8_t current_color; + +ISR(SIG_OVERFLOW0) +{ +#if 0 + /* switch color */ + switch (current_color) { + case 0: /* RED */ + current_comp_reg = comp_regR; + current_port_reg = port_regR; + current_color = 1; + break; + + case 1: /* GREEN */ + current_comp_reg = comp_regG; + current_port_reg = port_regG; + current_color = 2; + break; + + case 2: /* BLUE */ + current_comp_reg = comp_regB; + current_port_reg = port_regB; + current_color = 0; + break; + } +#else + current_comp_reg = comp_regR; + current_port_reg = port_regR; +#endif + + uint16_t port_reg = *current_port_reg++; + PORTA = (port_reg & 0xFF); + PORTC = (port_reg >> 8) & 0xFF; + + /* enable compare interrupts if any pin is active */ + if (port_reg != 0x0000) { + TIMSK |= (1<> 8) & 0xFF; + + /* no further compare interrupts needed */ + if (port_reg == 0x0000) { + TIMSK &= ~(1<>8) & 0xFF; + UBRRL = (UART_CALC_BAUDRATE(BAUDRATE) & 0xFF); + + /* enable usart with 8n1 */ + UCSRB = (1<