tannenbaum code as base
commit
baa35b8392
@ -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
|
||||
|
||||
@ -0,0 +1,151 @@
|
||||
#include <avr/io.h>
|
||||
#include <stdint.h> // has to be added to use uint8_t
|
||||
#include <avr/interrupt.h> // Needed to use interrupts
|
||||
#include <avr/sleep.h>
|
||||
|
||||
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<<PD1)|(1<<PD2)|(1<<PD3));
|
||||
PORTD = (leds)&((1<<PD5)|(1<<PD6));
|
||||
}
|
||||
else
|
||||
{
|
||||
PORTC = 0;
|
||||
PORTD = 0;
|
||||
}
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
InitSeed();
|
||||
DDRC = 0xFF;
|
||||
DDRD = 0xFF;
|
||||
|
||||
DDRB &= ~(1 << DDB0); // Clear the PB0, PB1, PB2 pin
|
||||
// PB0,PB1,PB2 (PCINT0, PCINT1, PCINT2 pin) are now inputs
|
||||
|
||||
PORTB |= ((1 << PORTB0) | (1 << PORTB1) | (1 << PORTB2)); // turn On the Pull-up
|
||||
// PB0, PB1 and PB2 are now inputs with pull-up enabled
|
||||
|
||||
TIMSK1 |= 1 << TOIE1;
|
||||
|
||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||
TIMSK0 |= 1 << TOIE0;
|
||||
|
||||
PCICR |= (1 << PCIE0); // set PCIE0 to enable PCMSK0 scan
|
||||
PCMSK0 |= (1 << PCINT0); // set PCINT0 to trigger an interrupt on state change
|
||||
|
||||
sei(); // turn on interrupts
|
||||
|
||||
while(1)
|
||||
{
|
||||
write_leds();
|
||||
}
|
||||
}
|
||||
|
||||
ISR (PCINT0_vect)
|
||||
{
|
||||
if(!debounce_lock){
|
||||
TCCR1B |= (1<<CS00);
|
||||
debounce_lock=1;
|
||||
if((~PINB & 0x01))
|
||||
{
|
||||
leds_active ^= 0x01;
|
||||
active_count=0;
|
||||
if(!leds_active){
|
||||
write_leds();
|
||||
TCCR0B &= ~((1<<CS01)|(1<<CS00));
|
||||
set_sleep_mode(SLEEP_MODE_PWR_SAVE);
|
||||
sleep_mode();
|
||||
TCCR0B |= (1<<CS01)|(1<<CS00);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ISR(TIMER0_OVF_vect){
|
||||
active_count++;
|
||||
if(count>=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<<CS00);
|
||||
debounce_lock=0;
|
||||
debounce_count=0;
|
||||
}
|
||||
else
|
||||
{
|
||||
debounce_count++;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue