Zitat Zitat von ACsenf
Bleibe bei dem hier schonmal erwähnten...daß Bascom da einen Bug hat, lasse mich aber gerne eines Besseren belehren.
Den Fehler der nichtangenommenen Veränderung des Prescalers gab es tatsächlich mal bei einer Version, ist aber schon länger behoben.

Das dies stimmt, kann man leich anhand dieses völlig fehlerfrei funktionierenden Testprogrammes überprüfen (habe jetzt nicht forenfreundlich gekürzt):

Code:
'Frequenzgenerator Atmega 8/168 oder M32 mit Menue - HR -  2006
'#######################################################

'$regfile = "m8def.dat"
'$regfile = "m168def.dat"
$regfile = "m32def.dat"
$crystal = 7372800
'$crystal = 14745000
'$crystal = 16000000

$baud = 9600

$hwstack = 64
$swstack = 64
$framesize = 64



'$lib "i2c_twi.lbx"                           'Hardware I2C!

'Config Lcdpin = Pin , Db4 = Pd.4 , Db5 = Pd.5 , Db6 = Pd.6 , Db7 = Pd.7 , _
'E = Pd.3 , Rs = Pd.2
'
'Config Scl = Portc.5           'für Mega8 und 168
'Config Sda = Portc.4

'Config Scl = Portd.0           'für Mega16 und 32
'Config Sda = Portd.1

'-------------------------------------------------------------------------------
$lib "Lcd_i2c.lib"                           'i2c Treiber für das LCD
Dim _lcd_e As Byte                           'Nötig für die Enable Auswahl
'Const Pcf8574_lcd = 112                      'Adresse des LCD-PCF
'Const Pcf8574_lcd = 64
Waitms 200
_lcd_e = 128                                 'Für LCDs mit 1 Controller

'Config Scl = Portc.5                         'für Mega8 und 168
'Config Sda = Portc.4
Config Scl = Portc.0                         'für Mega16 und 32
Config Sda = Portc.1


I2cinit                                      'I2C Initialisierung
'-------------------------------------------------------------------------------
Config Lcd = 16 * 2
Config Adc = Single , Prescaler = Auto       'Für Analogtastatur


'Config Portd.4 = Output                      'Test LED-Port

'Config Pina.0 = Input                        'Für analog_key
'Keyboard Alias Pina.0                        'Aliasname
'Porta.0 = 1                                  'Pullup Widerstand ein

'-------------------------------------------------------------------------------
Const Pcf8591 = 144                          'DA-AD-Wandler auf Basis
Const Eeprom2 = 166                          'Eeprom2 auf Basis
Const Eeprom1 = 174                          'Eeprom1 oder Ram auf Basis
Const Eeprom0 = 160                          'externes Chip Card Eeprom0
Const Pcf8574_1 = 64                         'Portext. auf Basis
Const Tda8444 = 78                           'DA-Wandler auf Extension-Platine
Const Pcf8574_lcd = 112                      'Portextension für LCD
Const Pcf8583_rtc = 162                      'RTC auf Extension-Platine

Const Max520 = 94                            'für MAX520 (extern)
Const Keyboard = 0                           'ADC-Nr.

Config Timer1 = Timer , Prescale = 8
Stop Timer1
On Timer1 Timer_irq Nosave

Enable Timer1
Enable Interrupts                            'Interrupts erlauben



Config Adc = Single , Prescaler = Auto
Config Pinb.1 = Output



Config Pinb.4 = Input                        'Für Frequenzzählung
Portb.4 = 1                                  'Pullup Widerstand ein
Flow_in Alias Pinb.4

Config Pinc.2 = Output                       'Ausgang  für beeper
Beep Alias Portc.2

Config Pinb.0 = Output                       'Ausgang  für Frequenz

Declare Sub Beep_alarm()
Declare Sub Beep_confirm()
Declare Sub Analog_key_wait()                'Resultat in Taste

Declare Function Analog_key() As Byte
Declare Function Adjust_value_
(byval Up_down As Word , Byval Schritt As Byte)as Word

Declare Function Timer_test() As Word

Dim Timervorgabe As Word
'Timervorgabe = 19456                         'für 20Hz bzw 10 Hz an PB0
'Timervorgabe = 34816                         'für 30Hz bzw 15 Hz an PB0
Timervorgabe = 56320                         'für 100Hz bzw 50 Hz an PB0

Dim Value As Word At &H100
Dim Value_low As Byte At &H100 Overlay
Dim Value_high As Byte At &H101 Overlay
Dim Address As Word At &H102
Dim Address_low As Byte At &H102 Overlay
Dim Address_high As Byte At &H103 Overlay


Dim I As Integer
Dim N As Integer
Dim Hertz_word As Word
Dim Count As Byte
Dim Temp As Word
Dim Schritt As Byte

Dim Command As Byte
Dim Taste As Byte
Dim Counter_ds As Word


Dim Menue_current As Byte
Dim Wert As Byte

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

