⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 adc.c

📁 专门针对avr系列的单片机优化的fft算法
💻 C
字号:
#include "adc.h"
#include "lcd.h"
#include "ffft.h"
#include "uart.h"

void adc_initial(void)
{
	DDRF = 0x00; // configure a2d port (PORTF) as input so we can receive analog signals
	PORTF = 0x00; // make sure pull-up resistors are turned off (else we’ll just read 0xCFF)
	
	sbi(ADCSRA,ADEN); // enables ADC by setting bit 7 (ADEN) in the ADCSRA
	cbi(ADCSRA,ADFR); // single sample conversion by clearing bit 5 (ADFR) in the ADCSRA
	ADCSRA = ((ADCSRA & 0b11111000) | 0b00000110); // clk/64
	
	print("ADC initialized.\r\n");	
}

void adc_capture (unsigned char channel, unsigned int *voltage)
{
	unsigned int i;

	ADMUX = 0x40 + channel;	// selects single-ended conversion on pin PF0 ~ PF7 (ADC0 ~ ADC7)
							// selects AVCC as Vref
							// selects right adjust of ADC result
												
	for (i = 0; i < FFT_N; i++)	// averaging the ADC results
	{
		sbi(ADCSRA,ADSC); // start a conversion by writing a one to the ADSC bit (bit 6)
		while(!(ADCSRA & 0b00010000)) // wait for conversion to complete: ADIF = 1
		{
			//sentchar1('.');		// for debugging
		}
		*voltage++ = ((ADCL) | (ADCH<<8));
	}
}

void adc_fft (unsigned char channel)
{
	unsigned int i;
	unsigned int voltage[FFT_N];
	int16_t capture[FFT_N];			/* Wave capturing buffer */
	complex_t bfly_buff[FFT_N];		/* FFT buffer */
	uint16_t spektrum[FFT_N/2];		/* Spectrum output buffer */
	
	// ADC 5v to 10-bit digital (0x00 - 0x3FF)
	adc_capture(channel, voltage);	

	// change 5v to +/-2.5v
	for (i = 0; i < FFT_N; i++)
	{		
		capture[i] = voltage[i] - 0x200;
	}

	// Fixed-point FFT routines for megaAVRs, (C)ChaN, 2005
	// http://www.embedds.com/avr-audio-spectrum-monitor-on-graphical-lcd/
	fft_input(capture, bfly_buff);
	fft_execute(bfly_buff);
	fft_output(bfly_buff, spektrum);
	
	// now show the results
	lcd_print_signal(voltage);
	lcd_print_spectrum(spektrum);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -