-
Projekt: Taschenrechner
Hallo
Ich habe vor einen Taschenrechner in Bascom zu programmieren.
Als Anzeige benutze ich ein 16*2 Display.
Mein Problem ist jetzt wie ich dem Controller sage welche Zahl eingegeben wurde.
Zur Erkennung welche Rechenoperation ausgeführt werden soll(+,-,* und /) habe ich mir gedacht, weise ich einem String die Rechenoperation zu und benutze diesen um das Ergebnis dann zu berechnen.
Momentan habe ich schon die Anzeige der Zahlen programmiert.
Hier der Quellcode:
Code:
$regfile = "M16def.dat" ' use the Mega16
$crystal = 1000000
Config Lcdpin = Pin , Db4 = Porta.3 , Db5 = Porta.2 , Db6 = Porta.1 , Db7 = Porta.0 , E = Porta.4 , Rs = Porta.5
Config Lcd = 16 * 2
Initlcd
Config Pinb.0 = Input 'Zahl 0
Config Portb.1 = Input 'Zahl 1
Config Portb.2 = Input 'Zahl 2
Config Portb.3 = Input 'Zahl 3
Config Portb.4 = Input 'Zahl 4
Config Portd.0 = Input 'Zahl 5
Config Portd.1 = Input 'Zahl 6
Config Portd.2 = Input 'Zahl 7
Config Portd.3 = Input 'Zahl 8
Config Portd.4 = Input 'Zahl 9
Config Portd.5 = Input 'Plus
Config Portd.7 = Input 'Minus
Config Porta.6 = Input 'Mal
Config Porta.7 = Input 'Geteilt
Config Portc.7 = Input 'DELETE
Config Portc.6 = Input 'AC
Config Portc.5 = Input '=
Config Portc.4 = Input 'Komma
Config Portc.3 = Input 'Shift
Config Portc.2 = Input 'x Hoch y
Portb.0 = 1 'Zahl 0
Portb.1 = 1 'Zahl 1
Portb.2 = 1 'Zahl 2
Portb.3 = 1 'Zahl 3
Portb.4 = 1 'Zahl 4
Portd.0 = 1 'Zahl 5
Portd.1 = 1 'Zahl 6
Portd.2 = 1 'Zahl 7
Portd.3 = 1 'Zahl 8
Portd.4 = 1 'Zahl 9
Portd.5 = 1 'Plus
Portd.7 = 1 'Minus
Porta.6 = 1 'Mal
Porta.7 = 1 'Geteilt
Portc.7 = 1 'keine Funktion
Portc.6 = 1 'AC
Portc.5 = 1 '=
Portc.4 = 1 'Komma
Portc.3 = 1 'Shift
Portc.2 = 1 'x Hoch y
Dim Positionzeile As Integer
Dim Positionspalte As Integer
Dim Variable2 As Integer
Dim Variable16 As Integer
Positionzeile = 1
Positionspalte = 1
Variable2 = 2
Variable16 = 16
Do
If Positionspalte > Variable16 Then
Positionzeile = 2
Waitms 50
End If
If Positionzeile > Variable2 Then
Positionzeile = 1
Waitms 50
End If
If Pinb.0 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte ' Zahl 0 schreiben
Lcd "0"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pinb.1 = 0 Then
'Zahl 1 schreiben
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "1"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pinb.2 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "2"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pinb.3 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "3"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pinb.4 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "4"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.0 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "5"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.1 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "6"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.2 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "7"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.3 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "8"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.4 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "9"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.5 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "+"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pind.7 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "-"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pina.6 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "x"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pina.7 = 0 Then
Cursor Off
Locate Positionzeile , Positionspalte
Lcd "/"
Positionspalte = Positionspalte + 1
Waitms 200
End If
If Pinc.6 = 0 Then
Cls
Cursor Off
Positionspalte = 1
Positionzeile = 1
Waitms 200
End If
Kann mir jemand einen Denkansatz geben wie ich das Problem lösen kann?
Mfg
David
-
Kann man die einzelnen Zahlen in in Array Felder schreiben und dann zusammenfügen ?
Oder kann man die angezeigte Zahl auf dem Display irgendwie auslesen indem man sie als String speichert ?
Mfg
David
-
Hallo David,
ich habe mal ein codeschloss entwickelt, bei dem ich den code folgendermassen zusammengefügt habe:
Code:
input code 'eingabe der einzelnen ziffern
if code <> "enter" then
mycode = mycode * 10
mycode = mycode + code
endif
vielleicht hilft dir das ja?!?!
gruß
Chris
-
ich würd alles in eine string schreiben z.B.
rechnung$="56+17="
for stelle = 1 to len(rechnung$)
if mid(rechnung$,stelle,1)="+" then Ergebniss=val(mid(rechnung$,1,stelle))+val(mid(rec hnung$,stelle+1,len(rechnung$)))
next stelle
oder so was ähnliches zur auswertung
-
Hi,
du hast die Eingänge mit:
Config Portb.1 = Input
anstatt
Config Pinb.1 = Input
festgelegt. Änder das mal ab sonst werden die nicht richtig funktionieren =)
-
Hallo
danke erstmal für die Antworten.
@ Che Guevara
Dein Codeschloss verstehe ich ehrlich gesagt nicht.
Ich weiß nicht was mit Mycode gemeint ist und was der Unterschied zwischen code und Mycode ist.
Brauche ich für das Programm noch einen Enter-Button oder kann dass bestätigen des Codes durch ein Rechenzeichen erfolgen.
Also ich meine wenn z.B. + gedrückt wird dass die eingegeben Zahlen dann bestätigt werden.
Da ich nicht weiß was Mycode ist verstehe ich den Sinn von mit 10 mal nehmen und Code addieren nicht.
Kannst du mir das nochmal genauer erklären ?
@Thomas
Das Grundschema deines Codes verstehe ich nur bei der ersten Zeile also
rechnung$="56+17="
weiß ich nicht wie man das dem String zuweisen soll.
Kann man da falls die Zahl 1 gedrückt wird, schreiben rechnung$="1"
und falls danach 2 gedrückt wird rechnung$ = "vorherige eingabe(1) und 2"?
Gibt es so eine Art Befehl in Bascom oder muss man das ganz anders machen?
Wäre nett wenn jemand das ganze nochmal erläutern könnte.
Mfg
David
-
Hallo David,
ich versuche mal dir das zu erklären:
'Mycode' ist die zahl, die du eingeben möchtest, z.b. '58'.
'Code' ist immer eine ziffer, in diesem fall erst die '5' und anschließend die '8'.
wenn man jetzt rechnet: 5 * 10 = 50 (also: mycode = mycode * 10)
50 + 8 (also 'code') = 58 (also: mycode = mycode + code)
du brauchst natürlich in deinem fall keine 'enter' taste.
du musst einfach nur nach jedem tastendruck abfragen, ob eine der 'entertasten' (also: + ; - ; * ; / ; =) gedrückt wurde.
ist das der fall, musst du die entsprechende rechenoperation ausführen.
ich hoffe, es ist jetzt etwas verständlicher für dich, ansonsten kannst du mir mal eine pn schicken, falls du interesse hast!
Gruß
chris
-
ich hab das so gedacht
wenn (1) gedrückt wurde dann rechnung$=rechnung$+"1"
oder wenn (+) gedrückt wurde dann rechnung$=rechnung$+"+"
-
hallo Thomas$,
ich glaube(!), dass das zwar gut für die einfache darstellung auf dem display wäre, aber damit kann der µC dann doch nicht rechnen! dann müsste man alles doppelt machen, oder nicht?!?!
Gruß
chris
-
ich weis das das umständlich ist aber es hat vorteile was ist wenn ich ein was ändern will ? ich will doch nicht immer die lange zahl mehrmals eingeben weil ich mich vertippt hab ( ich bin classpad 300 verwöhnt) dann lösch ich die stelle am string da ist das umständliche von Vorteil auserdem find ich das leichter zu verstehen (ok das ist ansichts sache)