Also, ich find´s schon viel besser als vorher!
Sogar die If-Then-Abfragen mit Tab eingerückt, sieht richtig gut aus!
Allerdings, Kommentarzeilen (die beginnen mit Hochkomma ') sind nicht nur ein uncooles Feature für Alzheimer-geplagte Programmierer - sie erleichtern auch "Programmfremden" den Durchblick und beschleunigen sogar die Orientierung im eigenen Programm!
Ich weiß ja, "Kommentarzeilen-Einfüger" steht in einer Reihe mit "Schattenparker" und "Warmduscher", aber ich finde, fehlende Kommentare sind fast noch schlimmer als verstrubbelte Goto´s und Gosub´s, weil hinterher noch nicht einmal mehr die Intentionen des Programmierers nachvollzogen werden können -> 10 Euro in die Kaffeekasse! \
/
Man kann im Übrigen Kommentare ins Bascom-Programm hacken bis zum Abwinken, sie fressen keinen Speicherplatz im Chip und werden somit auch bei der Einschränkung der Bascom-Demo nicht berücksichtigt.
Du hattest ja gefragt, ob der Stil so ok ist, und das würde halt noch fehlen.
Hier ein Beispiel aus eigener Feder, stell Dir mal vor, Du müsstest da ohne die Kommentare durchsteigen:
:
Code:
'*****************************************
' Program for PWM Speed Controller
' based on Stupsi´s RN-Power-Board
' Designed for Atmel AVR Mega 8
' Bascom Version 1.11.7.9
' 18.11.2005 T. Gietenbruch
'
' Hardware Configuration:
' -> Setup button input at Pinb.0 (14)
' -> RC-Channel PWM input at Pind.2 (4) (INT0)
' -> Output FWD direction signal at Portd.6 (12)
' -> Output BWD direction signal at Portd.7 (13)
' -> LED for FWD direction indication at Portd.4 (6)
' -> LED for BWD direction indication at Portd.5 (11)
' -> Output PWM_Pulse at Portb.3 (17) (MOSI / OC2)
' -> I2C-Bus: SDA at PC4 / SCL at PC5
' -> Internal Oscillator 8000000Hz
'
' External TTL logic for circuit protection
'
'******************************************
'==========================================
' System Configurations
'==========================================
'Definition for Mega 8
$regfile "m8def.dat"
$crystal = 8000000
'$lib "i2c_twi.lbx"
'==========================================
' Configurations
'==========================================
'PIN Configurations
' Bit 76543210 0->IN / 1->OUT
Ddrb = &B00001000
Ddrd = &B11110000
'Timer 1 for RC-Channel measurement
Config Timer1 = Timer , Prescale = 1
'Timer 2 for PWM-signal on OC2
Config Timer2 = Pwm , Prescale = 64 , Compare = Clear
'==========================================
' Tag Declarations
'==========================================
Dim Error As Byte
Dim Init As Bit
Dim N As Byte
Dim Timer_calc As Long
Dim Steps_max As Word
Dim Steps_min As Word
Dim Upperlimit As Word
Dim Lowerlimit As Word
Dim Window As Word
Dim Start_max As Word
Dim Start_min As Word
Dim State As Byte
'EEPROM Tags
Dim Neutral As Eram Word
Dim Maximum As Eram Word
Dim Minimum As Eram Word
Dim State_byte As Eram Byte
'State_Byte.0 for Direction: 0-> Not inverted, 1-> Inverted
'==========================================
' Interrupt Handlings
'==========================================
On Timer1 Read_error
Enable Interrupts
'==========================================
' Initializing Timers
'==========================================
Enable Timer1
Enable Timer2
Stop Timer1
Stop Timer2
Timer1 = 0
Timer2 = 0
'==========================================
' Initializing values
'==========================================
Ocr2 = 0
Portb = &B11111111
Portd = &B00111111
Window = 250
Init = 0
Error = 0
'==========================================
' Configure RC-Channel Settings
'==========================================
Setup_again:
'1. Wait until switch on B.0 is on
If Pinb.0 = 1 Then Goto Nosetup
Bitwait Pinb.0 , Set
While Pinb.0 <> 0
Toggle Portd.4
Toggle Portd.5
Waitms 500
Wend
Portd.4 = 1
Portd.5 = 1
'2. Read neutral position value
Timer1 = 0
Start Timer1
Bitwait Pind.2 , Reset
Gosub Read_channel
Stop Timer1
Neutral = Timer1
Timer1 = 0
'Flash LED for read confirmation
For N = 0 To 2
Toggle Portd.4
Toggle Portd.5
Waitms 30
Toggle Portd.4
Toggle Portd.5
Waitms 100
Next
Portd.4 = 1
Portd.5 = 1
'4. Wait until switch on B.0 is off and on again
Bitwait Pinb.0 , Set
Waitms 100
While Pinb.0 <> 0
Toggle Portd.4
Waitms 500
Wend
Portd.4 = 1
'5. Read forward value
Timer1 = 0
Start Timer1
Bitwait Pind.2 , Reset
Gosub Read_channel
Stop Timer1
If Timer1 > Neutral Then
Maximum = Timer1
State.0 = 0
Else
Minimum = Timer1
State.0 = 1
End If
Timer1 = 0
'Flash LED for read confirmation
For N = 0 To 2
Toggle Portd.4
Waitms 30
Toggle Portd.4
Waitms 100
Next
Portd.5 = 1
'6. Wait until switch on B.0 is off and on again
Bitwait Pinb.0 , Set
Waitms 100
While Pinb.0 <> 0
Toggle Portd.5
Waitms 500
Wend
Portd.5 = 1
'7. Read backward value
Timer1 = 0
Start Timer1
Bitwait Pind.2 , Reset
Gosub Read_channel
Stop Timer1
Select Case State.0
Case 0:
If Timer1 > Neutral Then
Goto Setup_again
Else
Minimum = Timer1
End If
Case 1:
If Timer1 < Neutral Then
Goto Setup_again
Else
Maximum = Timer1
End If
End Select
Timer1 = 0
'Flash LED for read confirmation
For N = 0 To 2
Toggle Portd.5
Waitms 30
Toggle Portd.5
Waitms 100
Next
Portd.5 = 1
'8. Write State to eprom byte
State_byte = State
'8. Wait until switch on B.0 is off
Bitwait Pinb.0 , Set
'Target without setup
Nosetup:
If Neutral < 2000 Or Neutral > 20000 Or Maximum < 6000 Or Maximum > 30000 Or Maximum <= Neutral Or Minimum < 2000 Or Minimum > 30000 Or Minimum >= Neutral Then
Portd.4 = 0
Portd.5 = 1
While Pinb.0 = 1
Toggle Portd.4
Toggle Portd.5
Waitms 100
Wend
Portd.4 = 1
Portd.5 = 1
Goto Setup_again
End If
'Calculation of RC-Channel compare values
Start_max = Neutral + Window
Steps_max = Maximum - Start_max
Steps_max = Steps_max - 30
Start_min = Neutral - Window
Steps_min = Start_min - Minimum
Steps_min = Steps_min - 30
State = State_byte
'==========================================
' Main Program Loop
'==========================================
Do
'Disable PWM with Pinb.0=0 (Button pressed)
If Pinb.0 = 0 Or Init = 0 Then
Stop Timer2
Timer2 = 0
Ocr2 = 0
Portd.6 = 0
Portd.7 = 0
End If
'Enable PWM with Pinb.0=1
If Pinb.0 = 1 And Init = 1 Then
Start Timer2
End If
'Calculate PWM-value for maximum direction
If Timer1 >= Start_max And Pinb.0 = 1 And Init = 1 Then
Portd.6 = Not State.0
Portd.7 = State.0
Timer_calc = Timer1 - Start_max
Timer_calc = Timer_calc * 255
Timer_calc = Timer_calc / Steps_max
End If
'Calculate PWM-value for minimum direction
If Timer1 <= Start_min And Pinb.0 = 1 And Init = 1 Then
Portd.6 = State.0
Portd.7 = Not State.0
Timer_calc = Start_min - Timer1
Timer_calc = Timer_calc * 255
Timer_calc = Timer_calc / Steps_min
End If
'Set PWM-value to zero in within neutral position window
If Timer1 > Start_min And Timer1 < Start_max Then
Portd.6 = 0
Portd.7 = 0
Timer_calc = 0
Init = 1
End If
'Limit PWM-value to maximum 255
If Timer_calc > 255 Then Timer_calc = 255
'Write PWM-value to compare register for timer2
If Init = 1 And Pinb.0 = 1 Then
Ocr2 = Timer_calc
Else
Portd.6 = 0
Portd.7 = 0
Ocr2 = 0
End If
'Switch LED´s on according direction bits
Portd.4 = Not Portd.6
Portd.5 = Not Portd.7
'Jump to RC-channel subroutine
Enable Interrupts
Timer1 = 0
Start Timer1
Bitwait Pind.2 , Reset
Stop Timer1
Gosub Read_channel
Disable Interrupts
Loop
'RC-channel reading
Read_channel:
Timer1 = 0
Start Timer1
Bitwait Pind.2 , Set
Timer1 = 0
Bitwait Pind.2 , Reset
Stop Timer1
Error = 0
Return
'Error handler
Read_error:
If Error < 10 Then Error = Error + 1
While Error = 10
Stop Timer2
Timer2 = 0
Ocr2 = 0
Init = 0
Portd.6 = 0
Portd.7 = 0
Portd.4 = 0
Portd.5 = 0
Gosub Read_channel
Wend
Return
Grüße
Torsten
@PicNick:
Dir wär´s bestimmt lieber, wenn das der reiche Erbonkel wäre...
Lesezeichen