'GenAkustLed

Bascom

Generator tonów akustycznych z linijką diodową.

W czasie emisji tonów, generator zapala kolejno 13 diod Led, które świecą jeszcze chwilę po emisji.
Dioda w Portd.1 miga w takt pojawiających się tonów.
Dźwięki nie stykają się z sobą, pomiędzy nimi są krótkie przerwy.



'GenAkustLed.bas

 'Generator tonów akustycznych, przełączanych krokami pętli For.

 'Zastosowano Select Case i Declare  Sub.

 'Tutaj 13 tonów z krokiem Waitms XX.

 '13 Portów wyjściowych, to kolejno zapalające się Led.

 'Wyjście częstotliwości z Portd.3..

 'Opis pod programem.

 'Opracował Henryk Wydmuch

 'Fusebit ustawiony na generator RC wewnętrzny 0100 8MHz.

 'Program zajmuje 34% pamięci.

 

 $regfile = "m8def.dat"                                     'Info dla kompilatora.

$crystal = 8000000                                          'Informujemy kompilator o frekwencji 8MHz.

 

 Config Lcd = 16 * 2                                        'Konfiguracja Lcd.

 Config Lcdpin = Pin , Db4 = Portc.3 , Db5 = Portc.2 , Db6 = Portc.1 , Db7 = Portc.0 , E = Portc.4 , Rs = Portc.5

 

 Config Timer0 = Timer , Prescale = 1                       'Timer0 jako miernik czasu, podział przez 1.

 

  Config Pind.1 = Output                                    'Port wykonawczy i dla Led.

  Set Portd.1                                               'Ustaw poziom wysoki Portd.1.

  Config Pind.6 = Input                                     'Przycisk startu.

  Set Portd.6                                               '

  Config Pind.3 = Output                                    'Wyjście generatora.

  Set Portd.3                                               '

 

    Config Pind.0 = Output                                  '

    Config Pind.2 = Output                                  '

    Config Pind.4 = Output                                  '

    Config Pind.5 = Output                                  '

    Config Pind.7 = Output                                  '

    Config Pinb.6 = Output                                  '

    Config Pinb.7 = Output                                  '

    Config Pinb.0 = Output                                  '

    Config Pinb.1 = Output                                  '

    Config Pinb.2 = Output                                  '

    Config Pinb.3 = Output                                  '

    Config Pinb.4 = Output                                  '

    Config Pinb.5 = Output                                  '

       Set Portd.0                                          '

       Set Portd.2                                          '

       Set Portd.4                                          '

       Set Portd.5                                          '

       Set Portd.7                                          '

       Set Portb.6                                          '

       Set Portb.7                                          '

       Set Portb.0                                          '

       Set Portb.1                                          '

       Set Portb.2                                          '

       Set Portb.3                                          '

       Set Portb.4                                          '

       Set Portb.5                                          '

 

  On Timer0 Licz                                            'Podprogram Timer0.

 

  Dim Bt As Byte                                            'Deklaracje zmiennych.

  Dim Odp As Single                                         '

  Dim Zm As Byte                                            '

  Dim Wp As Byte                                            '

 

  Declare Sub Plcd                                          'Deklaracja Sub.

 

  Enable Interrupts                                         '

 Stop Timer0                                                '

 Cls                                                        'Wyczyść Lcd.

 

 '------------------------------------------------------------------------------------------------------

Do                                                          'Początek pętli głównej programu.

 

If Pind.6 = 0 Then                                          'Jeśli w Portd.6 pojawi się 0...

Enable Timer0                                               'Odblokuj Timer0.

 

 For Bt = 1 To 14                                           'Pętla For.

  Waitms 100                                                'Szybkość kroków tonów.

