2024-03-22 22:53:40 +00:00
|
|
|
#include<avr/power.h>
|
2024-02-27 12:12:18 +00:00
|
|
|
#include<util/delay.h>
|
|
|
|
#include<avr/io.h>
|
|
|
|
#include"fix_fft.h"
|
|
|
|
#include"avr_adc.h"
|
|
|
|
#include"light_ws2812_AVR/Light_WS2812/ws2812_config.h"
|
|
|
|
#include"light_ws2812_AVR/Light_WS2812/light_ws2812.h"
|
2024-02-29 20:32:26 +00:00
|
|
|
#include"main.h"
|
2024-02-27 12:12:18 +00:00
|
|
|
|
2024-02-28 21:52:37 +00:00
|
|
|
#define STARTADC ADCSRA |= (1<<ADSC)
|
2024-02-27 12:12:18 +00:00
|
|
|
#define PB_mic PB0
|
2024-02-29 20:32:26 +00:00
|
|
|
#define NOISE 100
|
2024-03-01 19:34:42 +00:00
|
|
|
#define SCALE_FACTOR 10
|
2024-02-28 21:52:37 +00:00
|
|
|
//resolution fft calc:
|
|
|
|
//https://3roam.com/fft-resolution-bandwidth-calculator/
|
|
|
|
//128 samples , 1kSPS = 7.8Hz
|
|
|
|
//interested in 0-10000 Hz
|
|
|
|
//
|
|
|
|
//128 bins a 7.8hz = 0...1000Hz
|
|
|
|
//
|
|
|
|
//we have 8 leds:
|
|
|
|
//bands 125hz/band -> 16 bins
|
|
|
|
//
|
|
|
|
//1-7.8 - ca 130hz
|
|
|
|
//2- 255hz
|
|
|
|
//3- 380
|
|
|
|
//4- 505
|
|
|
|
//5- 630
|
|
|
|
//6- 755
|
|
|
|
//7- 880
|
|
|
|
//8- 1kHz
|
2024-02-29 20:32:26 +00:00
|
|
|
//
|
2024-03-22 22:53:40 +00:00
|
|
|
|
|
|
|
const float log_scale = 64./log(64./SCALE_FACTOR + 1.);
|
|
|
|
|
2024-02-29 20:32:26 +00:00
|
|
|
|
2024-03-01 19:34:42 +00:00
|
|
|
int msec;
|
2024-03-22 22:53:40 +00:00
|
|
|
|
|
|
|
ISR(TIMER1_OVF_vect)
|
|
|
|
{
|
|
|
|
if(msec<100)
|
|
|
|
{
|
|
|
|
msec++;
|
|
|
|
}
|
|
|
|
else msec=0;
|
|
|
|
}
|
|
|
|
|
2024-02-27 12:12:18 +00:00
|
|
|
int main (void)
|
|
|
|
{
|
2024-02-28 21:52:37 +00:00
|
|
|
int i,i2;
|
2024-02-27 20:59:27 +00:00
|
|
|
int colorb;
|
2024-02-28 21:52:37 +00:00
|
|
|
int colora;
|
2024-02-29 20:32:26 +00:00
|
|
|
int sum,avg;
|
|
|
|
char sample[64];
|
|
|
|
char im[64];
|
|
|
|
char buff[64];
|
|
|
|
char temp;
|
2024-03-22 22:53:40 +00:00
|
|
|
|
|
|
|
float fac_r=0.5;
|
|
|
|
float fac_g=0.5;
|
|
|
|
float fac_b=1;
|
2024-03-01 19:34:42 +00:00
|
|
|
msec=0;
|
2024-02-27 20:29:54 +00:00
|
|
|
struct cRGB led_bar1[8];
|
|
|
|
//DDRB = (1<<mic);//would be setting output, default is input so no need for setting
|
2024-02-27 20:59:27 +00:00
|
|
|
setupADC();
|
2024-03-01 19:34:42 +00:00
|
|
|
setupTimer();
|
|
|
|
|
2024-03-22 22:53:40 +00:00
|
|
|
OSCCAL = 250;//overclock!!! to 30MHz
|
2024-02-27 20:29:54 +00:00
|
|
|
for(i=0;i<8;i++)
|
|
|
|
{
|
2024-02-27 20:59:27 +00:00
|
|
|
led_bar1[i].r=0;
|
|
|
|
led_bar1[i].g=0;
|
|
|
|
led_bar1[i].b=0;
|
2024-02-27 20:29:54 +00:00
|
|
|
}
|
2024-02-29 20:32:26 +00:00
|
|
|
for(i=0;i<64;i++)
|
|
|
|
{
|
|
|
|
buff[i]=0;
|
|
|
|
}
|
2024-03-01 19:34:42 +00:00
|
|
|
|
|
|
|
sei();
|
2024-02-27 12:12:18 +00:00
|
|
|
while(1)
|
|
|
|
{
|
2024-02-29 20:32:26 +00:00
|
|
|
//######RECORD SAMPLES######
|
|
|
|
sum = 0;
|
|
|
|
for(i=0;i<64;i++)
|
2024-02-28 21:52:37 +00:00
|
|
|
{
|
2024-03-01 19:34:42 +00:00
|
|
|
msec=0;
|
2024-02-28 21:52:37 +00:00
|
|
|
STARTADC;
|
2024-02-29 20:32:26 +00:00
|
|
|
sample[i] = (ADCH-128); //read Analog value register 8bit center -128
|
|
|
|
//sample[i] = (sample[i] <= NOISE) ? 0 : (sample[i] - NOISE); //noise reduction
|
2024-02-27 12:12:18 +00:00
|
|
|
im[i]=0; //pseudo data for funciton
|
2024-02-29 20:32:26 +00:00
|
|
|
//
|
|
|
|
sum+=sample[i];//dc-bias
|
2024-03-01 19:34:42 +00:00
|
|
|
|
2024-03-22 22:53:40 +00:00
|
|
|
//while(msec<=1);
|
2024-02-27 20:59:27 +00:00
|
|
|
}
|
2024-02-29 20:32:26 +00:00
|
|
|
avg = sum/64;//calculate bias
|
|
|
|
for(i=0;i<64;i++)
|
|
|
|
{
|
|
|
|
sample[i]-=avg;//remove bias
|
|
|
|
}
|
2024-03-22 22:53:40 +00:00
|
|
|
fix_fftr(sample,6,0);//im[i] is alwa§§ys zero
|
2024-02-28 21:52:37 +00:00
|
|
|
//####Averaging Data####
|
|
|
|
//
|
2024-02-29 20:32:26 +00:00
|
|
|
for(i=0;i<64;i++)
|
|
|
|
{
|
2024-03-01 19:34:42 +00:00
|
|
|
if(sample[i]<0) sample[i] = 0;
|
|
|
|
if(sample[i]>64) sample[i]=64;
|
2024-02-29 20:32:26 +00:00
|
|
|
sample[i] = sample[i] *SCALE_FACTOR;
|
2024-03-01 19:34:42 +00:00
|
|
|
sample[i] = (sample[i]*0.7 + buff[i]*0.3);
|
2024-02-29 20:32:26 +00:00
|
|
|
buff[i] = sample[i];
|
2024-03-01 19:34:42 +00:00
|
|
|
|
2024-02-29 20:32:26 +00:00
|
|
|
}
|
2024-02-27 20:59:27 +00:00
|
|
|
for(i=0;i<8;i++)
|
|
|
|
{
|
2024-02-29 20:45:44 +00:00
|
|
|
for(i2=1;i2<=4;i2++)
|
2024-02-28 21:52:37 +00:00
|
|
|
{
|
2024-03-01 19:49:59 +00:00
|
|
|
sample[i] = ((sample[i] + sample[i*8+i2])/2);
|
2024-02-28 21:52:37 +00:00
|
|
|
}
|
|
|
|
//avg written to sample[0..7]
|
2024-02-29 20:32:26 +00:00
|
|
|
|
2024-03-01 19:34:42 +00:00
|
|
|
if(sample[i]<20)
|
2024-02-29 20:32:26 +00:00
|
|
|
{
|
2024-03-22 22:53:40 +00:00
|
|
|
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;
|
2024-02-29 20:32:26 +00:00
|
|
|
}
|
2024-03-22 22:53:40 +00:00
|
|
|
else if(sample[i]<30)
|
2024-02-29 20:32:26 +00:00
|
|
|
{
|
|
|
|
led_bar1[i].r=0;
|
|
|
|
led_bar1[i].g=sample[i];
|
2024-03-01 19:34:42 +00:00
|
|
|
led_bar1[i].b=sample[i]/2;
|
2024-02-29 20:32:26 +00:00
|
|
|
}
|
2024-03-22 22:53:40 +00:00
|
|
|
else if(sample[i]<50)
|
2024-02-29 20:32:26 +00:00
|
|
|
{
|
|
|
|
led_bar1[i].r=sample[i];
|
|
|
|
led_bar1[i].g=0;
|
2024-03-01 19:34:42 +00:00
|
|
|
led_bar1[i].b=sample[i]/2;
|
2024-02-29 20:32:26 +00:00
|
|
|
}
|
2024-03-14 21:41:35 +00:00
|
|
|
}
|
2024-02-29 20:32:26 +00:00
|
|
|
ws2812_setleds(led_bar1,8);
|
2024-02-28 21:52:37 +00:00
|
|
|
}
|
2024-03-14 21:41:35 +00:00
|
|
|
return 0;
|
2024-02-27 12:12:18 +00:00
|
|
|
}
|
2024-02-28 21:52:37 +00:00
|
|
|
|
|
|
|
int map(int value, int old_min, int old_max,int new_min, int new_max)
|
|
|
|
{
|
|
|
|
return (new_max/old_max*value);
|
|
|
|
}
|
2024-03-01 09:49:07 +00:00
|
|
|
|
|
|
|
void setupTimer(void)
|
|
|
|
{
|
2024-03-22 22:53:40 +00:00
|
|
|
clock_prescale_set(0);
|
|
|
|
TCCR1 = (1<<CS10);//(1<<CS13)|(1<<CS10)|(1<<CTC1); //clear timer on compare match
|
2024-03-01 09:49:07 +00:00
|
|
|
GTCCR = 0; //default values should be ok
|
2024-03-22 22:53:40 +00:00
|
|
|
//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
|
2024-03-01 09:49:07 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* 1/(30Mhz/16384) *255 = 0.139264s Time for one overflow
|
|
|
|
* 1024 = 0.008704s
|
|
|
|
* 2024 = 0.017204s
|
|
|
|
* 255/0.017204*0.001=15 counts = 1ms
|
2024-03-01 19:34:42 +00:00
|
|
|
* 256 gives 1ms
|
|
|
|
*/
|
|
|
|
|
|
|
|
void wait_period(void)
|
|
|
|
{
|
|
|
|
// TIFR |=(1<<TOV1); //clear overflow flag
|
|
|
|
}
|
|
|
|
|
2024-03-22 22:53:40 +00:00
|
|
|
|
2024-03-01 19:34:42 +00:00
|
|
|
|