- Labornetzteil AliExpress         
Ergebnis 1 bis 3 von 3

Thema: Problem mit Programm

  1. #1
    Benutzer Stammmitglied
    Registriert seit
    08.07.2008
    Beiträge
    49

    Problem mit Programm

    Anzeige

    E-Bike
    Hallo

    Also ich hab eine kleines Programm für einen Atmega 8 geschrieben welches LEDs ansteuert.
    Es werden genau 7 Ausgänge angesteuert. Jeder Ausgang kann über einen Software PWM gesteuert werden. Es sollen Lauflichter entestehen die Geschwindigkeit dieser Lauflichter kann auch eingestellt werden. Beides geschieht über ein Poti. Es werden dazu 2 Timer benutzt. Die Einstellungen sollen Spannungsfrei gespeichert werden.

    Nun zum Problem. Leider stürzt das Programm immer mal wieder ab. Ich weiß nicht warum und es gibt auch keine Regelmäßigkeit. Was aber bis jetzt immer so war ist wenn der µC längere Zeit nicht eingeschaltet ist und dann wieder eingeschaltet wird stürzt das Programm immer ab. Es kann dann durch ein bischen hin und her stellen zum laufen gebracht werden. Das war aber eigentlich nicht so gedacht.... Kann mir jemand helfen?

    Danke für die Hilfe.

    lg dämmi

    Das Programm:

    Code:
    
    '-----------------------------Registrierung-------------------------------------
    
    $regfile = "m8def.dat"                                      'Konfiguration auf ATmega 8
    
    $crystal = 16000000                                         'Quarztakt 16MHz
    
    '-----------------------------Configuration-------------------------------------
    
    'Config Porta = Input
    Config Portb = Output                                       'Led Ausgang
    Config Portd.7 = Output                                     'Led Ausgang
    Config Portd.0 = Input                                      'Schalter Eingang
    Config Portd.1 = Input                                      'Schalter Eingang
    Config Portd.2 = Input                                      'Schalter Eingang
    Config Portd.3 = Input                                      'Schalter Eingang
    Config Portd.4 = Input                                      'Schalter Eingang
    Config Portd.5 = Input                                      'Schalter Eingang
    Config Portd.6 = Input                                      'Schalter Eingang
    
    Config Adc = Single , Prescaler = Auto , Reference = Internal       'ADC Konfiguaration                                                                     'mit interner reference
                                                                         'Spannung
    Start Adc
    
    Config Timer0 = Timer , Prescale = 1                        'Konfiguriere Timer0
    Config Timer2 = Timer , Prescale = 8
    Enable Timer2
    Enable Timer0                                               'schalte den Timer0 ein
    On Timer0 Routtimer0
    On Timer2 Routtimer2                                        'verzweige bei Timer0 überlauf zu Isr_von_Timer0
    Enable Interrupts
    
    '-----------------------------Dimensionierung-----------------------------------
    
    Tast1 Alias Pind.0
    Tast2 Alias Pind.1
    Tast3 Alias Pind.2
    Tast4 Alias Pind.3
    Tast5 Alias Pind.4
    Tast6 Alias Pind.5
    Tast7 Alias Pind.6
    
    Leds Alias Portb
    Led1 Alias Portb.0
    Led2 Alias Portb.1
    Led3 Alias Portb.2
    Led4 Alias Portb.3
    Led5 Alias Portb.4
    Led6 Alias Portb.5
    Led7 Alias Portd.7
    Dim Adcwert As Word
    Dim Adcwertalt As Word
    Dim Zaehler As Word
    Dim Zaehler2 As Word
    Dim Pwm As Word
    Dim Speed As Word
    Dim Schritt As Word
    Dim Aus As Bit
    Dim Status As Byte
    Dim Staled1 As Bit
    Dim Staled2 As Bit
    Dim Staled3 As Bit
    Dim Staled4 As Bit
    Dim Staled5 As Bit
    Dim Staled6 As Bit
    Dim Staled7 As Bit
    Dim Epwm As Eram Word
    Dim Espeed As Eram Word
    Dim Tast1stat As Bit
    Dim Adcstat As Bit
    
    '-----------------------------Vorkonfiguration----------------------------------
    
    Portb = &B00000000
    Portd.7 = 0
    Adcwert = 0
    Adcwertalt = 0
    Zaehler = 0
    Zaehler2 = 0
    Schritt = 0
    Aus = 0
    Status = 0
    Timer0 = 0
    Timer2 = 0
    Adcstat = 0
    Pwm = Epwm
    Speed = Espeed
    Tast1stat = Tast1
    
    '----------------------------Hauptprogramm--------------------------------------
    
    Do
    
       If Tast1stat <> Tast1 Or Adcstat = 1 Then                'einlesen des Poti Werts
    
          Adcstat = 1
          Adcwert = Getadc(0)
    
          If Adcwert <> Adcwertaltthen
    
             Adcwertalt = Adcwert
    
             If Tast1 = 0 Then                                  'abspeichern des Wertes als PWM Wert
                Pwm = Adcwertalt
             End If
    
             If Tast1 = 1 Then                                  'abspeichern des Wertes als Speed Wert
    
                Speed = 1400 - Adcwertalt
    
             End If
    
          End If
    
       End If
    
    
       If Tast7 = 1 And Tast3 = 0 And Tast4 = 0 And Tast5 = 0 And Tast6 = 0 And Tast2 = 0 Then
    
          If Status <> 1 Then
                Schritt = 0                                     'Programm 1
                Status = 1
          End If
    
          If Schritt = 1 Then
                Staled1 = 1
                Staled2 = 1
                Staled3 = 1
                Staled4 = 1
                Staled5 = 1
                Staled6 = 1
                Staled7 = 1
                Led1 = 1
                Led2 = 1
                Led3 = 1
                Led4 = 1
                Led5 = 1
                Led6 = 1
                Led7 = 1
          End If
    
          If Schritt = 2 Then
                Staled1 = 0
                Staled2 = 0
                Staled3 = 0
                Staled4 = 0
                Staled5 = 0
                Staled6 = 0
                Staled7 = 0
                Led1 = 0
                Led2 = 0
                Led3 = 0
                Led4 = 0
                Led5 = 0
                Led6 = 0
                Led7 = 0
                Schritt = 0
          End If
    
       End If
    
       If Tast3 = 1 And Tast2 = 0 And Tast4 = 0 And Tast5 = 0 And Tast6 = 0 And Tast7 = 0 Then
    
          If Status <> 2 Then
                Schritt = 0                                     'Programm 2
                Status = 2
          End If
    
          If Schritt = 1 Or Schritt = 15 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
             If Schritt = 15 Then Schritt = 0
          End If
    
          If Schritt = 2 Or Schritt = 14 Then
             Staled1 = 1
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 3 Or Schritt = 13 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 4 Or Schritt = 12 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 5 Or Schritt = 11 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 6 Or Schritt = 10 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 7 Or Schritt = 9 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 0
          End If
    
          If Schritt = 8 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
       End If
    
       If Tast4 = 1 And Tast2 = 0 And Tast3 = 0 And Tast5 = 0 And Tast6 = 0 And Tast7 = 0 Then
    
          If Status <> 3 Then                                   'Programm 3
                Schritt = 0
                Status = 3
          End If
    
          If Schritt = 1 Or Schritt = 11 Or Schritt = 21 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
             If Schritt = 21 Then Schritt = 0
          End If
    
          If Schritt = 2 Or Schritt = 20 Then
             Staled1 = 1
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 3 Or Schritt = 19 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 4 Or Schritt = 18 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 5 Or Schritt = 17 Then
             Staled1 = 0
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 0
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 6 Or Schritt = 16 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 0
             Staled7 = 0
             Led1 = 0
             Led2 = 0
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 7 Or Schritt = 15 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 0
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 0
          End If
    
          If Schritt = 8 Or Schritt = 14 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 9 Or Schritt = 13 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 10 Or Schritt = 12 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 1
          End If
    
       End If
    
       If Tast6 = 1 And Tast2 = 0 And Tast3 = 0 And Tast4 = 0 And Tast5 = 0 And Tast7 = 0 Then
    
          If Status <> 5 Then
                Schritt = 0                                     'Programm 4
                Status = 5
          End If
    
          If Schritt = 1 Then
             Staled1 = 1
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 1
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 1
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 2 Then
             Staled1 = 0
             Staled2 = 1
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 1
             Staled7 = 0
             Led1 = 0
             Led2 = 1
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 1
             Led7 = 0
          End If
    
          If Schritt = 3 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 1
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 1
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 1
          End If
    
          If Schritt = 4 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 1
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 1
             Led5 = 0
             Led6 = 0
             Led7 = 0
             Schritt = 0
          End If
    
       End If
    
       If Tast5 = 1 And Tast2 = 0 And Tast3 = 0 And Tast4 = 0 And Tast7 = 0 And Tast6 = 0 Then
    
          If Status <> 6 Then
                Schritt = 0                                     'Programm 5
                Status = 6
          End If
    
          If Schritt = 1 Then
             Staled1 = 1
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 2 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 3 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 4 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 0
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 0
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 5 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 0
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 0
             Led7 = 0
          End If
    
          If Schritt = 6 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 0
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 0
          End If
    
          If Schritt = 7 Then
             Staled1 = 1
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 8 Then
             Staled1 = 0
             Staled2 = 1
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 9 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 1
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 10 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 1
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 11 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 1
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 1
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 12 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 1
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 1
             Led7 = 1
          End If
    
          If Schritt = 13 Then
             Staled1 = 0
             Staled2 = 0
             Staled3 = 0
             Staled4 = 0
             Staled5 = 0
             Staled6 = 0
             Staled7 = 1
             Led1 = 0
             Led2 = 0
             Led3 = 0
             Led4 = 0
             Led5 = 0
             Led6 = 0
             Led7 = 1
             Schritt = 0
          End If
    
       End If
    
       If Tast2 = 1 Then                                        'abspeichern der Werte im Eprom
    
          Espeed = Speed
          Epwm = Pwm
    
          Do
    
             Led1 = 1
             Led2 = 1
             Led3 = 1
             Led4 = 1
             Led5 = 1
             Led6 = 1
             Led7 = 1
    
          Loop Until Tast7 = 0
    
    
       End If
    
    
    
    Loop
    
    End
    
    
    
    Routtimer0:
    
       If Zaehler <= Pwm Then                                   'PWM
    
          If Staled1 = 1 Then Led1 = 1
          If Staled2 = 1 Then Led2 = 1
          If Staled3 = 1 Then Led3 = 1
          If Staled4 = 1 Then Led4 = 1
          If Staled5 = 1 Then Led5 = 1
          If Staled6 = 1 Then Led6 = 1
          If Staled7 = 1 Then Led7 = 1
    
       End If
    
       If Zaehler > Pwm Then
    
         If Staled1 = 1 Then Led1 = 0
         If Staled2 = 1 Then Led2 = 0
         If Staled3 = 1 Then Led3 = 0
         If Staled4 = 1 Then Led4 = 0
         If Staled5 = 1 Then Led5 = 0
         If Staled6 = 1 Then Led6 = 0
         If Staled7 = 1 Then Led7 = 0
    
       End If
    
       If Zaehler = 1026 Then Zaehler = 0
    
       Zaehler = Zaehler + 1
    
    Return
    
    
    
    
    Routtimer2:                                                 'Programm geschwindigkeit
    
       If Zaehler2 >= Speed Then
          Zaehler2 = 0
          Schritt = Schritt + 1
       End If
    
       Zaehler2 = Zaehler2 + 1
    
    Return

  2. #2
    Erfahrener Benutzer Roboter Experte
    Registriert seit
    18.05.2007
    Ort
    Berlin
    Alter
    53
    Beiträge
    765
    Es gibt im Netz einige Hinweise, dass man die 1.Eprom-Adresse besser nicht benutzt. Probier mal ganz am Anfang ein Dim eDum as eram word oder so einzufügen und dieses nicht weiter zu benutzen. Ansonsten ist der Code noch gut optimierbar.
    Wenn das Herz involviert ist, steht die Logik außen vor! \/

  3. #3
    Benutzer Stammmitglied
    Registriert seit
    08.07.2008
    Beiträge
    49
    Ok

    Danke für die Hilfe. Werde ich mrogen oder so einfach einmal machen.
    Was wäre denn noch zu optimieren ist eins meiner ersten Programme....

    vielen Dank
    dämmi

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Solar Speicher und Akkus Tests