Compare commits
No commits in common. "fading" and "main" have entirely different histories.
134
main.c
134
main.c
@ -1,4 +1,3 @@
|
|||||||
#include<avr/power.h>
|
|
||||||
#include<util/delay.h>
|
#include<util/delay.h>
|
||||||
#include<avr/io.h>
|
#include<avr/io.h>
|
||||||
#include"fix_fft.h"
|
#include"fix_fft.h"
|
||||||
@ -30,55 +29,26 @@
|
|||||||
//7- 880
|
//7- 880
|
||||||
//8- 1kHz
|
//8- 1kHz
|
||||||
//
|
//
|
||||||
|
const float log_scale = 64./log(64./SCALE_FACTOR + 1.);
|
||||||
|
|
||||||
int sec;
|
int msec;
|
||||||
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 main (void)
|
||||||
{
|
{
|
||||||
int i,i2;
|
int i,i2;
|
||||||
int i4=0;
|
int colorb;
|
||||||
|
int colora;
|
||||||
int sum,avg;
|
int sum,avg;
|
||||||
char sample[64];
|
char sample[64];
|
||||||
|
char im[64];
|
||||||
char buff[64];
|
char buff[64];
|
||||||
|
char temp;
|
||||||
float fac_r=0.1;
|
msec=0;
|
||||||
float fac_g=0.5;
|
|
||||||
float fac_b=0.7;
|
|
||||||
sec=0;
|
|
||||||
ticks=0;
|
|
||||||
ticks_sample=0;
|
|
||||||
struct cRGB led_bar1[8];
|
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
|
//DDRB = (1<<mic);//would be setting output, default is input so no need for setting
|
||||||
setupADC();
|
setupADC();
|
||||||
setupTimer();
|
setupTimer();
|
||||||
|
|
||||||
//OSCCAL = 250;//overclock!!! to 30MHz
|
OSCCAL = 240;//overclock!!! to 30MHz
|
||||||
for(i=0;i<8;i++)
|
for(i=0;i<8;i++)
|
||||||
{
|
{
|
||||||
led_bar1[i].r=0;
|
led_bar1[i].r=0;
|
||||||
@ -93,87 +63,82 @@ int main (void)
|
|||||||
sei();
|
sei();
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
|
//######RECORD SAMPLES######
|
||||||
//######RECORD SAMPLES######
|
|
||||||
sum = 0;
|
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++)
|
for(i=0;i<64;i++)
|
||||||
{
|
{
|
||||||
|
msec=0;
|
||||||
STARTADC;
|
STARTADC;
|
||||||
sample[i] = (ADCH-128); //read Analog value register 8bit center -128
|
sample[i] = (ADCH-128); //read Analog value register 8bit center -128
|
||||||
//sample[i] = (sample[i] <= NOISE) ? 0 : (sample[i] - NOISE); //noise reduction
|
//sample[i] = (sample[i] <= NOISE) ? 0 : (sample[i] - NOISE); //noise reduction
|
||||||
|
im[i]=0; //pseudo data for funciton
|
||||||
|
//
|
||||||
sum+=sample[i];//dc-bias
|
sum+=sample[i];//dc-bias
|
||||||
}
|
|
||||||
|
|
||||||
|
//while(msec==0);
|
||||||
|
}
|
||||||
avg = sum/64;//calculate bias
|
avg = sum/64;//calculate bias
|
||||||
for(i=0;i<64;i++)
|
for(i=0;i<64;i++)
|
||||||
{
|
{
|
||||||
sample[i]-=avg;//remove bias
|
sample[i]-=avg;//remove bias
|
||||||
}
|
}
|
||||||
fix_fftr(sample,6,0);
|
fix_fftr(sample,6,0);//im[i] is always zero
|
||||||
|
//####Averaging Data####
|
||||||
//####Averaging Data####
|
//
|
||||||
for(i=0;i<64;i++)
|
for(i=0;i<64;i++)
|
||||||
{
|
{
|
||||||
if(sample[i]<0) sample[i] = 0;
|
if(sample[i]<0) sample[i] = 0;
|
||||||
if(sample[i]>64) sample[i]=64; //define upper limit
|
if(sample[i]>64) sample[i]=64;
|
||||||
sample[i] = sample[i] *SCALE_FACTOR;
|
sample[i] = sample[i] *SCALE_FACTOR;
|
||||||
sample[i] = (sample[i]*0.7 + buff[i]*0.3); //influence of last sample on current sample
|
sample[i] = (sample[i]*0.7 + buff[i]*0.3);
|
||||||
buff[i] = sample[i]; //store sample for next round
|
buff[i] = sample[i];
|
||||||
|
|
||||||
}
|
}
|
||||||
for(i=0;i<8;i++) //we store 8 frequency domains acros the whole 64 stpectrum
|
for(i=0;i<8;i++)
|
||||||
{
|
{
|
||||||
for(i2=1;i2<=4;i2++)
|
for(i2=1;i2<=4;i2++)
|
||||||
{
|
{
|
||||||
sample[i] = ((sample[i] + sample[i*8+i2])/2);//average each 4 following spectrums, write results to sample[0..7]
|
sample[i] = ((sample[i] + sample[i*8+i2])/2);
|
||||||
}
|
}
|
||||||
|
//avg written to sample[0..7]
|
||||||
|
|
||||||
if(sample[i]<20)
|
if(sample[i]<20)
|
||||||
{
|
{
|
||||||
led_bar1[i].r=(char)sample[i]*fac_r;
|
led_bar1[i].r=0;
|
||||||
led_bar1[i].g=(char)sample[i]*fac_g;
|
led_bar1[i].g=0;
|
||||||
led_bar1[i].b=(char)sample[i]*fac_b;
|
led_bar1[i].b=sample[i];
|
||||||
}
|
}
|
||||||
else if(sample[i]<30)
|
else if(sample[i]<40)
|
||||||
{
|
{
|
||||||
led_bar1[i].r=(char)sample[i]*fac_r*2;
|
led_bar1[i].r=0;
|
||||||
led_bar1[i].g=(char)sample[i]*fac_g/2;
|
led_bar1[i].g=sample[i];
|
||||||
led_bar1[i].b=(char)sample[i]*fac_b/2;
|
led_bar1[i].b=sample[i]/2;
|
||||||
|
|
||||||
}
|
}
|
||||||
else if(sample[i]<50)
|
else if(sample[i]<60)
|
||||||
{
|
{
|
||||||
led_bar1[i].r=(char)sample[i]*fac_r*2;
|
led_bar1[i].r=sample[i];
|
||||||
led_bar1[i].g=(char)sample[i]*fac_g*2;
|
led_bar1[i].g=0;
|
||||||
led_bar1[i].b=(char)sample[i]*fac_b*2;
|
led_bar1[i].b=sample[i]/2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ws2812_setleds(led_bar1,8);
|
ws2812_setleds(led_bar1,8);
|
||||||
while(ticks_sample<60){asm("");}//workaround for stoping gcc optimizing
|
|
||||||
}
|
}
|
||||||
return 0;
|
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)
|
void setupTimer(void)
|
||||||
{
|
{
|
||||||
clock_prescale_set(0);
|
TCCR1 = (1<<CS13)|(1<<CS10)|(1<<CTC1); //clear timer on compare match
|
||||||
TCCR1 = (1<<CS10);//|(1<<CS10)|(1<<CTC1); /8 //clear timer on compare match
|
|
||||||
GTCCR = 0; //default values should be ok
|
GTCCR = 0; //default values should be ok
|
||||||
//OCR1C = 117; //Compare value of timer / overflow , sets OCF1A
|
OCR1C = 117; //Compare value of timer / overflow , sets OCF1A
|
||||||
//OCR1A = 117;
|
OCR1A = 117;
|
||||||
TIMSK |= (1<<TOIE1);//|(1<<TOIE1); //timer0 ovf
|
TIMSK = (1<<OCIE1A);//|(1<<TOIE1); //enable interupt compare a
|
||||||
TIFR |= (1<<TOV1); //flag register "aka when to interrupt
|
TIFR = (1<<OCF1A); //flag register "aka when to interrupt
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* 1/(30Mhz/16384) *255 = 0.139264s Time for one overflow
|
* 1/(30Mhz/16384) *255 = 0.139264s Time for one overflow
|
||||||
@ -182,3 +147,14 @@ void setupTimer(void)
|
|||||||
* 255/0.017204*0.001=15 counts = 1ms
|
* 255/0.017204*0.001=15 counts = 1ms
|
||||||
* 256 gives 1ms
|
* 256 gives 1ms
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
void wait_period(void)
|
||||||
|
{
|
||||||
|
// TIFR |=(1<<TOV1); //clear overflow flag
|
||||||
|
}
|
||||||
|
|
||||||
|
ISR(TIMER1_COMPA_vect)
|
||||||
|
{
|
||||||
|
msec++;
|
||||||
|
}
|
||||||
|
|
||||||
|
8
makefile
8
makefile
@ -10,7 +10,7 @@ FORMAT = ihex
|
|||||||
TARGET = main
|
TARGET = main
|
||||||
SRC = $(TARGET).c avr_adc.c fix_fft.c light_ws2812_AVR/Light_WS2812/light_ws2812.c
|
SRC = $(TARGET).c avr_adc.c fix_fft.c light_ws2812_AVR/Light_WS2812/light_ws2812.c
|
||||||
ASRC =
|
ASRC =
|
||||||
OPT = 1
|
OPT = s
|
||||||
|
|
||||||
# Name of this Makefile (used for "make depend").
|
# Name of this Makefile (used for "make depend").
|
||||||
MAKEFILE = Makefile
|
MAKEFILE = Makefile
|
||||||
@ -18,7 +18,7 @@ MAKEFILE = Makefile
|
|||||||
# Debugging format.
|
# Debugging format.
|
||||||
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
|
# Native formats for AVR-GCC's -g are stabs [default], or dwarf-2.
|
||||||
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
|
# AVR (extended) COFF requires stabs, plus an avr-objcopy run.
|
||||||
#DEBUG = stabs
|
DEBUG = stabs
|
||||||
|
|
||||||
# Compiler flag to set the C Standard level.
|
# Compiler flag to set the C Standard level.
|
||||||
# c89 - "ANSI" C
|
# c89 - "ANSI" C
|
||||||
@ -28,7 +28,7 @@ MAKEFILE = Makefile
|
|||||||
CSTANDARD = -std=gnu99
|
CSTANDARD = -std=gnu99
|
||||||
|
|
||||||
# Place -D or -U options here
|
# Place -D or -U options here
|
||||||
CDEFS = -D F_CPU=16000000UL
|
CDEFS = -DF_CPU=30000000UL
|
||||||
|
|
||||||
# Place -I options here
|
# Place -I options here
|
||||||
CINCS =
|
CINCS =
|
||||||
@ -38,7 +38,7 @@ CDEBUG = -g$(DEBUG)
|
|||||||
CWARN = -Wall -Wstrict-prototypes
|
CWARN = -Wall -Wstrict-prototypes
|
||||||
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
|
CTUNING = -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums
|
||||||
#CEXTRA = -Wa,-adhlns=$(<:.c=.lst)
|
#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
|
#ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
||||||
|
Loading…
Reference in New Issue
Block a user