Forum >Audio Analyzer
ArduinoGeneral

Audio Analyzer

userHead pandora 2012-05-30 11:14:09 5688 Views6 Replies
Hello,

I came again, now I want to try using audio analyzer (+analog sound sensor) connect to atmega16 (coding with cvavr).
I've seen the sample code in the wiki, but I have trouble to change library code in arduino to cvavr.
can you help me?
2012-07-03 18:33:14 Hi Pandora,


Thanks for the contribution! Your code has been added to the Wiki. Please let me know if you would like me to make some modification.


[url=https://www.dfrobot.com/wiki/index.php?title=Audio_Analyzer_(SKU:DFR0126)#Sample_code]https://www.dfrobot.com/wiki/index.php?title=Audio_Analyzer_(SKU:DFR0126)#Sample_code[/url]
userHeadPic Hector
2012-07-03 03:46:29 Hi,

This convert code to cvavr :
I'm using atmega128 (clock 16Mhz), usart0 (9600 baud rate), timer1 (scale clock 1024), adc.
formula timer1 when use clock freq 16Mhz

Ttimer1 = Periode Timer1
TCNT1 = Register Timer1
N = Scale clock (1, 8, 64, 256 dan 1024)
Tosc = Periode  clock
Fosc = Frekuensi clock cristal

Tosc = 1/Fosc
Tosc = 1/16Mhz = 0,0000000625

Ttimer1 = Tosc * (65536 - TCNT1) * N
1 (second) = 0,0000000625 * (65536 - TCNT1) * 1024
TCNT1 = 49911
TCNT1 = C2F7 (in hex) <-- this use in //Timer 1 overflow interrupt service routine

Clock value = Fosc/N
Clock value = 16Mhz/1024 = 15,625 kHz <-- this use in clock value timer1

Can use timer0 or other timer with each formula

CMIIW. thanks.

[code]
/*****************************************************
Chip type          : ATmega128
Program type        : Application
Clock frequency    : 16,000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size    : 1024
*****************************************************/

int sec, band, freq[7], i;
unsigned long int time_a, time_b;
int stat = 0;

#include <mega128.h>
#include <stdio.h>
#include <delay.h>

// Timer 1 overflow interrupt service routine
interrupt [TIM1_OVF] void timer1_ovf_isr(void)
{
// Reinitialize Timer 1 value
TCNT1H=0xC2F7 >> 8;
TCNT1L=0xC2F7 & 0xff;
// Place your code here
sec++;

}

#define ADC_VREF_TYPE 0x40

// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{

ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;

}

void RstModule()
{

PORTD.0 = 0; //S Low
PORTD.1 = 1; //R High
PORTD.0 = 1; //S High
PORTD.0 = 0; //S Low
PORTD.1 = 0; //R Low
delay_us(72);

}

void Init()
{
           
DDRD.0 = 1; //S pin
DDRD.1 = 1; //R pin
RstModule();

}

void ReadFreq(int *value)
{

if (stat == 0) {
  time_a = sec;
  stat = 1;
} else if (stat == 1) {
  time_b = sec;
  if (time_b - time_a > 3) {
      RstModule();
      stat = 0;
  }
}
for (band=0;band<7;band++) {
  delay_us(10);
  value[band] = read_adc(0);
  delay_us(50);
  PORTD.0 = 1; //S High
  delay_us(18);
  PORTD.0 = 0; //S Low
}

}

void main(void)
{

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 15,625 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// OC1C output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: On
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
// Compare C Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x05;
TCNT1H=0xC2;
TCNT1L=0xF7;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
OCR1CH=0x00;
OCR1CL=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x04;
ETIMSK=0x00;

// USART0 initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART0 Receiver: Off
// USART0 Transmitter: On
// USART0 Mode: Asynchronous
// USART0 Baud Rate: 9600
UCSR0A=0x00;
UCSR0B=0x08;
UCSR0C=0x06;
UBRR0H=0x00;
UBRR0L=0x67;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 1000,000 kHz
// ADC Voltage Reference: AREF pin
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;

// Global enable interrupts
#asm("sei")

Init();

while (1)
      {
        ReadFreq(freq);
       
        for (i=0;i<7;i++) {
          printf("%d",freq[i]-100);
          if(i<6) printf(", ");
          else printf("\r\n");
        }
       
        delay_ms(20);

      };
}
[/code]
userHeadPic pandora
2012-07-03 00:33:55 Hi,


If there is no sound then you could end up with negative numbers.

userHeadPic Hector
2012-06-30 05:15:22 finally, success convert code to cvavr and working.
maybe tomorrow I will post the code.

I want to ask, data FreqVal[] number 3,4,5,6 the result is minus if there is no sound. it's true?

example result if there is no sound :

484, 180, 31, -28, -37, -8, -43
userHeadPic pandora
2012-05-31 20:19:17 Thanks for reply hector,

oke i'll try it. but I'm still confuse about "millis()" to convert to cvavr.
i'll post it if succeed.
userHeadPic pandora
2012-05-31 19:39:23 Hi Pandora,


I'm not familiar with cvavr. We supply the code as is, we really can not help with modifying code. I really have a lot of work and lots of requests as you can see in the forum. I would love to help with modifying code, but alas, there is only one of me :(
If you can succeed in making new code, please post it here and I will add it to the wiki for others to use.
userHeadPic Hector