Example Audio Spectrum Analyzer

Dependencies:   mbed FFT

Files at this revision

API Documentation at this revision

Comitter:
Suky
Date:
Fri Feb 11 00:37:57 2011 +0000
Commit message:

Changed in this revision

DataCGRAM.h Show annotated file Show diff for this revision Revisions of this file
FFT.lib Show annotated file Show diff for this revision Revisions of this file
LCD_Serial.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 74f5a741c618 DataCGRAM.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DataCGRAM.h	Fri Feb 11 00:37:57 2011 +0000
@@ -0,0 +1,8 @@
+const char CGRAM_DATA[64]={0,0,0,0,0,0,0,0x1F,
+						    0,0,0,0,0,0,0x1F,0x1F,
+							0,0,0,0,0,0x1F,0x1F,0x1F,
+							0,0,0,0,0x1F,0x1F,0x1F,0x1F,
+							0,0,0,0x1F,0x1F,0x1F,0x1F,0x1F,
+							0,0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
+							0,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,
+							0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F};
diff -r 000000000000 -r 74f5a741c618 FFT.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/FFT.lib	Fri Feb 11 00:37:57 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/Suky/code/FFT/#e3af07c00c13
diff -r 000000000000 -r 74f5a741c618 LCD_Serial.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LCD_Serial.h	Fri Feb 11 00:37:57 2011 +0000
@@ -0,0 +1,229 @@
+/*
+  @file LCD_Serial.h
+  @version: 1.0
+  
+  @web www.micros-designs.com.ar
+  @date 05/02/11
+  
+*- Version Log --------------------------------------------------------------*
+*   Fecha       Autor                Comentarios                             *
+*----------------------------------------------------------------------------*
+* 05/02/11      Suky        Original                                         *
+*----------------------------------------------------------------------------*/ 
+///////////////////////////////////////////////////////////////////////////
+////                                                                   ////
+////                                                                   ////
+////        (C) Copyright 2011 www.micros-designs.com.ar               ////
+//// Este codigo puede ser usado, modificado y distribuido libremente  ////
+//// sin eliminar esta cabecera y  sin garantia de ningun tipo.        ////
+////                                                                   ////
+////                                                                   ////
+/////////////////////////////////////////////////////////////////////////// 
+#include "mbed.h"
+#include "DataCGRAM.h"
+
+class LCDSerial:public Stream {
+    public: 
+        #define LCDClr             0x01   // Borra pantalla, cursor a Inicio
+        #define LCDHome            0x02   // Cursor a Inicio, DDRAM sin cambios
+        #define LCDIncCursor       0x06   // Modo incrementa cursor
+        #define LCDDecCursor       0x04   // Modo decrementa cursor
+        #define LCDOn              0x0C   // Pantalla On
+        #define LCDOff             0x08   // Pantalla Off
+        #define LCDCursorOn        0x0E   // Pantalla On, cursor On
+        #define LCDCursorOff       0x0C   // Pantalla On, cursor Off
+        #define LCDCursorBlink     0x0F   // Pantalla On, Cursor parpadeante
+        #define LCDCursorLeft      0x10   // Mueve cursor a la izquierda
+        #define LCDCursorRight     0x14   // Mueve cursor a la derecha
+        #define LCDDisplayLeft     0x18   // Mueve Display   a la izquierda
+        #define LCDDisplayRight    0x1C   // Mueve   Display a la Derecha 
+         /** Crea LCD interface
+         *
+         * @param DATA    
+         * @param CLK   
+         * @param E
+         */
+        LCDSerial(PinName DATA,PinName CLK,PinName E,PinName BACK=NC);
+        void vGotoxy(char x,char y);
+        void vSetBacklight(bool Value);
+        void vPutc(char Data);
+        void vCommand(char Data);
+        #if DOXYGEN_ONLY
+            /** Escribe un caracter en LCD
+             *
+             * @param c El caracter a escribir en LCD
+             */
+            int putc(int c);
+        
+            /** Escribe un string formateado en LCD
+             *
+             * @param format A printf-style format string, followed by the
+             *               variables to use in formating the string.
+             */
+            int printf(const char* format, ...);
+        #endif       
+    private:    
+        #define LcdType 2                       // 0=5x7, 1=5x10, 2=varias lineas
+        #define LCD_LINE_1_ADDRESS  0x00
+        #define LCD_LINE_2_ADDRESS  0x40
+        #define LCD_LINE_3_ADDRESS  0x14
+        #define LCD_LINE_4_ADDRESS  0x54
+        #define LCD_COMMAND         0
+        #define LCD_DATA            1
+        DigitalOut _DATA,_CLK,_E,_BACK;
+        char NLinea;
+        // Stream implementation functions
+        virtual int _putc(int value);
+        virtual int _getc();
+        virtual void vWriteLCD(char Data,char Type);
+        virtual void vSetCGRAM(const char *Data);
+};
+
+
+LCDSerial::LCDSerial(PinName DATA,PinName CLK,PinName E,PinName BACK)
+    :_DATA(DATA),_CLK(CLK),_E(E),_BACK(BACK){
+    char Temp;
+    
+    NLinea=1;
+    wait_ms(15);
+    _DATA=0;
+    _CLK=0;
+    _E=0;
+    _BACK=0;
+
+    for(int k=0;k<8;k++){
+        _CLK=1;
+        wait_us(1);
+        _CLK=0;
+        wait_us(1);
+    } 
+    
+    Temp=0x03;
+    for(int k=0;k<8;k++){
+        _DATA=!!(Temp&0x80);
+        Temp<<=1;
+        _CLK=1;
+        wait_us(1);
+        _CLK=0;
+    }  
+    for(int k=0;k<3;k++){
+        _E=1;
+        wait_ms(2);
+        _E=0;
+        wait_ms(2);
+    }
+    Temp=0x02;
+    for(int k=0;k<8;k++){
+        _DATA=!!(Temp&0x80);
+        Temp<<=1;
+        _CLK=1;
+        wait_us(1);
+        _CLK=0;
+    }  
+    _E=1;
+    wait_us(1);
+    _E=0;
+    
+    vWriteLCD(0x20 | (LcdType<<2),LCD_COMMAND);  // Tipo display.-  
+    wait_ms(2);    
+    vWriteLCD(0x01,LCD_COMMAND);    // Borramos display.-   
+    wait_ms(2);          
+    vWriteLCD(0x06,LCD_COMMAND);    // Incrementa cursor.-
+    vWriteLCD(0x0C,LCD_COMMAND);    // Encendemos display.-
+    vSetCGRAM(CGRAM_DATA);          // Guardamos data en CGRAM.-
+}
+
+void LCDSerial::vWriteLCD(char Data,char Type){
+    char data_temp;
+
+
+    wait_us(100);
+
+    data_temp=(Data>>4);  // Rs es bit 4
+    if(Type){
+        data_temp|=0x10;
+    }
+    for(int i=0;i<8;i++){
+        _DATA=!!(data_temp & 0x80);
+        data_temp<<=1;
+        _CLK=1;
+        wait_us(1);
+        _CLK=0;
+    }
+   
+    _E = 1;
+    wait_us(1);
+    _E = 0;
+    data_temp=(Data&0x0F);  // Rs es bit 4
+    if(Type){
+        data_temp|=0x10;
+    }
+    for(int i=0;i<8;i++){
+        _DATA=!!(data_temp & 0x80);
+        data_temp<<=1;
+        _CLK=1;
+        wait_us(1);
+        _CLK=0;
+    }
+    _E = 1;
+    wait_us(1);
+    _E = 0;
+   
+} 
+
+void LCDSerial::vGotoxy(char x,char y){
+    char Direccion;
+
+    switch(y){
+        case 1:Direccion = LCD_LINE_1_ADDRESS;NLinea=1;break;
+        case 2:Direccion = LCD_LINE_2_ADDRESS;NLinea=2;break;
+        case 3:Direccion = LCD_LINE_3_ADDRESS;NLinea=3;break;
+        case 4:Direccion = LCD_LINE_4_ADDRESS;NLinea=4;break;
+        default:Direccion = LCD_LINE_1_ADDRESS;NLinea=1;break;
+    }
+
+    Direccion+=x-1;
+    vWriteLCD(0x80|Direccion,LCD_COMMAND);
+}
+
+int LCDSerial::_getc(){
+    return(-1);
+}
+
+int LCDSerial::_putc(int value){
+   
+   switch(value){
+      case '\f':
+         vWriteLCD(0x01,LCD_COMMAND);
+         NLinea=1;
+         wait_ms(2);
+      break;
+      case '\n':
+         vGotoxy(1,++NLinea);         
+      break;
+      default:
+         vWriteLCD(value,LCD_DATA);
+   }
+   
+   return(value);
+}
+
+void LCDSerial::vSetBacklight(bool Value){
+    _BACK=Value;
+}
+
+void LCDSerial::vSetCGRAM(const char *Data){
+
+    vWriteLCD(0x40,LCD_COMMAND);
+    for(int k=0;k<64;k++){
+        vWriteLCD(*Data++,LCD_DATA);
+    }
+}
+
+void LCDSerial::vPutc(char Data){
+    _putc(Data);
+}
+
+void LCDSerial::vCommand(char Data){
+    vWriteLCD(Data,LCD_COMMAND);
+}
diff -r 000000000000 -r 74f5a741c618 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Fri Feb 11 00:37:57 2011 +0000
@@ -0,0 +1,93 @@
+// Nothing special, an example for using the library of FFT, Audio Spectrum Analyzer with LCD
+#include "mbed.h"
+#include "FFT.h"
+#include "LCD_Serial.h"
+// **********************************************************************************************
+#define N 32
+float Data[N*2];  
+unsigned char PowerInt[N/2];
+volatile int kbhit;
+float Average;
+
+AnalogIn    ChannelR(p16);
+AnalogIn    ChannelL(p17);
+AnalogIn    ChannelC(p18);
+LCDSerial myLcd(p21,p22,p23,p24); // Data, Clock, Enable, Back
+// **********************************************************************************************
+void vUpdateDataAnalogs(float Analogs[]);
+void vGraphicDisplay(unsigned char *Data);
+// **********************************************************************************************
+int main() {
+    myLcd.vSetBacklight(1);
+    myLcd.printf("\f FFT with mbed!!!");
+    wait(2.0);
+    myLcd.printf("\f");
+    
+    Average=0;
+    for(int k=0;k<64;k++){
+        Average+=ChannelC.read_u16();
+        wait_us(100);
+    }
+    Average/=64;
+   
+    while(1){
+        vUpdateDataAnalogs(Data);
+        vFFT(Data-1,N);
+        vCalPowerInt(Data,PowerInt,N/2);
+        vGraphicDisplay(PowerInt);
+        wait_ms(30);
+    }
+}
+// **********************************************************************************************
+void vUpdate(void){
+    kbhit=1;
+}
+void vUpdateDataAnalogs(float Analogs[]){
+    unsigned short TempReadR[32],TempReadL[32];
+    Ticker Period;
+    
+    kbhit=0;
+    Period.attach_us(&vUpdate,25); // 25 us, 40.000 Hz
+    for(int k=0;k<32;k++){
+        while(kbhit==0);
+        kbhit=0;
+        TempReadR[k]=ChannelR.read_u16();
+    }
+    for(int k=0;k<32;k++){
+        while(kbhit==0);
+        kbhit=0;
+        TempReadL[k]=ChannelL.read_u16();
+    }
+    Period.detach();
+    for(int k=0,j=0;k<32;k++,j++){
+        Analogs[j]=(TempReadR[k]+TempReadL[k])/2;   // Promedio (x1[n]+x2[n]=X1[k]+X2[k])
+        Analogs[j]=((float)(Analogs[j]-Average)/10);   // Desplazo y aplico escala.-
+        Analogs[++j]=0;
+    }
+    return;
+}
+// **********************************************************************************************
+void vGraphicDisplay(unsigned char *Data){
+     unsigned char k;
+ 
+     myLcd.vGotoxy(3,1);
+     for(k=0;k<16;k++,Data++){
+         if(*Data>192){myLcd.vPutc((*Data-193)/8);}else{myLcd.vPutc(' ');}
+     }
+     myLcd.vGotoxy(3,2);
+     Data-=16;
+    for(k=0;k<16;k++,Data++){
+         if((*Data>128)&&(*Data<193)){myLcd.vPutc((*Data-129)/8);}else if(*Data>192){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
+     }
+     myLcd.vGotoxy(3,3);
+     Data-=16;
+     for(k=0;k<16;k++,Data++){
+         if((*Data>64)&&(*Data<129)){myLcd.vPutc((*Data-65)/8);}else if(*Data>128){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
+     }
+     myLcd.vGotoxy(3,4);
+     Data-=16;
+     for(k=0;k<16;k++,Data++){
+         if((*Data>0)&&(*Data<65)){myLcd.vPutc((*Data-1)/8);}else if(*Data>64){myLcd.vPutc(0x07);}else{myLcd.vPutc(' ');}
+     }
+ 
+ }
\ No newline at end of file
diff -r 000000000000 -r 74f5a741c618 mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Fri Feb 11 00:37:57 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/9a9732ce53a1