Start Timer0                                                '

 Select Case Bt                                             'Przyglądaj się zmiennej Bt.

 Cursor Off                                                 '

 

 Case 1 :                                                   'Jeśli Bt w pętli For ma 1...

   Wp = 21                                                  'To Wp będzie 21.

 Reset Portd.0                                              'Led.

 Call Plcd                                                  'Wywołaj podprogram.

 Locate 2 , 10                                              '

 Lcd "Portd.0"                                              '

 Toggle Portd.1                                             '

 

 Case 2 :                                                   '

   Wp = 33                                                  '

 Reset Portd.2                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portd.2"                                              '

 Toggle Portd.1                                             '

 

 Case 3 :                                                   '

   Wp = 19                                                  '

 Reset Portd.4                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portd.4"                                              '

 Toggle Portd.1                                             '

 

 Case 4 :                                                   '

   Wp = 13                                                  '

 Reset Portd.5                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portd.5"                                              '

 Toggle Portd.1                                             '

 

 Case 5 :                                                   '

   Wp = 26                                                  '

 Reset Portd.7                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portd.7"                                              '

 Toggle Portd.1                                             '

 

 Case 6 :                                                   '

   Wp = 22                                                  '

 Reset Portb.6                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portd.6"                                              '

 Toggle Portd.1                                             '

 

 Case 7 :                                                   '

   Wp = 17                                                  '

 Reset Portb.7                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.7"                                              '

 Toggle Portd.1                                             '

 

 Case 8 :                                                   '

   Wp = 26                                                  '

 Reset Portb.0                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.0"                                              '

 Toggle Portd.1                                             '

 

 Case 9 :                                                   '

   Wp = 15                                                  '

 Reset Portb.1                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.1"                                              '

 Toggle Portd.1                                             '

 

 Case 10:                                                   '

   Wp = 33                                                  '

 Reset Portb.2                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.2"                                              '

 Toggle Portd.1                                             '

 

 Case 11 :                                                  '

   Wp = 42                                                  '

 Reset Portb.3                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.3"                                              '

 Toggle Portd.1                                             '

 

 Case 12 :                                                  '

   Wp = 30                                                  '

 Reset Portb.4                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.4"                                              '

 Toggle Portd.1                                             '

 

 Case 13 :                                                  '

   Wp = 41                                                  '

 Reset Portb.5                                              '

 Call Plcd                                                  '

 Locate 2 , 10                                              '

 Lcd "Portb.5"                                              '

 Set Portd.6                                                'Wyłącz Port (przycisk P1 - nie będzie powtórzeń melodyjki).

 Toggle Portd.1                                             '

 

 Waitms 60                                                  'Czas trwania Case13, 60.

  Disable Timer0                                            'Wyłącz przerwanie.

 

  Waitms 500                                                'Czekaj 500mS.

       Set Portd.0                                          'Wyłącz wszystkie diody Led.

       Set Portd.2                                          '

       Set Portd.4                                          '

       Set Portd.5                                          '

       Set Portd.7                                          '

       Set Portb.6                                          '

       Set Portb.7                                          '

       Set Portb.0                                          '

       Set Portb.1                                          '

       Set Portb.2                                          '

       Set Portb.3                                          '

       Set Portb.4                                          '

       Set Portb.5                                          '

       Set Portd.1                                          '

  Exit For

  End Select                                                'Koniec selekcji.

  Stop Timer0

  Next Bt                                                   'Koniec For.

 

End If                                                      'Koniec warunku, gdy Pind.6=0 .

Loop                                                        '

End                                                         '

'--------------------------------------------------------------------------------------------------------

Licz:                                                       'Podprogram Timer0.

 Incr Zm                                                    'Przy każdym przepełnieniu Timer0 powiększaj Zm.

 If Zm = Wp Then                                            'Jeśli liczba przepełnień wyniesie tyle co zmienna Az (czyli Wp)...

 Toggle Portd.3                                             'Przełącz Portd.3 (który jest wyjściem częstotliwości Timer0).

 Zm = 0                                                     '

 End If                                                     '

 Return                                                     'Wróć do programu.

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

Sub Plcd                                                    'Deklaracja podprogramu Plcd.

Home                                                        '

 Lcd Wp                                                     'Pokazuj Wp, Wp jest to samo, co zmienna Az.

 Odp = 15.625 / Wp                                          'Taka będzie częstotliwość.

 Locate 2 , 1                                               '

 Lcd Odp ;                                                  'Pokaż wartość (częstotliwość generatora) zmiennej Odp.

 Locate 2 , 5                                               '

 Lcd " KHz      "                                           '

End Sub                                                     '

'--------------------------------------------------------------------------------------------------------

'Objaśnienie:

'Timer0 jest generatorem.

'Timer0 przepełnia się po 256 impulsach.

'8MHz/256=31250.

'1sek/31250=0,000032. Jedno przerwanie, to czas 32uS.

'32uS x 2 = 64uS, to jeden okres.

'64uS, to 15,625KHz.

'Jeśli zmienna Wp=1, to częstotliwość generatora=15,625KHz.

 

'Generator emituje 13 dźwięków zależnych od wpisanych wartości do Wp.

'Szybkość kroków, to Waitms 100.

'Trzynaście wolnych Portów zaangażowano do zaświecania Led.

'Ledy zapalają się kolejno, a następnie gasną i układ czeka ponownie na naciśnięcie P1.

'Z Portu D3 emitowany jest dźwięk. Każdy z tonów oddzielony jest krótkim czasem przerwy.

'Koniec.