Call Beep_alarm()                            'Bin wach -)
Cls
Cursor Off
Lcd "  FREQUENZGEN."
'Locate 2 , 1
'Lcd " MEGA8 - Control"
Wait 3
Start Timer1
'------------------------------------------------------------------------------
Beep_confirm
Cls
Menue:
Menue_current = 1

Menue_1:

Taste = Analog_key()
Waitms 100

If Taste = 32 Then
Menue_current = Menue_current + 1
End If

If Taste = 33 Then
Menue_current = Menue_current - 1
End If

If Menue_current > 8 Then Menue_current = 1
If Menue_current < 1 Then Menue_current = 8

If Taste = 10 Then Goto Execute
'-------------------------------------------------------------------------------
Select Case Menue_current                    'aktuelle Auswahl anzeigen

   Case 1
    Locate 1 , 1
    Lcd " 10 Hz          "
   Case 2
    Locate 1 , 1
    Lcd " 50 Hz          "
   Case 3
    Locate 1 , 1
    Lcd " 100 Hz         "
   Case 4
    Locate 1 , 1
    Lcd " 1 kHz          "
   Case 5
    Locate 1 , 1
    Lcd " 10 kHz         "
   Case 6
    Locate 1 , 1
    Lcd " 20 kHz         "
   Case 7
    Locate 1 , 1
    Lcd " 50 kHz         "
   Case 8
    Locate 1 , 1
    Lcd " FREE           "



End Select
    Taste = 255
Goto Menue_1

'-------------------------------------------------------------------------------
Execute:                                     'aktuellen Menuepunkt ausführen
Cls

Select Case Menue_current
   Case 1
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 8
          Timervorgabe = 19456               'für 20 bzw. 10 Hz
          Enable Timer1

   Case 2
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 8
          Timervorgabe = 56320               'für 100 bzw. 50 Hz
          Enable Timer1
          Cls
   Case 3
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 1
          Timervorgabe = 28672               'für 200 bzw. 100 Hz
          Enable Timer1
          Cls
   Case 4
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 1
          Timervorgabe = 61850 + 32          'für 2000 bzw. 1000 Hz
          Enable Timer1
          Cls
   Case 5
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 1
          Timervorgabe = 65167 + 32          'für 20000 bzw. 10000 Hz
          Enable Timer1
          Cls
   Case 6
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 1
          Timervorgabe = 65352 + 32          'für 40000 bzw. 20000 Hz
          Enable Timer1
          Cls
   Case 7
          Cls
          Disable Timer1
          Config Timer1 = Timer , Prescale = 1
          Timervorgabe = 65462 + 32          'für 100kHz bzw. 50kHz  bei Toggle
          Enable Timer1
          Cls
  Case 8
          Cls                                'zur Ermittlung der maximal
          Locate 2 , 2                       'erziebaren Frequenz
          Lcd " free running"
          Disable Timer1
          Do
          Set Portb.0
          nop
          nop
          Reset Portb.0
          nop
          nop
          Loop
          Enable Timer1



End Select

Goto Menue_1
'-------------------------------------------------------------------------------

End                                          'Hauptprogramm Ende

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


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


Sub Beep_alarm()
          Sound Beep , 200 , 450
          Sound Beep , 200 , 250
End Sub
'-------------------------------------------------------------------------------

Sub Beep_confirm()
          Sound Beep , 200 , 450
End Sub
'-------------------------------------------------------------------------------

Sub Analog_key_wait()
Wait_ana_key:
Taste = Analog_key()
If Taste = 255 Then Goto Wait_ana_key
End Sub

'------------------------------------------------------------------------------
'Diese Unterfunktion fragt die Tastatur am analogen Port ab
Function Analog_key() As Byte
   Local Ws As Word
   Analog_key = 255
   Start Adc
   Ws = Getadc(keyboard)
   If Ws < 900 Then
      Select Case Ws
         Case 0 To 100
            Analog_key = 32
         Case 400 To 600
            Analog_key = 33
         Case 650 To 850
            Analog_key = 10
      End Select
      Call Beep_confirm()
   End If
Waitms 100
End Function
'-------------------------------------------------------------------------------

Function Adjust_value(byval Up_down As Word , Byval Schritt As Byte)

Cls

Adj1:
Locate 2 , 1
Lcd Up_down ; "  "
Taste = Analog_key()
If Taste = 32 Then Up_down = Up_down + Schritt
If Taste = 33 Then Up_down = Up_down - Schritt
If Taste = 10 Then
   Adjust_value = Up_down
   Exit Sub
End If

Goto Adj1

End Function
'-------------------------------------------------------------------------------
Timer_irq:

$asm
push R24
push R25
push R26
push R27
push XH
push XL
in R24, SREG
push R24
$end Asm
Timer1 = Timervorgabe
Toggle Portb.0
$asm
pop R24
Out Sreg , R24
pop XL
pop XH
pop R27
pop R26
pop R25
pop R24
$end Asm


Return
'-------------------------------------------------------------------------------
Das Beispiel ist mit Absicht "Klein Fritzchen mäßig" strukturiert.

Grüße
Henrik