Compare commits

..

7 Commits

3 changed files with 340 additions and 97 deletions

210
main.c
View File

@ -1,9 +1,13 @@
//#pragma GCC push_options
//#pragma GCC optimize ("O0")
#define F_CPU 8000000UL
#include <util/delay.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#define PATTERN_COUNT 4
#define attiny45
typedef struct
{
@ -12,18 +16,20 @@ typedef struct
unsigned char b;
int time_hold;
}rgbval;
volatile uint16_t ms;
volatile int counts;
ISR(TIMER0_OVF_vect)
{
count();
}
volatile uint16_t ms;
volatile uint16_t counts;
void InitTimer0(void);
void InitTimer1(void);
void setrgb(unsigned int r,unsigned int g, unsigned int b);
void InitInterrupts(void);
void count(void)
{
//counts=0;//0..65535
counts++;
if(counts>=31)
if(counts>3) //1m //1ms
{
counts=0;
if(ms<60000)//1 minute
@ -36,103 +42,117 @@ void count(void)
}
}
}
ISR(TIMER0_OVF_vect)
{
count();
}
int main (void)
{
rgbval pattern[6][PATTERN_COUNT]={ {{0,0,0,10}, {0,0,0,10}, {0,0,0,10}, {0,0,0,10}},
{
rgbval pattern[6][PATTERN_COUNT]={ {{0,0,0,100}, {0,0,0,100}, {0,0,0,100}, {0,0,0,100}},
{{1,1,1,10}, {1,1,153,20}, {200,50,39,10}, {204,255,153,20}},
{{1,1,1,10}, {10,255,0,10}, {1,1,255,20}, {1,200,1,20}},
{{1,50,50,10}, {10,255,0,10}, {80,200,0,20}, {200,200,1,20}},
{{1,1,5,10}, {1,1,255,10}, {1,1,5,10}, {1,1,255,10}},
{{252,102,3,10}, {161,67,47,10}, {118,120,74,10}, {83,120,74,20}},
{{87,119,122,10}, {95,87,122,10}, {52, 69, 56,10}, {83, 84, 72,10}}};
int i=0,i2=0,i3;
int r,g,b = 0;
int set=0;
int input=0;
uint16_t pre_delay=0;
ms =0;
counts = 0;
{{87,119,122,10}, {95,87,122,10}, {52, 69, 56,10}, {83, 84, 72,10}}};
int i=0,i2=0,i3=0;
int r,g,b = 0;
int set=0;
int input=0;
uint16_t pre_delay=0;
ms =0;
counts = 0;
//disable prescaler
CLKPR = (1<<CLKPR);
CLKPR = 0x00;
// CLKPR = (1<<CLKPR); maybe optimization removes this
// CLKPR = 0x00;
clock_prescale_set(clock_div_1);//replaces above 2 lines
//#######OC0A auf ausgang
DDRB = (1 << PB0 )|(1<<PB1)|(1<<PB4);
cli();
//#######Timer0-setup##### |Phase correct pwm
TCCR0A = (1<<COM0A1)|(1<<COM0B1)|(1<<WGM00)|(1<<WGM00); //mode
TCCR0B = (1<<CS00)|(1<<CS00); //frequency /256 = 31250Hz , 1 t0 period = 8.192ms 1 increment = 0.032ms strangly prescaler of 1 results in expected results as prescaler was 256
// PLLCSR |= (1 << PLLE) | (1 << PCKE);
//######Timer1-setup
TCCR1 = (1<<COM1A0)|(1<<COM1A1)|(1<<CTC1)|(1<<CS12)|(1<<CS10);//|(1<<CS11)|(1<<CS13); //frequencyi //iinverting PWM because 0 dtc not possible
GTCCR = (1<<PWM1B)|(1<<COM1B0); //mode + overrun clear, Set on compare match
OCR1C = 255; // clear timer at 255
//######PWM_VALUE$
//timer 0
OCR0A = 1;
OCR0B = 1;
//timer 1
OCR1B = 1;
//######Configure Pullups
PORTB |= (1<<PB2);
//Interupts
TIMSK |= (1<<TOIE0);//interrupt on overflow = one timer cycle
TIFR |= (1<<TOV0);
TCNT0 = 0;
//enable global interrupts
sei();
while(1)
{
for(i=1;i<PATTERN_COUNT;i++)
{
r = pattern[i2][i].r;
g = pattern[i2][i].g;
b = pattern[i2][i].b;
if(i==(PATTERN_COUNT-1)) {i= -1;}
//reset timer counter
while(1)
{
//input handling
input=PINB;
if(((input & (1<<PB2)) == 0 && set==0))
DDRB = (1 << PB0 )|(1<<PB1)|(1<<PB4);//data directions - output
PORTB |= (1<<PB2); //pullups
cli(); //DISABLE Global Interrupts
InitTimer0();
InitTimer1();
InitInterrupts();
sei(); //ENABLE Global Interrupts
while(1)
{
for(i=0;i<PATTERN_COUNT;i++)
{
set=1;
i=0;
i2++;
if(i2==6)
r = pattern[i2][i].r;
g = pattern[i2][i].g;
b = pattern[i2][i].b;
if(i==(PATTERN_COUNT-1)) {i= 0;}
while(1)
{
input=PINB;
if(((input & (1<<PB2)) == 0 && set==0))
{
i2=0;
set=1;
i=0;
i2++;
if(i2==6)
{
i2=0;
}
//Visual feedback "mode change"
for(i3=0;i3<i2;i3++)
{
setrgb(0,0,0);
pre_delay = ms;
while((ms-pre_delay) < 200);
setrgb(0,200,0);
pre_delay = ms;
while((ms-pre_delay) < 200){};
}
}
//cycle through and stop pwm at value 0
//Visual feedback "mode change"
for(i3=0;i3<i2;i3++)
{
OCR0A=1;OCR0B=1;OCR1B=1;
_delay_ms(30);
OCR0A=255;OCR0B=255;OCR1B=255;
_delay_ms(30);
else if(((input & (1<<PB2)) != 0))
{
set=0;
}
//iterate throught pattern to the next ones
if(r < pattern[i2][i+1].r) {r++;}
if(r > pattern[i2][i+1].r) {r--;}
if(g < pattern[i2][i+1].g) {g++;}
if(g > pattern[i2][i+1].g) {g--;}
if(b < pattern[i2][i+1].b) {b++;}
if(b > pattern[i2][i+1].b) {b--;}
setrgb(r,g,b);
//read timer evaluate time since read in 1ms steps
//pre_delay = ms;
//while((ms-pre_delay) <1000);
_delay_ms(30);
if(r == pattern[i2][i+1].r && g == pattern[i2][i+1].g && b == pattern[i2][i+1].b) break;
}
}
else if(((input & (1<<PB2)) != 0))
{
set=0;
}
//Pattern handling
if(r < pattern[i2][i+1].r) {r++;}
if(r > pattern[i2][i+1].r) {r--;}
if(g < pattern[i2][i+1].g) {g++;}
if(g > pattern[i2][i+1].g) {g--;}
if(b < pattern[i2][i+1].b) {b++;}
if(b > pattern[i2][i+1].b) {b--;}
OCR0A = g;//green
OCR0B = r;//red
OCR1B = b;//blue
// _delay_ms(10);
pre_delay = ms;
while((ms-pre_delay) < pattern[i2][i+1].time_hold){};
if(r == pattern[i2][i+1].r && g == pattern[i2][i+1].g && b == pattern[i2][i+1].b) break;
}
}
}
return 0;
}
return 0;
}
void InitTimer0(void)
{
//#######Timer0-setup##### |Phase correct pwm
TCCR0A = (2<<COM0A0)|(2<<COM0B0)|(1<<WGM00)|(1<<WGM01); //mode fast pwm (1/Fcpu)*prescaler*(max+1) =
//prescaler /64 gives 0...255 0.002048s = 2.0ms 1mhz-> /8
TCCR0B = (1<<CS01);//|(1<<CS01)/256;
}
void InitTimer1(void)
{
//######Timer1-setup
PLLCSR &= ~(1<<PCKE);
TCCR1 = (1<<COM1A0)|(1<<COM1A1)|(1<<CS12)|(1<<CS10)|(1<<CS11);//(1<<CS10)|(1<<CS12);//|(1<<CS13); //frequencyi //inverting PWM because 0 dtc not possible
GTCCR = (1<<PWM1B)|(1<<COM1B1); //mode + overrun clear, Set on compare match
OCR1C = 255; // clear timer at 255
}
void setrgb(unsigned int r,unsigned int g,unsigned int b)
{
//depends on timer0
OCR0A = g;//green
OCR0B = r;//red
//depends on Timer1
OCR1B = b;//blue
}
void InitInterrupts(void)
{
TIMSK |= (1<<TOIE0);//interrupt on overflow = one timer cycle
TIFR |= (1<<TOV0);
TCNT0 = 0;
}
//#pragma GCC pop_options

View File

@ -5,12 +5,12 @@
# (GNU make, BSD make, SysV make)
MCU = attiny85
MCU = attiny45
FORMAT = ihex
TARGET = main
SRC = $(TARGET).c
ASRC =
OPT = s
OPT = 0
# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile

223
makefile_45 Normal file
View File

@ -0,0 +1,223 @@
# Hey Emacs, this is a -*- makefile -*-
# AVR-GCC Makefile template, derived from the WinAVR template (which
# is public domain), believed to be neutral to any flavor of "make"
# (GNU make, BSD make, SysV make)
MCU = attiny45
FORMAT = ihex
TARGET = main
SRC = $(TARGET).c
ASRC =
OPT = s
# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile
# Debugging format.
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
DEBUG = stabs
# Compiler flag to set the C Standard level.
# c89 - "ANSI" C
# gnu89 - c89 plus GCC extensions
# c99 - ISO C99 standard (not yet fully implemented)
# gnu99 - c99 plus GCC extensions
CSTANDARD = -std=gnu99
# Place -D or -U options here
CDEFS =
# Place -I options here
CINCS =
CDEBUG = -g$(DEBUG)
CWARN = -Wall -Wstrict-prototypes
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD) $(CEXTRA)
#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
#Additional libraries.
# Minimalistic printf version
PRINTF_LIB_MIN = -Wl,-u,vfprintf -lprintf_min
# Floating point printf version (requires MATH_LIB = -lm below)
PRINTF_LIB_FLOAT = -Wl,-u,vfprintf -lprintf_flt
PRINTF_LIB =
# Minimalistic scanf version
SCANF_LIB_MIN = -Wl,-u,vfscanf -lscanf_min
# Floating point + %[ scanf version (requires MATH_LIB = -lm below)
SCANF_LIB_FLOAT = -Wl,-u,vfscanf -lscanf_flt
SCANF_LIB =
MATH_LIB = -lm
# External memory options
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# used for variables (.data/.bss) and heap (malloc()).
#EXTMEMOPTS = -Wl,--section-start,.data=0x801100,--defsym=__heap_end=0x80ffff
# 64 KB of external RAM, starting after internal RAM (ATmega128!),
# only used for heap (malloc()).
#EXTMEMOPTS = -Wl,--defsym=__heap_start=0x801100,--defsym=__heap_end=0x80ffff
EXTMEMOPTS =
#LDMAP = $(LDFLAGS) -Wl,-Map=$(TARGET).map,--cref
LDFLAGS = $(EXTMEMOPTS) $(LDMAP) $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
# Programming support using avrdude. Settings and variables.
AVRDUDE_PROGRAMMER = usbasp
#AVRDUDE_PORT = /dev/term/a
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
# Uncomment the following if you want avrdude's erase cycle counter.
# Note that this counter needs to be initialized first using -Yn,
# see avrdude manual.
#AVRDUDE_ERASE_COUNTER = -y
# Uncomment the following if you do /not/ wish a verification to be
# performed after programming the device.
#AVRDUDE_NO_VERIFY = -V
# Increase verbosity level. Please use this when submitting bug
# reports about avrdude. See <http://savannah.nongnu.org/projects/avrdude>
# to submit bug reports.
#AVRDUDE_VERBOSE = -v -v
AVRDUDE_BASIC = -p $(MCU) -c $(AVRDUDE_PROGRAMMER) -B4
AVRDUDE_FLAGS = $(AVRDUDE_BASIC) $(AVRDUDE_NO_VERIFY) $(AVRDUDE_VERBOSE) $(AVRDUDE_ERASE_COUNTER)
CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
SIZE = avr-size
NM = avr-nm
AVRDUDE = avrdude
REMOVE = rm -f
MV = mv -f
# Define all object files.
OBJ = $(SRC:.c=.o) $(ASRC:.S=.o)
# Define all listing files.
LST = $(ASRC:.S=.lst) $(SRC:.c=.lst)
# Combine all necessary flags and optional flags.
# Add target processor to flags.
ALL_CFLAGS = -mmcu=$(MCU) -I. $(CFLAGS)
ALL_ASFLAGS = -mmcu=$(MCU) -I. -x assembler-with-cpp $(ASFLAGS)
# Default target.
all: build
build: elf hex eep
elf: $(TARGET).elf
hex: $(TARGET).hex
eep: $(TARGET).eep
lss: $(TARGET).lss
sym: $(TARGET).sym
# Program the device.
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
# Convert ELF to COFF for use in debugging / simulating in AVR Studio or VMLAB.
COFFCONVERT=$(OBJCOPY) --debugging \
--change-section-address .data-0x800000 \
--change-section-address .bss-0x800000 \
--change-section-address .noinit-0x800000 \
--change-section-address .eeprom-0x810000
coff: $(TARGET).elf
$(COFFCONVERT) -O coff-avr $(TARGET).elf $(TARGET).cof
extcoff: $(TARGET).elf
$(COFFCONVERT) -O coff-ext-avr $(TARGET).elf $(TARGET).cof
.SUFFIXES: .elf .hex .eep .lss .sym
.elf.hex:
$(OBJCOPY) -O $(FORMAT) -R .eeprom $< $@
.elf.eep:
-$(OBJCOPY) -j .eeprom --set-section-flags=.eeprom="alloc,load" \
--change-section-lma .eeprom=0 -O $(FORMAT) $< $@
# Create extended listing file from ELF output file.
.elf.lss:
$(OBJDUMP) -h -S $< > $@
# Create a symbol table from ELF output file.
.elf.sym:
$(NM) -n $< > $@
# Link: create ELF output file from object files.
$(TARGET).elf: $(OBJ)
$(CC) $(ALL_CFLAGS) $(OBJ) --output $@ $(LDFLAGS)
# Compile: create object files from C source files.
.c.o:
$(CC) -c $(ALL_CFLAGS) $< -o $@
# Compile: create assembler files from C source files.
.c.s:
$(CC) -S $(ALL_CFLAGS) $< -o $@
# Assemble: create object files from assembler source files.
.S.o:
$(CC) -c $(ALL_ASFLAGS) $< -o $@
# Target: clean project.
clean:
$(REMOVE) $(TARGET).hex $(TARGET).eep $(TARGET).cof $(TARGET).elf \
$(TARGET).map $(TARGET).sym $(TARGET).lss \
$(OBJ) $(LST) $(SRC:.c=.s) $(SRC:.c=.d)
depend:
if grep '^# DO NOT DELETE' $(MAKEFILE) >/dev/null; \
then \
sed -e '/^# DO NOT DELETE/,$$d' $(MAKEFILE) > \
$(MAKEFILE).$$$$ && \
$(MV) $(MAKEFILE).$$$$ $(MAKEFILE); \
fi
echo '# DO NOT DELETE THIS LINE -- make depend depends on it.' \
>> $(MAKEFILE); \
$(CC) -M -mmcu=$(MCU) $(CDEFS) $(CINCS) $(SRC) $(ASRC) >> $(MAKEFILE)
.PHONY: all build elf hex eep lss sym program coff extcoff clean depend