'GenAkustSelect

Bascom

Generator tonów akustycznych.
To rozwiązanie generatora zawiera w swoim programie Select Case oraz deklarację Sub.
Układ pamięta oczywiście wpisane dane o wysokościach tonów i szybkości przełączania.



'GenAkustSelect.bas

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

 'Zastosowano Select Case i Declare  Sub.

 'Tutaj 20 tonów z krokiem Waitms XX.

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

 'Opis pod programem.

 'Opracował Henryk Wydmuch

 'Program zajmuje 27% pamięci.

 

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

$crystal = 10240000                                         'Informujemy kompilator o kwarcu 10,24MHz.

 

 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                                               '

 

  On Timer0 Licz                                            'Podprogram Timer0.

 

  Dim Bt As Byte                                            'Deklaracje.

  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 20                                           'Pętla For.

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

 

 Select Case Bt

 

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

   Wp = 17                                                  'To Wp będzie 17.

 Call Plcd                                                  'Wywołaj podprogram.

 

 Case 2 :                                                   '

   Wp = 33                                                  '

 Call Plcd                                                  '

 

 Case 3 :                                                   '

   Wp = 19                                                  '

 Call Plcd                                                  '

 

 Case 4 :                                                   '

   Wp = 13                                                  '

 Call Plcd                                                  '

 

 Case 5 :                                                   '

   Wp = 26                                                  '

 Call Plcd                                                  '

 

 Case 6 :                                                   '

   Wp = 22                                                  '

 Call Plcd                                                  '

 

 Case 7 :                                                   '

   Wp = 17                                                  '

 Call Plcd                                                  '

 

 Case 8 :                                                   '

   Wp = 26                                                  '

 Call Plcd                                                  '

 

 Case 9 :                                                   '

   Wp = 11                                                  '

 Call Plcd                                                  '

 

 Case 10:                                                   '

   Wp = 33                                                  '

 Call Plcd                                                  '

 

 Case 11 :                                                  '

   Wp = 39                                                  '

 Call Plcd                                                  '

 

 Case 12 :                                                  '

   Wp = 30                                                  '

 Call Plcd                                                  '

 

 Case 13 :                                                  '

   Wp = 41                                                  '

 Call Plcd                                                  '

 

 Case 14 :                                                  '

   Wp = 45                                                  '

 Call Plcd                                                  '

 

 Case 15 :

   Wp = 17                                                  '

 Call Plcd                                                  '

 

 Case 16 :                                                  '

   Wp = 48                                                  '

 Call Plcd                                                  '

 

 Case 17 :                                                  '

   Wp = 22                                                  '

 Call Plcd                                                  '

 

 Case 18 :                                                  '

   Wp = 33                                                  '

 Call Plcd                                                  '

 

 Case 19 :                                                  '

   Wp = 44                                                  '

 Call Plcd                                                  '

 

 Case 20:                                                   '

   Wp = 25                                                  '

 Call Plcd                                                  '

 Waitms 60                                                  'Czas trwania Case 20.

 

  Stop Timer0

  End Select                                                'Koniec selekcji.

 

  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                                                        '

 Start Timer0                                               '

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

 Odp = 20 / Wp                                              'Zrób pełny okres przebiegu generatora.

 Locate 2 , 1                                               '

 Lcd Odp ;                                                  'Pokaż wartość zmiennej Odp.

 Locate 2 , 5                                               '

 Lcd " KHz      "                                           '

End Sub                                                     '

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

'Objaśnienie:

'Timer0 jest generatorem częstotliwości 20KHz (gdy Wp=1).

'Jeśli Wp ma inną wartość, generator ma inną częstotliwość.

'Inne wartości Wp uzyskujemy wpisując je do pętli Select Case.

'Kolejność wybierania tych Wp dokonywana jest pętlą For kroczącą od 1 do 20.

'Za każdym razem jest skok do Sub Plcd celem obsługi wyświetlacza Lcd.

'Koniec.