Compare commits

...

5 Commits
main ... fading

2 changed files with 84 additions and 60 deletions

134
main.c
View File

@ -1,3 +1,4 @@
#include<avr/power.h>
#include<util/delay.h>
#include<avr/io.h>
#include"fix_fft.h"
@ -29,26 +30,55 @@
//7- 880
//8- 1kHz
//
const float log_scale = 64./log(64./SCALE_FACTOR + 1.);
int msec;
int sec;
uint16_t ticks;
int ticks_sample;
ISR(TIMER1_OVF_vect)
{
ticks_sample++;
if(ticks < 65000)
{
ticks++;
}
if(ticks >= 65000)
{
ticks=0;
if(sec<255)
{
sec++;
}
else
{
sec=0;
}
}
}
int main (void)
{
int i,i2;
int colorb;
int colora;
int i4=0;
int sum,avg;
char sample[64];
char im[64];
char buff[64];
char temp;
msec=0;
float fac_r=0.1;
float fac_g=0.5;
float fac_b=0.7;
sec=0;
ticks=0;
ticks_sample=0;
struct cRGB led_bar1[8];
Fader fader1[4]={{5,5,5},{7,3,2},{3,7,2},{2,3,7}};
//DDRB = (1<<mic);//would be setting output, default is input so no need for setting
setupADC();
setupTimer();
OSCCAL = 240;//overclock!!! to 30MHz
//OSCCAL = 250;//overclock!!! to 30MHz
for(i=0;i<8;i++)
{
led_bar1[i].r=0;
@ -63,82 +93,87 @@ int main (void)
sei();
while(1)
{
//######RECORD SAMPLES######
//######RECORD SAMPLES######
sum = 0;
ticks_sample=0;
if(sec>3)
{
fac_r=(float)fader1[i4].r/10;
fac_g=(float)fader1[i4].g/10;
fac_b=(float)fader1[i4].b/10;
if(i4<3) {i4++;}
else {i4=0;}
sec=0;
}
for(i=0;i<64;i++)
{
msec=0;
STARTADC;
sample[i] = (ADCH-128); //read Analog value register 8bit center -128
//sample[i] = (sample[i] <= NOISE) ? 0 : (sample[i] - NOISE); //noise reduction
im[i]=0; //pseudo data for funciton
//
sum+=sample[i];//dc-bias
//while(msec==0);
}
avg = sum/64;//calculate bias
for(i=0;i<64;i++)
{
sample[i]-=avg;//remove bias
}
fix_fftr(sample,6,0);//im[i] is always zero
//####Averaging Data####
//
fix_fftr(sample,6,0);
//####Averaging Data####
for(i=0;i<64;i++)
{
if(sample[i]<0) sample[i] = 0;
if(sample[i]>64) sample[i]=64;
if(sample[i]>64) sample[i]=64; //define upper limit
sample[i] = sample[i] *SCALE_FACTOR;
sample[i] = (sample[i]*0.7 + buff[i]*0.3);
buff[i] = sample[i];
sample[i] = (sample[i]*0.7 + buff[i]*0.3); //influence of last sample on current sample
buff[i] = sample[i]; //store sample for next round
}
for(i=0;i<8;i++)
for(i=0;i<8;i++) //we store 8 frequency domains acros the whole 64 stpectrum
{
for(i2=1;i2<=4;i2++)
{
sample[i] = ((sample[i] + sample[i*8+i2])/2);
sample[i] = ((sample[i] + sample[i*8+i2])/2);//average each 4 following spectrums, write results to sample[0..7]
}
//avg written to sample[0..7]
if(sample[i]<20)
{
led_bar1[i].r=0;
led_bar1[i].g=0;
led_bar1[i].b=sample[i];
led_bar1[i].r=(char)sample[i]*fac_r;
led_bar1[i].g=(char)sample[i]*fac_g;
led_bar1[i].b=(char)sample[i]*fac_b;
}
else if(sample[i]<40)
else if(sample[i]<30)
{
led_bar1[i].r=0;
led_bar1[i].g=sample[i];
led_bar1[i].b=sample[i]/2;
led_bar1[i].r=(char)sample[i]*fac_r*2;
led_bar1[i].g=(char)sample[i]*fac_g/2;
led_bar1[i].b=(char)sample[i]*fac_b/2;
}
else if(sample[i]<60)
else if(sample[i]<50)
{
led_bar1[i].r=sample[i];
led_bar1[i].g=0;
led_bar1[i].b=sample[i]/2;
led_bar1[i].r=(char)sample[i]*fac_r*2;
led_bar1[i].g=(char)sample[i]*fac_g*2;
led_bar1[i].b=(char)sample[i]*fac_b*2;
}
}
ws2812_setleds(led_bar1,8);
while(ticks_sample<60){asm("");}//workaround for stoping gcc optimizing
}
return 0;
}
int map(int value, int old_min, int old_max,int new_min, int new_max)
{
return (new_max/old_max*value);
}
void setupTimer(void)
{
TCCR1 = (1<<CS13)|(1<<CS10)|(1<<CTC1); //clear timer on compare match
clock_prescale_set(0);
TCCR1 = (1<<CS10);//|(1<<CS10)|(1<<CTC1); /8 //clear timer on compare match
GTCCR = 0; //default values should be ok
OCR1C = 117; //Compare value of timer / overflow , sets OCF1A
OCR1A = 117;
TIMSK = (1<<OCIE1A);//|(1<<TOIE1); //enable interupt compare a
TIFR = (1<<OCF1A); //flag register "aka when to interrupt
//OCR1C = 117; //Compare value of timer / overflow , sets OCF1A
//OCR1A = 117;
TIMSK |= (1<<TOIE1);//|(1<<TOIE1); //timer0 ovf
TIFR |= (1<<TOV1); //flag register "aka when to interrupt
}
/*
* 1/(30Mhz/16384) *255 = 0.139264s Time for one overflow
@ -147,14 +182,3 @@ void setupTimer(void)
* 255/0.017204*0.001=15 counts = 1ms
* 256 gives 1ms
*/
void wait_period(void)
{
// TIFR |=(1<<TOV1); //clear overflow flag
}
ISR(TIMER1_COMPA_vect)
{
msec++;
}

View File

@ -10,7 +10,7 @@ FORMAT = ihex
TARGET = main
SRC = $(TARGET).c avr_adc.c fix_fft.c light_ws2812_AVR/Light_WS2812/light_ws2812.c
ASRC =
OPT = s
OPT = 1
# Name of this Makefile (used for "make depend").
MAKEFILE = Makefile
@ -18,7 +18,7 @@ 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
#DEBUG = stabs
# Compiler flag to set the C Standard level.
# c89 - "ANSI" C
@ -28,7 +28,7 @@ DEBUG = stabs
CSTANDARD = -std=gnu99
# Place -D or -U options here
CDEFS = -DF_CPU=30000000UL
CDEFS = -D F_CPU=16000000UL
# Place -I options here
CINCS =
@ -38,7 +38,7 @@ 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)
CFLAGS = $(CDEBUG) $(CDEFS) $(CINCS) -O$(OPT) $(CWARN) $(CSTANDARD)# $(CEXTRA)
#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs