Small simple FFT for 18f452 or similar PIC in mikoBasic

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
Tvojmuc
Posts: 51
Joined: 08 Nov 2010 18:48
Location: Slovenija

Small simple FFT for 18f452 or similar PIC in mikoBasic

#1 Post by Tvojmuc » 16 Jul 2011 10:17

Hi to all,
I would like to build simple level meter of audio signal for 6 different frequency canal something like small spectrum analyzer with 6 canal (i believe simple version of FFT) Is something similar like this available on someone HD and are you ready to share? :wink:
Last edited by Tvojmuc on 30 Jul 2011 07:10, edited 1 time in total.

Tvojmuc
Posts: 51
Joined: 08 Nov 2010 18:48
Location: Slovenija

Re: Small simple FFT for 18f452 or similar PIC in mikoBasic

#2 Post by Tvojmuc » 30 Jul 2011 07:09

This is what I build till now...but i need interupt on 40kHz
any help? :idea: :idea:
is working super but sampling in 11kHz :? :?

Code: Select all

program MyProject

'18F452.........20MHz

    Dim SPExpanderRST as sbit at RC0_bit
        SPExpanderCS  as sbit at RC1_bit
        SPExpanderRST_Direction as sbit at TRISC0_bit
        SPExpanderCS_Direction  as sbit at TRISC1_bit


    Dim AG as word[1]
    Dim LCD_RS as sbit at RB4_bit
        LCD_EN as sbit at RB5_bit
        LCD_D4 as sbit at RB0_bit
        LCD_D5 as sbit at RB1_bit
        LCD_D6 as sbit at RB2_bit
        LCD_D7 as sbit at RB3_bit
        LCD_RS_Direction as sbit at TRISB4_bit
        LCD_EN_Direction as sbit at TRISB5_bit
        LCD_D4_Direction as sbit at TRISB0_bit
        LCD_D5_Direction as sbit at TRISB1_bit
        LCD_D6_Direction as sbit at TRISB2_bit
        LCD_D7_Direction as sbit at TRISB3_bit
    Dim txt1 as char[3]
        'txt2 as char[16]
    Dim C1,C2 as float[60]
    Dim B as float[6]
    Dim NE As byte
    Dim N1 ,Z1 As byte
    Dim SpreR1 as float[6][2]
    Dim SW AS float[6]
    Dim k1,p1  as float
    Dim prog as byte
        prog1 as byte
    Dim oldstateA as byte
        oldstateB as byte
        oldstateC as byte
        oldstateD as byte
        vol as byte
        volold as byte
        stevv as byte
        x1 as byte
