commit baa35b8392289978f3bd6d4f43689c59e0186450 Author: Eggert Jung Date: Mon Dec 27 02:46:06 2021 +0100 tannenbaum code as base diff --git a/code/Makefile b/code/Makefile new file mode 100644 index 0000000..e523244 --- /dev/null +++ b/code/Makefile @@ -0,0 +1,153 @@ +MCU = atmega328p +DUDEMCU = m328p +TARGET = main +AVRDUDE_PROGRAMMER = atmelice_isp +FCPU=1000000UL + +BUILDDIR=build + +OPT = s +SRC = $(shell find src -name '*.c') +#SRC = main.c + +FORMAT = ihex + +# List any extra directories to look for include files here. +# Each directory must be seperated by a space. +EXTRAINCDIRS = inc + +#### Flags #### +CFLAGS = -mmcu=$(MCU) -I. \ +-g -O$(OPT) \ +-funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums \ +-Wall -Wstrict-prototypes \ +-Wa,-adhlns=$(<:src/%.c=$(BUILDDIR)/%.lst) \ +-DF_CPU=$(FCPU)\ +$(patsubst %,-I%,$(EXTRAINCDIRS))\ +-std=gnu99 +#-fno-builtin + +LDFLAGS = -Wl,-Map=$(BUILDDIR)/$(TARGET).map,--cref +# Minimalistic printf version +#LDFLAGS += -Wl,-u,vfprintf -lprintf_min + +# Floating point printf version (requires -lm below) +#LDFLAGS += -Wl,-u,vfprintf -lprintf_flt + +# -lm = math library +LDFLAGS += -lm +#LDFLAGS += -nostdlib + +AVRDUDE_FLAGS = -p $(DUDEMCU) -c $(AVRDUDE_PROGRAMMER) -U flash:w:$(BUILDDIR)/$(TARGET).hex -B 10 + +#### Define programs and commands. #### +CC = avr-gcc + +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size +AVRDUDE = avrdude + + +REMOVE = rm -f +COPY = cp + +# Define all object files. +OBJ = $(SRC:src/%.c=$(BUILDDIR)/%.o) + +# Define all listing files. +LST = $(SRC:src/%.c=$(BUILDDIR)/%.lst) + +# Default target: make program! +all: $(TARGET).elf +# $(BUILDDIR)/$(TARGET).lss $(BUILDDIR)/$(TARGET).sym +# $(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) + +# Program the device. +program: $(BUILDDIR)/$(TARGET).hex $(BUILDDIR)/$(TARGET).eep + $(AVRDUDE) $(AVRDUDE_FLAGS) + + +# Create final output files (.hex, .eep) from ELF output file. +$(BUILDDIR)/%.hex: %.elf + @echo + @echo "creating ihex" + $(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@ + +$(BUILDDIR)/%.eep: %.elf + @echo + @echo "eeprom" + -$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \ + --change-section-lma .eeprom=0 -O $(FORMAT) $< $@ + +# Create extended listing file from ELF output file. +$(BUILDDIR)/%.lss: %.elf + @echo + @echo "listing" + $(OBJDUMP) -h -S $< > $@ + +# Create a symbol table from ELF output file. +$(BUILDDIR)/%.sym: %.elf + @echo + @echo "symbol table" + avr-nm -n $< > $@ + + + +# Link: create ELF output file from object files. +#.SECONDARY : $(BUILDDIR)/$(TARGET).elf +#.PRECIOUS : $(OBJ) +$(TARGET).elf: $(OBJ) + @echo + @echo "linking: " $(OBJ) + $(CC) $(CFLAGS) $(OBJ) --output $@ $(LDFLAGS) + + +# Compile: create object files from C source files. +$(BUILDDIR)/%.o : src/%.c + @echo + @echo "compiling" $< + $(CC) -c $(CFLAGS) $< -o $@ + + +# Target: clean project. +clean: + @echo + @echo "cleaning" + $(REMOVE) $(BUILDDIR)/$(TARGET).hex + $(REMOVE) $(BUILDDIR)/$(TARGET).eep + $(REMOVE) $(BUILDDIR)/$(TARGET).obj + $(REMOVE) $(BUILDDIR)/$(TARGET).cof + $(REMOVE) $(TARGET).elf + $(REMOVE) $(BUILDDIR)/$(TARGET).map + $(REMOVE) $(BUILDDIR)/$(TARGET).obj + $(REMOVE) $(BUILDDIR)/$(TARGET).a90 + $(REMOVE) $(BUILDDIR)/$(TARGET).sym + $(REMOVE) $(BUILDDIR)/$(TARGET).lnk + $(REMOVE) $(BUILDDIR)/$(TARGET).lss + $(REMOVE) $(OBJ) + $(REMOVE) $(LST) + $(REMOVE) $(SRC:src/%.c=$(BUILDDIR)/%.s) + $(REMOVE) $(SRC:src/%.c=$(BUILDDIR)/%.d) + $(REMOVE) $(BUILDDIR)/*~ + +# Automatically generate C source code dependencies. +# (Code originally taken from the GNU make user manual and modified +# (See README.txt Credits).) +# +# Note that this will work with sh (bash) and sed that is shipped with WinAVR +# (see the SHELL variable defined above). +# This may not work with other shells or other seds. +# +$(BUILDDIR)/%.d: %.c + set -e; $(CC) -MM $(CFLAGS) $< \ + | sed 's,\(.*\)\.o[ :]*,\1.o \1.d : ,g' > $@; \ + [ -s $@ ] || rm -f $@ + + +# Remove the '-' if you want to see the dependency files generated. +-include $(SRC:.c=.d) + +# Listing of phony targets. +.PHONY : all clean program + diff --git a/code/src/main.c b/code/src/main.c new file mode 100644 index 0000000..8e18d94 --- /dev/null +++ b/code/src/main.c @@ -0,0 +1,151 @@ +#include +#include // has to be added to use uint8_t +#include // Needed to use interrupts +#include + +unsigned long NextVal(void); +unsigned long InitSeed(); +void random(void); +void link_rechts(void); + +int (*animation)(void) = &link_rechts; + +volatile uint8_t leds = 0x00; +volatile uint8_t leds_active = 0x00; + +volatile uint32_t active_count = 0; +volatile uint32_t active_count_max = 13733; // 1 count = 0,065536 s + +volatile uint8_t debounce_lock = 0; +volatile uint8_t debounce_count = 0; +volatile uint8_t debounce_count_max = 10; + +volatile uint16_t count = 0; +volatile uint16_t count_max = 3; + +volatile uint8_t dir = 0; + +static unsigned long Seed; + +unsigned long InitSeed() +{ + return NextVal(); +} + +unsigned long NextVal() +{ + Seed=Seed*1632125L+1013904223L; + return Seed; +} + +void random(){ + int temp; + do{ + temp=NextVal(); + temp=1 << (temp%8); + }while(temp==leds); + leds=temp; +} + +void link_rechts(){ + if(dir) + leds = leds << 1; + else + leds = leds >> 1; + + if(!(leds & 0x7F)){ + leds = 0x08; + dir^=1; + } +} + +void write_leds(){ + if(leds_active){ + PORTC = leds; + PORTD = (leds>>1)&((1<=count_max){ + count=0; + animation(); + } + else + { + count++; + } + if(active_count >= active_count_max){ + active_count=0; + leds_active = 0; + } +} + +ISR(TIMER1_OVF_vect){ + if(debounce_count >= debounce_count_max) + { + TCCR1B &= ~(1<