Components and supplies
Arduino UNO
Standard LCD - 16x2 White on Blue
Apps and platforms
Microchip Studio
Project description
Code
DFT Audio Analyzer
c_cpp
1/* 2 * DFT_on_AVR.c 3 * 4 * Created: 19-08-2016 11:02:32 PM 5 * Author : Akash Kollipara 6 */ 7#define F_CPU 16000000UL 8#define RS 2 9#define EN 3 10 11#include <avr/io.h> 12#include <util/delay.h> 13#include <math.h> 14#include <avr/pgmspace.h> 15 16//INITIALZATIONS 17 18#define N 32 //#samples 19uint16_t f[N], angle; 20float cs, ss; 21uint8_t x, k; 22char pin; 23char arr[8][8]= 24{ 25 {0, 0, 0, 0, 0, 0, 0, 31}, //L1 26 {0, 0, 0, 0, 0, 0, 31, 31}, //L2 27 {0, 0, 0, 0, 0, 31, 31, 31}, //L3 28 {0, 0, 0, 0, 31, 31, 31, 31}, //L4 29 {0, 0, 0, 31, 31, 31, 31, 31}, //L5 30 {0, 0, 31, 31, 31, 31, 31, 31}, //L6 31 {0, 31, 31, 31, 31, 31, 31, 31}, //L7 32 {31, 31, 31, 31, 31, 31, 31, 31} //L8 33}; 34 35//PRTOTYPING 36 37//LCD FUNCTIONS 38void lcd_init(); 39void cmd(char); 40void Data(char); 41void lcd_clear(); 42void lcd_printc(char); 43void lcd_prints(char *); 44void lcd_cust_char(char, char *); 45void lcd_setCursor(char, char); 46 47//ADC FUNCTIONS 48void adc_init(); 49uint16_t analogRead(); 50 51//BOOTUP 52void bootup(); 53 54const int16_t cosine_lookup[] PROGMEM = 55{ 56 1000, 980, 923, 831, 707, 555, 382, 195, 0, -195, -382, -555, -707, -831, -923, -980, -1000, -980, -923, -831, -707, -555, -382, -195, 0, 195, 382, 555, 707, 831, 923, 980, 1000, 923, 707, 382, 0, -382, -707, -923, -1000, -923, -707, -382, 0, 382, 707, 923, 1000, 923, 707, 382, 0, -382, -707, -923, -1000, -923, -707, -382, 0, 382, 707, 923, 1000, 831, 382, -195, -707, -980, -923, -555, 0, 555, 923, 980, 707, 195, -382, -831, -1000, -831, -382, 195, 707, 980, 923, 555, 0, -555, -923, -980, -707, -195, 382, 831, 1000, 707, 0, -707, -1000, -707, 0, 707, 1000, 707, 0, -707, -1000, -707, 0, 707, 999, 707, 0, -707, -1000, -707, 0, 707, 1000, 707, 0, -707, -1000, -707, 0, 707, 1000, 555, -382, -980, -707, 195, 923, 831, 0, -831, -923, -195, 707, 980, 382, -555, -1000, -555, 382, 980, 707, -195, -923, -831, 0, 831, 923, 195, -707, -980, -382, 555, 1000, 382, -707, -923, 0, 923, 707, -382, -1000, -382, 707, 923, 0, -923, -707, 382, 1000, 382, -707, -923, 0, 923, 707, -382, -1000, -382, 707, 923, 0, -923, -707, 382, 1000, 195, -923, -555, 707, 831, -382, -980, 0, 980, 382, -831, -707, 555, 923, -195, -1000, -195, 923, 555, -707, -831, 382, 980, 0, -980, -382, 831, 707, -555, -923, 195, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 999, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, -195, -923, 555, 707, -831, -382, 980, 0, -980, 382, 831, -707, -555, 923, 195, -1000, 195, 923, -555, -707, 831, 382, -980, 0, 980, -382, -831, 707, 555, -923, -195, 1000, -382, -707, 923, 0, -923, 707, 382, -1000, 382, 707, -923, 0, 923, -707, -382, 1000, -382, -707, 923, 0, -923, 707, 382, -1000, 382, 707, -923, 0, 923, -707, -382, 1000, -555, -382, 980, -707, -195, 923, -831, 0, 831, -923, 195, 707, -980, 382, 555, -1000, 555, 382, -980, 707, 195, -923, 831, 0, -831, 923, -195, -707, 980, -382, -555, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -831, 382, 195, -707, 980, -923, 555, 0, -555, 923, -980, 707, -195, -382, 831, -1000, 831, -382, -195, 707, -980, 923, -555, 0, 555, -923, 980, -707, 195, 382, -831, 1000, -923, 707, -382, 0, 382, -707, 923, -1000, 923, -707, 382, 0, -382, 707, -923, 1000, -923, 707, -382, 0, 382, -707, 923, -1000, 923, -707, 382, 0, -382, 707, -923, 1000, -980, 923, -831, 707, -555, 382, -195, 0, 195, -382, 555, -707, 831, -923, 980, -1000, 980, -923, 831, -707, 555, -382, 195, 0, -195, 382, -555, 707, -831, 923, -980, 1000, -1000, 1000, -1000, 999, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -1000, 1000, -999, 1000, -1000, 1000, -1000, 1000, -999, 1000, -1000 57}; 58 59const int16_t sine_lookup[] PROGMEM = 60{ 61 0, 195, 382, 555, 707, 831, 923, 980, 1000, 980, 923, 831, 707, 555, 382, 195, 0, -195, -382, -555, -707, -831, -923, -980, -1000, -980, -923, -831, -707, -555, -382, -195, 0, 382, 707, 923, 1000, 923, 707, 382, 0, -382, -707, -923, -1000, -923, -707, -382, 0, 382, 707, 923, 999, 923, 707, 382, 0, -382, -707, -923, -1000, -923, -707, -382, 0, 555, 923, 980, 707, 195, -382, -831, -1000, -831, -382, 195, 707, 980, 923, 555, 0, -555, -923, -980, -707, -195, 382, 831, 1000, 831, 382, -195, -707, -980, -923, -555, 0, 707, 1000, 707, 0, -707, -1000, -707, 0, 707, 999, 707, 0, -707, -1000, -707, 0, 707, 1000, 707, 0, -707, -1000, -707, 0, 707, 1000, 707, 0, -707, -1000, -707, 0, 831, 923, 195, -707, -980, -382, 555, 999, 555, -382, -980, -707, 195, 923, 831, 0, -831, -923, -195, 707, 980, 382, -555, -1000, -555, 382, 980, 707, -195, -923, -831, 0, 923, 707, -382, -1000, -382, 707, 923, 0, -923, -707, 382, 1000, 382, -707, -923, 0, 923, 707, -382, -1000, -382, 707, 923, 0, -923, -707, 382, 1000, 382, -707, -923, 0, 980, 382, -831, -707, 555, 923, -195, -1000, -195, 923, 555, -707, -831, 382, 980, 0, -980, -382, 831, 707, -555, -923, 195, 1000, 195, -923, -555, 707, 831, -382, -980, 0, 1000, 0, -1000, 0, 999, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 1000, 0, -1000, 0, 980, -382, -831, 707, 555, -923, -195, 1000, -195, -923, 555, 707, -831, -382, 980, 0, -980, 382, 831, -707, -555, 923, 195, -1000, 195, 923, -555, -707, 831, 382, -980, 0, 923, -707, -382, 999, -382, -707, 923, 0, -923, 707, 382, -1000, 382, 707, -923, 0, 923, -707, -382, 1000, -382, -707, 923, 0, -923, 707, 382, -1000, 382, 707, -923, 0, 831, -923, 195, 707, -980, 382, 555, -1000, 555, 382, -980, 707, 195, -923, 831, 0, -831, 923, -195, -707, 980, -382, -555, 1000, -555, -382, 980, -707, -195, 923, -831, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -1000, 707, 0, -707, 1000, -707, 0, 707, -999, 707, 0, -707, 999, -707, 0, 555, -923, 980, -707, 195, 382, -831, 1000, -831, 382, 195, -707, 980, -923, 555, 0, -555, 923, -980, 707, -195, -382, 831, -1000, 831, -382, -195, 707, -980, 923, -555, 0, 382, -707, 923, -1000, 923, -707, 382, 0, -382, 707, -923, 1000, -923, 707, -382, 0, 382, -707, 923, -1000, 923, -707, 382, 0, -382, 707, -923, 999, -923, 707, -382, 0, 195, -382, 555, -707, 831, -923, 980, -1000, 980, -923, 831, -707, 555, -382, 195, 0, -195, 382, -555, 707, -831, 923, -980, 999, -980, 923, -831, 707, -555, 382, -195, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 62}; 63 64 65//MAIN 66int main(void) 67{ 68 bootup(); 69 while(1) 70 { 71 for(k = 0; k < N; k++) 72 f[k] = analogRead(); //fetch samples 73 for(k = 0; k < N/2; k++) 74 { 75 cs = ss = 0; 76 for(x = 0; x < N; x++) 77 { 78 angle = x + (32*k); 79 cs += (float)f[x]*(int16_t)pgm_read_word(&cosine_lookup[angle]); //real part of dft 80 ss += (float)f[x]*(int16_t)pgm_read_word(&sine_lookup[angle]); //imaginary part of dft 81 } 82 cs /= N*1000; 83 ss /= N*1000; 84 x = (uint8_t)sqrt((cs*cs)+(ss*ss)); //absolute value of dft 85 x /= 2; //scaling 86 x++; 87 if(x > 16) x = 16; 88 if(x < 9) 89 { 90 lcd_setCursor(k, 0); 91 lcd_printc(' '); 92 lcd_setCursor(k, 1); 93 lcd_printc(x); 94 95 } 96 else 97 { 98 lcd_setCursor(k, 1); 99 lcd_printc(8); 100 lcd_setCursor(k, 0); 101 lcd_printc(x-8); 102 } 103 } 104 if(!(PINB & 0x01) && ((pin & 0x80) == 0x00)) 105 { 106 pin |= 0x80; 107 pin++; 108 if((pin & 0x0f)>1) pin &= 0xf0; 109 switch((pin & 0x0f)) 110 { 111 case 0: 112 { 113 ADCSRA = 0x86; 114 lcd_clear(); 115 lcd_setCursor(2, 0); 116 lcd_prints("560 - 8960 Hz"); 117 _delay_ms(1000); 118 break; 119 } 120 case 1: 121 { 122 ADCSRA = 0x87; 123 lcd_clear(); 124 lcd_setCursor(2, 0); 125 lcd_prints("280 - 4480 Hz"); 126 _delay_ms(1000); 127 break; 128 } 129 } 130 } 131 else if((pin & 0x80) && ((PINB & 0x01))) 132 { 133 pin &= 0x7f; 134 } 135 } 136 return 0; 137} 138 139//bootup functions 140void bootup() 141{ 142 PORTB = 0x01; //internal pull-up 143 DDRD = 0xfc; //set o/p port for LCD 144 adc_init(); 145 _delay_ms(1); 146 lcd_init(); 147 for(k = 0; k < 8; k++) 148 lcd_cust_char((k+1), arr[k]); //load custom characters 149 lcd_clear(); 150} 151 152//SYSTEM FUNCTIONS 153void adc_init() 154{ 155 ADCSRA = 0x86; 156 ADMUX = 0x40; 157} 158 159uint16_t analogRead() 160{ 161 uint16_t adcl, adch; 162 ADCSRA |= (1<<ADSC); 163 while((ADCSRA & (1<<ADSC))); 164 adcl = ADCL; 165 adch = ADCH; 166 return((adch<<8)|adcl); 167} 168 169void lcd_init() 170{ 171 cmd(0x28); //2->4-bit mode, 8->2-line disp mode 172 _delay_ms(1); 173 cmd(0x28); //2->4-bit mode, 8->2-line disp mode 174 _delay_ms(1); 175 cmd(0x0c); 176 _delay_ms(1); 177 cmd(0x06); 178 _delay_ms(1); 179 cmd(0x80); 180 _delay_ms(1); 181 lcd_clear(); 182} 183 184void cmd(char comm) 185{ 186 PORTD = (comm & 0xf0) | (1<<EN); 187 PORTD = (comm & 0xf0) & (~(1<<EN)); 188 PORTD = ((comm << 4) & 0xf0) | (1<<EN); 189 PORTD = ((comm << 4) & 0xf0) & (~(1<<EN)); 190 _delay_us(1150); 191} 192 193void lcd_clear() 194{ 195 cmd(0x01); 196} 197 198void lcd_printc(char c) 199{ 200 Data(c); 201} 202 203void lcd_prints(char *arr) 204{ 205 for(uint8_t i = 0; arr[i] != '\\0'; i++) 206 Data(arr[i]); 207} 208 209void Data(char comm) 210{ 211 PORTD = (comm & 0xf0) | (1<<EN) | (1<<RS); 212 PORTD = ((comm & 0xf0) & (~(1<<EN))) | (1<<RS); 213 PORTD = ((comm<<4) & 0xf0) | (1<<EN) | (1<<RS); 214 PORTD = (((comm<<4) & 0xf0) & (~(0<<EN))) | (1<<RS); 215 _delay_us(100); 216} 217 218void lcd_cust_char(char i, char *arr) 219{ 220 cmd(0x40 | (8*i)); 221 for(int j = 0; j <8; j++) 222 Data(arr[j]); 223 cmd(0x80); 224} 225 226void lcd_setCursor(char x, char y) 227{ 228 if(y) 229 cmd(0x80 + 0x40 + x); 230 else if(!y) 231 cmd(0x80 + x); 232} 233
Comments
Only logged in users can leave comments
AkashKollipara
0 Followers
•0 Projects
Table of contents
Intro
6
0