sub procedure spremenljivke
    C1[0]= 1
    C1[1]= 1
    C1[2]= 1
    C1[3]= 1
    C1[4]= 1
    C1[5]= 1
    C1[6]= 1
    C1[7]= 1
    C1[8]= 1
    C1[9]= 1
    C1[10]= 1
    C1[11]= 0.809016994374947
    C1[12]= 0.309016994374947
    C1[13]= -0.309016994374947
    C1[14]= -0.809016994374947
    C1[15]= -1
    C1[16]= -0.809016994374948
    C1[17]= -0.309016994374948
    C1[18]= 0.309016994374947
    C1[19]= 0.809016994374947
    C1[20]= 1
    C1[21]= 0.309016994374947
    C1[22]= -0.809016994374947
    C1[23]= -0.809016994374948
    C1[24]= 0.309016994374947
    C1[25]= 1
    C1[26]= 0.309016994374949
    C1[27]= -0.809016994374947
    C1[28]= -0.809016994374948
    C1[29]= 0.309016994374945
    C1[30]= 1
    C1[31]= -0.309016994374947
    C1[32]= -0.809016994374948
    C1[33]= 0.809016994374947
    C1[34]= 0.309016994374948
    C1[35]= -1
    C1[36]= 0.309016994374945
    C1[37]= 0.809016994374948
    C1[38]= -0.809016994374947
    C1[39]= -0.309016994374948
    C1[40]= 1
    C1[41]= -0.809016994374947
    C1[42]= 0.309016994374947
    C1[43]= 0.309016994374949
    C1[44]= -0.809016994374948
    C1[45]= 1
    C1[46]= -0.809016994374946
    C1[47]= 0.309016994374947
    C1[48]= 0.309016994374948
    C1[49]= -0.80901699437495
    C1[50]= 1
    C1[51]= -1
    C1[52]= 1
    C1[53]= -1
    C1[54]= 1
    C1[55]= -1
    C1[56]= 1
    C1[57]= -1
    C1[58]= 1
    C1[59]= -1
    C2[0]= 0
    C2[1]= 0
    C2[2]= 0
    C2[3]= 0
    C2[4]= 0
    C2[5]= 0
    C2[6]= 0
    C2[7]= 0
    C2[8]= 0
    C2[9]= 0
    C2[10]= 0
    C2[11]= -0.587785252292473
    C2[12]= -0.951056516295154
    C2[13]= -0.951056516295154
    C2[14]= -0.587785252292473
    C2[15]= -1.22514845490862E-16
    C2[16]= 0.587785252292473
    C2[17]= 0.951056516295154
    C2[18]= 0.951056516295154
    C2[19]= 0.587785252292474
    C2[20]= 0
    C2[21]= -0.951056516295154
    C2[22]= -0.587785252292473
    C2[23]= 0.587785252292473
    C2[24]= 0.951056516295154
    C2[25]= 2.45029690981724E-16
    C2[26]= -0.951056516295153
    C2[27]= -0.587785252292473
    C2[28]= 0.587785252292473
    C2[29]= 0.951056516295154
    C2[30]= 0
    C2[31]= -0.951056516295154
    C2[32]= 0.587785252292473
    C2[33]= 0.587785252292474
    C2[34]= -0.951056516295154
    C2[35]= -3.67544536472586E-16
    C2[36]= 0.951056516295154
    C2[37]= -0.587785252292473
    C2[38]= -0.587785252292474
    C2[39]= 0.951056516295153
    C2[40]= 0
    C2[41]= -0.587785252292473
    C2[42]= 0.951056516295154
    C2[43]= -0.951056516295153
    C2[44]= 0.587785252292473
    C2[45]= 4.90059381963448E-16
    C2[46]= -0.587785252292475
    C2[47]= 0.951056516295154
    C2[48]= -0.951056516295153
    C2[49]= 0.58778525229247
    C2[50]= 0
    C2[51]= 3.21574364359201E-16
    C2[52]= -6.43148728718401E-16
    C2[53]= -3.67544536472586E-16
    C2[54]= -1.2862974574368E-15
    C2[55]= -6.1257422745431E-16
    C2[56]= 7.35089072945172E-16
    C2[57]= -8.57603918436034E-16
    C2[58]= -2.5725949148736E-15
    C2[59]= -1.10263360941776E-15
    B[0] = 0.1           '1/10
    B[1] = 0.2            '2/10
    B[2] = 0.2            '2/10
    B[3] = 0.2            '2/10
    B[4] = 0.2           '2/10
    B[5] = 0.1           '1/10
end sub

'************************************************************
 sub procedure blue(dim x as integer)
     Expander_Write_PortAB(x,0x4912)
 end sub
  sub procedure red(dim x as integer)
     Expander_Write_PortAB(x,0x9224)
 end sub
  sub procedure green(dim x as integer)
     Expander_Write_PortAB(x,0x2449)
 end sub
 sub procedure yellow(dim x as integer)
     Expander_Write_PortAB(x,0xB66D)
 end sub
 sub procedure mangenta(dim x as integer)
     Expander_Write_PortAB(x,0xDB36)
 end sub
 sub procedure white(dim x as integer)
     Expander_Write_PortAB(x,0xFF7F)
 end sub
 sub procedure cian(dim x as integer)
      Expander_Write_PortAB(x,0x6D5B)
 end sub
'************************************************************

sub procedure gumbi
'*****************občutljivost v plus

              IF Button(PORTB, 7, 1, 1) then oldstateA = 255 end if
              IF oldstateA and Button(PORTB, 7, 1, 0) then
                vol = vol + 10
                oldstateA = 0
              end IF
'*****************občutljivost v minus
             IF Button(PORTB, 6, 1, 1) then oldstateB = 255 end if
             IF oldstateB and Button(PORTB, 6, 1, 0) then
                          IF vol > 0 THEN vol = vol - 10 END IF
                oldstateB = 0
             end IF
'*************************
'*****************Programi gor
              IF Button(PORTE, 2, 1, 1) then oldstateC = 255 end if
              IF oldstateC and Button(PORTE, 2, 1, 0) then
                prog = prog + 1
                oldstateC = 0
              end IF
'*****************Programi dol
             IF Button(PORTE, 1, 1, 1) then oldstateD = 255 end if
             IF oldstateD and Button(PORTE, 1, 1, 0) then
                          IF prog > 0 THEN prog = prog - 1 END IF
                oldstateD = 0
             end IF
'*************************
                          if  volold  <> vol then
                          volold = vol
                          ByteToStr(vol,txt1)
                          Lcd_Out(2,1,"Stopnja: ")
                          Lcd_Out(2,14,txt1)
                          end if
end sub

