fix different bug, not zeroing rgb, not manipulating over each datapoint
This commit is contained in:
parent
b710fed79b
commit
4b46c16539
13
avr_adc.c
13
avr_adc.c
@ -37,16 +37,17 @@ void setupADC(void)
|
|||||||
//_BC(REFS1) | //Set_Vref
|
//_BC(REFS1) | //Set_Vref
|
||||||
//_BC(MUX3) | //
|
//_BC(MUX3) | //
|
||||||
//_BC(MUX2) | //
|
//_BC(MUX2) | //
|
||||||
_BS(MUX1) | //
|
// _BS(MUX1) | //
|
||||||
_BS(MUX0) | //PORT Select PB3
|
_BS(MUX0) | //PORT Select PB3
|
||||||
_BS(ADLAR); //left shift conversion results(used in 8bit mode)
|
_BS(ADLAR); //left shift conversion results(used in 8bit mode)
|
||||||
|
// ADCSRB = _BS(
|
||||||
ADCSRA = _BS(ADEN) | //Enable ADC
|
ADCSRA = _BS(ADEN) | //Enable ADC
|
||||||
//_BC(ADATE) | //Disable AutoTrigger aka Freerunning mode
|
//_BC(ADATE) | //Disable AutoTrigger aka Freerunning mode
|
||||||
_BS(ADPS2) | //Select Prescaler clk/32
|
// _BS(ADPS1) | //Select Prescaler
|
||||||
//_BC(ADPS1) |
|
_BS(ADPS0) | //prescaler 128
|
||||||
_BS(ADPS0);
|
//_BS(ADPS1) |
|
||||||
|
_BS(ADPS2);
|
||||||
|
ADCSRA &= ~(1<<ADATE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
82
main.c
82
main.c
@ -1,4 +1,3 @@
|
|||||||
#define F_CPU 16000000UL
|
|
||||||
|
|
||||||
#include<util/delay.h>
|
#include<util/delay.h>
|
||||||
#include<avr/io.h>
|
#include<avr/io.h>
|
||||||
@ -6,11 +5,12 @@
|
|||||||
#include"avr_adc.h"
|
#include"avr_adc.h"
|
||||||
#include"light_ws2812_AVR/Light_WS2812/ws2812_config.h"
|
#include"light_ws2812_AVR/Light_WS2812/ws2812_config.h"
|
||||||
#include"light_ws2812_AVR/Light_WS2812/light_ws2812.h"
|
#include"light_ws2812_AVR/Light_WS2812/light_ws2812.h"
|
||||||
|
#include"main.h"
|
||||||
|
|
||||||
#define STARTADC ADCSRA |= (1<<ADSC)
|
#define STARTADC ADCSRA |= (1<<ADSC)
|
||||||
#define PB_mic PB0
|
#define PB_mic PB0
|
||||||
|
#define NOISE 100
|
||||||
|
#define SCALE_FACTOR 8
|
||||||
//resolution fft calc:
|
//resolution fft calc:
|
||||||
//https://3roam.com/fft-resolution-bandwidth-calculator/
|
//https://3roam.com/fft-resolution-bandwidth-calculator/
|
||||||
//128 samples , 1kSPS = 7.8Hz
|
//128 samples , 1kSPS = 7.8Hz
|
||||||
@ -29,63 +29,101 @@
|
|||||||
//6- 755
|
//6- 755
|
||||||
//7- 880
|
//7- 880
|
||||||
//8- 1kHz
|
//8- 1kHz
|
||||||
|
//
|
||||||
|
const float log_scale = 64./log(64./SCALE_FACTOR + 1.);
|
||||||
|
|
||||||
int main (void)
|
int main (void)
|
||||||
{
|
{
|
||||||
int i,i2;
|
int i,i2;
|
||||||
int colorb;
|
int colorb;
|
||||||
int colora;
|
int colora;
|
||||||
int avg;
|
int sum,avg;
|
||||||
char sample[128];
|
char sample[64];
|
||||||
char im[128];
|
char im[64];
|
||||||
|
char buff[64];
|
||||||
|
char temp;
|
||||||
struct cRGB led_bar1[8];
|
struct cRGB led_bar1[8];
|
||||||
//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();
|
||||||
|
OSCCAL = 250;//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;
|
||||||
led_bar1[i].g=0;
|
led_bar1[i].g=0;
|
||||||
led_bar1[i].b=0;
|
led_bar1[i].b=0;
|
||||||
}
|
}
|
||||||
|
for(i=0;i<64;i++)
|
||||||
|
{
|
||||||
|
buff[i]=0;
|
||||||
|
}
|
||||||
while(1)
|
while(1)
|
||||||
{
|
{
|
||||||
//######RECORD SAMPLES#######
|
//######RECORD SAMPLES######
|
||||||
for(i=0;i<128;i++)
|
sum = 0;
|
||||||
|
for(i=0;i<64;i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
STARTADC;
|
STARTADC;
|
||||||
sample[i] = (ADCH-128)*2; //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
|
||||||
im[i]=0; //pseudo data for funciton
|
im[i]=0; //pseudo data for funciton
|
||||||
|
//
|
||||||
|
sum+=sample[i];//dc-bias
|
||||||
}
|
}
|
||||||
fix_fft(sample,im,7,0);//im[i] is always zero
|
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####
|
//####Averaging Data####
|
||||||
//
|
//
|
||||||
for(i2=0;i2<128;i2++)
|
for(i=0;i<64;i++)
|
||||||
{
|
{
|
||||||
|
|
||||||
sample[i2] = sqrt(sample[i2] * sample[i2] + im[i2]* im[i2]);
|
sample[i] = sample[i] *SCALE_FACTOR;
|
||||||
|
sample[i] = (sample[i]*0.6 + buff[i]*0.5);
|
||||||
|
buff[i] = sample[i];
|
||||||
|
if(sample[i]<0) sample[i] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for(i=0;i<8;i++)
|
for(i=0;i<8;i++)
|
||||||
{
|
{
|
||||||
for(i2=1;i2<=16;i2++)
|
for(i2=1;i2<=8;i2++)
|
||||||
{
|
{
|
||||||
sample[i] = (sample[i] + sample[i*16+i2])/2;
|
sample[i] = ((sample[i] + sample[i*8+i2])/2);
|
||||||
}
|
}
|
||||||
//avg written to sample[0..7]
|
//avg written to sample[0..7]
|
||||||
//contains values scaled to int boundarys
|
//contains values scaled to int boundarys
|
||||||
//scale them to 0..256
|
//scale them to 0..256
|
||||||
sample[i]= map(sample[i],0,30,0,255);
|
|
||||||
led_bar1[i].r=0;;led_bar1[i].b=0;
|
|
||||||
//if(sample[i]<2) sample[i]=2;
|
|
||||||
led_bar1[i].g=sample[i];
|
|
||||||
|
|
||||||
|
|
||||||
|
//sample[i]= map(sample[i],0,50,0,255);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if(sample[i]<30)
|
||||||
|
{
|
||||||
|
led_bar1[i].r=0;
|
||||||
|
led_bar1[i].g=0;
|
||||||
|
led_bar1[i].b=sample[i];
|
||||||
|
}
|
||||||
|
else if(sample[i]<50)
|
||||||
|
{
|
||||||
|
led_bar1[i].r=0;
|
||||||
|
led_bar1[i].g=sample[i];
|
||||||
|
led_bar1[i].b=0;
|
||||||
|
}
|
||||||
|
else if(sample[i]<100)
|
||||||
|
{
|
||||||
|
led_bar1[i].r=sample[i];
|
||||||
|
led_bar1[i].g=0;
|
||||||
|
led_bar1[i].b=0;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
ws2812_setleds((uint8_t *)led_bar1,8);
|
ws2812_setleds(led_bar1,8);
|
||||||
}
|
}
|
||||||
|
|
||||||
// _delay_ms(50);
|
// _delay_ms(50);
|
||||||
|
Loading…
Reference in New Issue
Block a user