es gibt noch gute Nachrichten:
nachdem wir vor 2 Jahren das Thema diskutiert hatten:
"State Machine: LOADLABEL in DATA einsetzen"https://www.roboternetz.de/phpBB2/ze...hlight=#191075
wurde in Bascom in der Version 1.11.8.9 den Befehl ADR und ADR2 ergänzt
Das gibt uns ganz neue Möglichkeiten die Datensätze anzulegen \
/ und den Resourcenfresser "Select Case" zu beerdigen
Codeschnipsel
Code:
S10:
ADR2 Null : ADR2 S20 : ADR2 Null : ADR2 S11 'Key_plus|Key_minus|Key_prev|Key_next
ADR2 Null 'Subroutine for current State
Data "1 Butterfly Bascom" 'Menue Display Text
Anbei eine geänderte Fassung des Beispiel-Menüs. Die Codelänge schrumpfte von 3300Byte auf sagenhafte 2200 Byte und liegt damit mit der alten unübersichtlichen Fassung aus dem AVR Butterfly Evaluation Kit gleichauf
Anbei wieder vollständig lauffähiger Code für den Bascom-Simulator (getestet 1.11.9.1)
Code:
'Beispiel für ein State Machine Menü
'Das Beispiel ist für den BASCOM-Simulator angepasst worden
' getestet mit Bascom 1.11.9.1
' Codelänge 2242 Byte
$regfile = "m32def.dat"
$framesize = 32 'Stack
$swstack = 32
$hwstack = 64
'
'Hinweis: Im Simulator müssen die Eingaben in dem blauen "Terminal Emulator Window" erfolgen!
'Auf der Tastatur ergeben sich für die VIER Joystick-Positionen folgende Umsetzungen
'
' [Key_plus ]
' [Key_prev] [ ] [Key_next]
' [Key_minus]
'
'
' [ Taste_8 ]
' [Taste_4 ] [ ] [Taste_6 ]
' [ Taste_2 ]
'
'bzw. im ASCII-Code:
Dim Keycode_string As String * 4
'Key_plus|Key_minus|Key_prev|Key_next
Keycode_string = "{056}{050}{052}{054}"
Dim Keycode(4) As Byte At Keycode_string Overlay
Const Key_null = 0 'keine Taste gedrückt
'Pins des LCD-Modules setzen ggf. an eigene Anschlüsse anpassen
Config Lcd = 16 * 1
Initlcd
Cls
'******** Joystick/Key Settings ***********************************************
Dim Key As Byte 'key in Mainloop
'******** LCD *****************************************************************
Dim Lcd_textbuffer As String * 25
'******** allg Variablen ******************************************************
Dim I As Byte , J As Byte
Dim W As Word
'********* State Variables ****************************************************
Dim State As Word 'aktueller State
Dim State_renew As Byte 'Flag
Dim State_gosub As Word 'aktuelles Unterprogramm
'Initial state variables
State_renew = 1
State = Loadlabel(s10) 'Startbildschirm
Key = Key_null 'keine Taste gedrückt
'********** MAIN-Loop *********************************************************
Do
'Menüeintrag und Tastencodes finden und ggf. State wechseln
'hier nur Tastendruck auswerten
Gosub Change_state
'Pointer nach Statuswechsel neu setzen
If State_renew = 1 Then Gosub Change_state
'Unterprogramm des aktuellen State anspringen
LDS R31, {State_gosub+1} 'High see Bascom-Doc Mixing ASM and BASIC
LDS R30, {State_gosub} 'Low
LSR R31 'Division durch 2 (Alternativ ADR verwenden)
ROR R30
'Call zum Sub
ICALL
'Displaytext auslesen
Read Lcd_textbuffer
'---------------------------------------------------------------
'place for your own code in main loop
'---------------------------------------------------------------
'LCD refresh wenn Menü verändert
If State_renew = 1 Then
State_renew = 0
Gosub Lcd_print
End If
'Tastaturabfrage mit Halt nur für Simulator, ansonsten Sleep+Timer_ISR verwenden!!
Key = Waitkey()
Loop
End
'********* State routines ***************************************************
'---------------------------------------------------------------
'Subroutine: Change_state
'Call from: main loop
'Purpose: Status der State Machine feststellen und ggf. Wechseln
'Result: Pointer auf State / Variable State_renew
'---------------------------------------------------------------
Change_state:
lds R8, {State}
lds R9, {State + 1}
For I = 1 To 4
Read W
If Key = Keycode(i) Then
If W <> Loadlabel(null) Then
State_renew = 1
Key = Key_null 'reset key status after get a new state of state machine (prevent influence on GOSUBs)
State = W
End If
End If
Next I
Read State_gosub 'Adesse des akt. Unterprogramms einlesen
Return
'---------------------------------------------------------------
'DATA: State Machine
'Result: Pointers auf States , Unterprogramme
' Menütexte
'---------------------------------------------------------------
Null:
'Null is a dummy flag for State and Gosub -> do nothing
Return
S10:
ADR2 Null : ADR2 S20 : ADR2 Null : ADR2 S11 'Key_plus|Key_minus|Key_prev|Key_next
ADR2 Null 'Subroutine for current State
Data "1 Butterfly Bascom" 'Menue Display Text
S11:
ADR2 Null : ADR2 Null : ADR2 S10 : ADR2 Null 'Key_plus|Key_minus|Key_prev|Key_next
ADR2 Null
Data "11 Rev 2"
S20:
ADR2 S10 : ADR2 S30 : ADR2 Null : ADR2 S21
ADR2 Null
Data "2 Time"
S21:
ADR2 Null : ADR2 S22 : ADR2 S20 : ADR2 S23
ADR2 Null
Data "21 Clock"
S22:
ADR2 S21 : ADR2 Null : ADR2 S20 : ADR2 S24
ADR2 Null
Data "22 Date"
S23: 'Show HH:MM:SS
ADR2 Null : ADR2 S24 : ADR2 S21 : ADR2 S25
ADR2 Showclock
Data ""
S24: 'Show DD:MM:YY
ADR2 S23 : ADR2 Null : ADR2 S22 : ADR2 S26
ADR2 Showdate
Data ""
S25:
ADR2 S27 : ADR2 Null : ADR2 S23 : ADR2 S65
ADR2 Null
Data "211 Adjust Clock"
S26:
ADR2 Null : ADR2 S28 : ADR2 S24 : ADR2 S66
ADR2 Null
Data "221 Adjust Date"
S27:
ADR2 Null : ADR2 S25 : ADR2 S23 : ADR2 S67
ADR2 Null
Data "212 Clock Format"
S28:
ADR2 S26 : ADR2 Null : ADR2 S23 : ADR2 S68
ADR2 Null
Data "222 Date Format"
S30:
ADR2 S20 : ADR2 S40 : ADR2 Null : ADR2 S31
ADR2 Null
Data "3 DataLogger"
S31:
ADR2 Null : ADR2 S32 : ADR2 S30 : ADR2 S35
ADR2 Null
Data "31 Log Cycle"
S32:
ADR2 S31 : ADR2 S33 : ADR2 S30 : ADR2 S36
ADR2 Null
Data "32 Delete Flash"
S33:
ADR2 S32 : ADR2 S34 : ADR2 S30 : ADR2 S37
ADR2 Null
Data "33 Show LogCount"
S34:
ADR2 S33 : ADR2 Null : ADR2 S30 : ADR2 S38
ADR2 Null
Data "34 Print to RS232"
S35: 'HH:MM
ADR2 Null : ADR2 Null : ADR2 S31 : ADR2 Null
ADR2 Datalogger_setloginterval
Data ""
S36:
ADR2 Null : ADR2 Null : ADR2 S32 : ADR2 S76
ADR2 Null
Data "321 RIGHT Delete DF"
S37: '1234
ADR2 Null : ADR2 Null : ADR2 S33 : ADR2 Null
ADR2 Datalogger_logcount
Data ""
S38:
ADR2 Null : ADR2 Null : ADR2 S34 : ADR2 S78
ADR2 Null
Data "341 RIGHT Print9600B"
S40:
ADR2 S30 : ADR2 S50 : ADR2 Null : ADR2 S41
ADR2 Null
Data "4 ADC"
S41:
ADR2 Null : ADR2 S42 : ADR2 S40 : ADR2 S45
ADR2 Null
Data "41 Temperature"
S42:
ADR2 S41 : ADR2 S43 : ADR2 S40 : ADR2 S46
ADR2 Null
Data "42 Voltage"
S43:
ADR2 S42 : ADR2 S44 : ADR2 S40 : ADR2 S47
ADR2 Null
Data "43 ADC Port RAW"
S44:
ADR2 S43 : ADR2 Null : ADR2 S40 : ADR2 S48
ADR2 Null
Data "44 Battery"
S45: '+24°C
ADR2 Null : ADR2 Null : ADR2 S41 : ADR2 Null
ADR2 Temperaturefunc
Data ""
S46: '0 mV
ADR2 Null : ADR2 Null : ADR2 S42 : ADR2 Null
ADR2 Voltagefunc
Data ""
S47: 'CH:RAW
ADR2 Null : ADR2 Null : ADR2 S43 : ADR2 Null
ADR2 Adc_raw_func
Data ""
S48: '2900mV
ADR2 Null : ADR2 Null : ADR2 S44 : ADR2 Null
ADR2 Adc_batt_func
Data ""
S50:
ADR2 S40 : ADR2 Null : ADR2 Null : ADR2 S51
ADR2 Null
Data "5 Options"
S51:
ADR2 Null : ADR2 S52 : ADR2 S50 : ADR2 S56
ADR2 Null
Data "51 LCD contrast"
S52:
ADR2 S51 : ADR2 S53 : ADR2 S50 : ADR2 S57
ADR2 Null
Data "52 Bootloader"
S53:
ADR2 S52 : ADR2 S54 : ADR2 S50 : ADR2 S83
ADR2 Null
Data "53 LCD OFF"
S54:
ADR2 S53 : ADR2 S55 : ADR2 S50 : ADR2 S58
ADR2 Null
Data "54 LCD Auto Power"
S55:
ADR2 S54 : ADR2 Null : ADR2 S50 : ADR2 S59
ADR2 Null
Data "55 Key Click"
S56: '0...15
ADR2 Null : ADR2 Null : ADR2 S51 : ADR2 Null
ADR2 Setcontrast
Data ""
S57:
ADR2 Null : ADR2 Null : ADR2 S52 : ADR2 S87
ADR2 Null
Data "521 RIGHT bootloader"
S58: 'OFF/5..90min
ADR2 Null : ADR2 Null : ADR2 S54 : ADR2 Null
ADR2 Autopower
Data ""
S59: 'ON/OFF
ADR2 Null : ADR2 Null : ADR2 S55 : ADR2 Null
ADR2 Keyclick_set
Data ""
'Untermenüs zu State 25 bis 28
S65: 'sub for "211 Adjust Clock"
ADR2 S67 : ADR2 Null : ADR2 S23 : ADR2 Null
ADR2 Setclock
Data ""
S66: 'sub for "221 Adjust Date"
ADR2 Null : ADR2 S68 : ADR2 S24 : ADR2 Null
ADR2 Setdate
Data ""
S67: 'sub for "212 Clock Format"
ADR2 Null : ADR2 S65 : ADR2 S23 : ADR2 Null
ADR2 Setclockformat
Data ""
S68: 'sub for "222 Date Format"
ADR2 S66 : ADR2 Null : ADR2 S24 : ADR2 Null
ADR2 Setdateformat
Data ""
'Untermenüs zu State 36 und 38
S76: 'sub for "321 RIGHT Delete DF"
ADR2 Null : ADR2 Null : ADR2 S36 : ADR2 Null
ADR2 Datalogger_erase
Data ""
S78: 'sub for "341 RIGHT Print9600B"
ADR2 Null : ADR2 Null : ADR2 S38 : ADR2 Null
ADR2 Datalogger_rs232
Data ""
'Untermenü zu State 53 und 57
S83: 'sub for "53 LCD OFF"
ADR2 Null : ADR2 Null : ADR2 S53 : ADR2 Null
ADR2 Power_off_func
Data ""
S87: 'sub for "521 RIGHT bootloader"
ADR2 Null : ADR2 Null : ADR2 S57 : ADR2 Null
ADR2 Bootfunc
Data ""
'********* LCD SUB routines ***************************************************
'---------------------------------------------------------------
'Subroutine: Lcd_print
'Call from: anywhere
'Purpose: gibt Lcd_textbuffer auf dem LCD-Display aus
'Result: LCD
'---------------------------------------------------------------
Lcd_print: 'Print lcd_textbuffer
Cls
Lcd Lcd_textbuffer
State_renew = 0
Return
'********* SUB Clock routines**************************************************
Showclock:
'Show the clock on the LCD
Lcd_textbuffer = "HH:MM:SS"
Gosub Lcd_print
Return
Setclock:
'Adjusts the Clock
Lcd_textbuffer = "HH=11"
Gosub Lcd_print
Return
Setclockformat:
'Adjusts the Clockformat (12H or 24H)
Lcd_textbuffer = "12H / 24H"
Gosub Lcd_print
Return
'********* SUB date routines **************************************************
Showdate:
'Show the date on the LCD
Lcd_textbuffer = "DD.MM.YY"
Gosub Lcd_print
Return
Setdate:
'Adjusts the Date
Lcd_textbuffer = "Month=12"
Gosub Lcd_print
Return
'---------------------------------------------------------------
Setdateformat:
'Adjusts the Dateformat "DDMMYY" , "MMDDYY" , "YYMMDD"
Lcd_textbuffer = "DDMMYY/YYMMDD"
Gosub Lcd_print
Return
'********* Datalogger routines ************************************************
Datalogger_setloginterval:
'set the datalog intervall HOUR:MINUTES
Lcd_textbuffer = "HH:MM"
Gosub Lcd_print
Return
Datalogger_erase:
'erase the dataflash
State = Loadlabel(s32) 'St_datalogger_erase
State_renew = 1
Gosub Change_state
Return
Datalogger_logcount:
'Show DF_LogCount
Lcd_textbuffer = "1234"
Gosub Lcd_print
Return
Datalogger_rs232:
'Print all DataLogs to RS232
State = Loadlabel(s34) 'St_datalogger_rs232 as next status of state machine
State_renew = 1
Gosub Change_state
Return
Temperaturefunc:
'temperature measurement in °C
Lcd_textbuffer = "+24C"
Gosub Lcd_print
Return
Voltagefunc:
'voltage measurement mV
Lcd_textbuffer = "0mV"
Gosub Lcd_print
Return
Adc_raw_func:
'ADC Temperature/Voltage/Light result as RAW
Lcd_textbuffer = "CH:RAW"
Gosub Lcd_print
Return
Adc_batt_func:
'battery voltage measurement
Lcd_textbuffer = "2900mV"
Gosub Lcd_print
Return
'********* Sub MENU / OPTIONS ********************************
Setcontrast:
'Adjust the LCD contrast
Lcd_textbuffer = "0...15"
Gosub Lcd_print
Return
Bootfunc:
State = Loadlabel(s10) 'St_avrbf as next status of state machine
State_renew = 1
Gosub Change_state
Return
Power_off_func:
'LCD OFF
State = Loadlabel(s53) 'Return State is St_options_power_off
Return
Autopower:
'Enable/Disable auto power save
Lcd_textbuffer = "ON/OFF"
Gosub Lcd_print
Return
Keyclick_set:
'Enable/Disable keyclick
Lcd_textbuffer = "ON/OFF"
Gosub Lcd_print
Return
Zur Info die Struktur des Testmenü:
Bild hier
Lesezeichen