sub procedure LightShow
    portD.0 = not portD.0
    'if portd.1 = 0 then
    'Expander_Write_PortAB(0,0x0000)
    'else
    'Expander_Write_PortAB(0,0xFFFF)
    'end if
    'update = false
    NE = 10     'NE
       For N1 = 0 To NE / 2
           SpreR1[N1][0] = 0
           SpreR1[N1][1] = 0
           For Z1 = 0 To NE - 1
           AG[0] = ADC_Read(1)
               k1 = AG[0] * B[N1] * c1[(Z1+(N1*10))]
               p1 = AG[0] * B[N1] * c2[(Z1+(N1*10))]
               SpreR1[N1][0] = SpreR1[N1][0] + k1
               SpreR1[N1][1] = SpreR1[N1][1] + p1
           Next Z1
         SW[N1] = SqrT(POW(SpreR1[N1][0],2) + POW(SpreR1[N1][1],2))
        Next N1

    ' For N1 = 0 To NE / 2
    ' Next N1

     ' if SW[0] > (vol+300) then portd.2 = 1 else  portd.2 = 0 end if
     ' if SW[1] > (vol+60)  then portd.3 = 1 else  portd.3 = 0 end if
     ' if SW[2] > (vol+40)  then portd.4 = 1 else  portd.4 = 0 end if
     ' if SW[3] > (vol+30)  then portd.5 = 1 else  portd.5 = 0 end if
      if SW[4] > (vol+20)  then portd.6 = 1 else  portd.6 = 0 end if
      if SW[5] > vol  then portd.7 = 1 else  portd.7 = 0 end if
     ' if SW[0] > (vol+160) then Expander_Write_PortAB(0,0x4912) else  Expander_Write_PortAB(0,0x0000) end if
     ' if SW[1] > (vol+60)  then Expander_Write_PortAB(1,0x9224) else  Expander_Write_PortAB(1,0x0000) end if
     ' if SW[2] > (vol+40)  then Expander_Write_PortAB(2,0x2449) else  Expander_Write_PortAB(2,0x0000) end if
     ' if SW[3] > (vol+30)  then Expander_Write_PortAB(3,0xB66D) else  Expander_Write_PortAB(3,0x0000) end if
      if SW[4] > (vol+20)  then Expander_Write_PortAB(4,0x4912) else  Expander_Write_PortAB(4,0x0000) end if
      if SW[5] > (vol+10)  then Expander_Write_PortAB(7,0x9224) else Expander_Write_PortAB(7,0x0000) end if
      gumbi
end sub

sub procedure Initialize
   ADCON1 = 0xB4                 ' Configure AN0 and 1 pin as analog
   TRISA  = 0xFF                 ' PORTA is input
   ADC_Init()
   Lcd_Init()
   Lcd_Cmd(_LCD_CLEAR)            ' Clear display
   Lcd_Cmd(_LCD_CURSOR_OFF)       ' Cursor off
   Spi1_Init()
   Expander_Init(0)
   Expander_Init(1)
   Expander_Init(2)
   Expander_Init(3)
   Expander_Init(4)
   Expander_Init(5)
   Expander_Set_DirectionPortAB(0, 0x0000)             ' set expander's portAB to be output
   Expander_Set_DirectionPortAB(1, 0x0000)             ' set expander's portAB to be output
   Expander_Set_DirectionPortAB(2, 0x0000)             ' set expander's portAB to be output
   Expander_Set_DirectionPortAB(3, 0x0000)             ' set expander's portAB to be output
   Expander_Set_DirectionPortAB(4, 0x0000)             ' set expander's portAB to be output
   Expander_Set_DirectionPortAB(5, 0x0000)             ' set expander's portAB to be output
   TRISE  = 0xFF
   PORTE  = 0
   TRISB.6 = 1
   TRISB.7 = 1
   PORTB.6 = 0
   PORTB.7 = 0
   TRISD = 0x00
   PORTD = 0
   spremenljivke
   vol = 100
   prog = 0
   prog1 = 1
end sub

main:
   Initialize
   While true                             ' loop forever
         gumbi                            'preveri če ej kak gumb pritisnjen
'********************************
         select case prog              'preveri preverjeni program
          case 0
            LightShow
          case else
            portD.0 = 0
         end select
'********************************
'********************************
         if prog <> prog1 then    'zamenjaj napis
            prog1 = prog
                  select case prog
                         case 0
                         Lcd_Out(1,1,"PR0 - LightShow")                          ' yes...then output
                         case else
                         Lcd_Out(1,1,"PR1 - xxxxxxxxx")
                         portD.0 = 0
                  end select
         end if
   Wend

end.

Post Reply

Return to “mikroBasic PRO for PIC General”