'gLCD-Modul für LC-Displays mit KS0108b-Controller von Christof Rueß, www.hobbyelektronik.org
'Die Verwendung dieses Moduls ist kostenfrei, solange diese Zeilen erhalten bleiben.
'Fragen, Kritik und Anregungen an: chris at hobbyelektronik punkt org
'Anschlussbelegung für KS0108b-Displays am ParallelPort:
'PC D-SUB-Stecker - Display
' 1 (Datastrobe) - 6 (Enable)
' 2 (D0) - 7 (D0)
' ... - ...
' 14 (Autofeed) - 15 (CS1)
' 16 (Init) - 16 (CS2)
' 17 (Select) - 4 (D/I)
' 18-25 (GND) - 1 (GND)
' Zusätzlich am Display:
' Pin:
' 1 (GND) - GND
' 2 (Vdd) - +5V
' 3 (Vee) - Potentiometer mit 25k, Schleifer an Vee (3), das eine Ende an Pin Vout (18), das andere über 20k-Widerstand an GND (1)
' 5 (R/W) - an GND (1) (leider noch - das Timing kann auf moderneren PCs knapp werden, wenn sowas mal passiert: Mail an mich!)
' 17 (RST) - über 10k-Widerstand an Vdd (2)
' 18 (Vout)- s.o.
' 19 (A) - Wenn eine Hintergrundbeleuchtung vorhanden ist, über 10-Ohm-Widerstand an Vdd (2)
' 20 (C) - einfach an GND (1)
'Deklaration für die Portansteuerung (auch für Win2k & WinXP)
Public Declare Sub Portout32 Lib "inpout32.dll" Alias "Out32" (ByVal PortAddress As Integer, ByVal Value As Integer)
Public Declare Function PortIn32 Lib "inpout32.dll" Alias "Inp32" (ByVal PortAddress As Integer) As Integer
'Damit das Auslesen der Bilder für die selbstdefinierten Zeichen schneller geht, bediene ich mich dieser API.
Public Declare Function GetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal Y As Long) As Long
Public Declare Function SetPixel Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, ByVal Y As Long, ByVal crColor As Long) As Long
'Zum Testen der Übertragungsgeschwindigkeit notwendig)
Public Declare Function QueryPerformanceCounter Lib "kernel32" (lpPerformanceCount As Currency) As Long
Public Declare Function QueryPerformanceFrequency Lib "kernel32" (lpFrequency As Currency) As Long
Const Enlo = 1 'Pin 1
Const Enhi = 0
Const CS1lo = 2 'Pin 14
Const CS1hi = 0
Const CS2lo = 0 'Pin 16
Const CS2hi = 4
Const RSlo = 8 'Pin 17
Const RShi = 0
Public Enum LCD_Controller
cLeft = CS1hi + CS2lo
cRight = CS1lo + CS2hi
cBoth = CS1hi + CS2hi
End Enum
Private LcdPort As Long
Private LcdInverted As Boolean
Private Charset(0 To 255) As String
'Hiermit wird der Port, an dem das LCD angschlossen ist festgelegt
Property Let Port(NewPort As Long)
LcdPort = NewPort
End Property
'Damit der Port auch gelesen werden kann, ist dies hier notwendig:
Property Get Port() As Long
Port = LcdPort
End Property
'Setzen, ob das Display invertiert werden soll
Property Let Inverted(NewInverted As Long)
LcdInverted = NewInverted
End Property
'Lesen, ob das Display invertiert ist
Property Get Inverted() As Long
Port = LcdInverted
End Property
'binären String in dezimalen Long umwandeln
Function BintoDez(ByVal Bin As String) As Long
Dim x&, Y&
Bin = Bin & String$(8 - Len(Bin), "0")
For x = 1 To Len(Bin)
If Mid$(Bin, x, 1) = "1" Then
Y = Y + 2 ^ (8 - x)
End If
Next x
BintoDez = Y
End Function
'dezimal -> binär
Function DeztoBin(ByVal Dez As Long) As String
Dim x%
If Dez >= 2 ^ 32 Then
Call MsgBox("Zahl ist größer als 32 Bit")
Exit Function
End If
Do
If (Dez And 2 ^ x) Then
DeztoBin = "1" & DeztoBin
Else
DeztoBin = "0" & DeztoBin
End If
x = x + 1
Loop Until 2 ^ x > Dez
DeztoBin = Format(DeztoBin, "00000000")
End Function
'Ausgabe an das LCD - durch den Inversmodus ein bisschen aufgebläht
Public Function OutLcd(ByVal LcdData As Long, ByVal IsCommand As Boolean, ByVal Controller As LCD_Controller)
If IsCommand = True Then
Portout32 LcdPort, LcdData
Portout32 LcdPort + 2, Enhi + Controller + RSlo
Portout32 LcdPort + 2, Enlo + Controller + RSlo
Else
If LcdInverted = True Then
Portout32 LcdPort, 255 - LcdData
Else
Portout32 LcdPort, LcdData
End If
Portout32 LcdPort + 2, Enhi + Controller + RShi
Portout32 LcdPort + 2, Enlo + Controller + RShi
End If
End Function
'Display initialisieren
Function Init()
OutLcd 63, True, cBoth 'Display anschalten
OutLcd 192, True, cBoth 'Start-Line festlegen
End Function
'Die angegebene Picturebox auf das Display übertragen
Function WritePic(ByVal LcdPicture As PictureBox)
Dim x, currPage
Dim StrRev As Boolean
Dim OldScaleMode As ScaleModeConstants
OldScaleMode = LcdPicture.ScaleMode 'Scalemode merken
LcdPicture.ScaleMode = 3 'Scalemode auf Pixel stellen
For x = 0 To 7
OutLcd 184 + x, True, cBoth
OutLcd 64, True, cBoth
WritePicLine LcdPicture, x * 8 'Zeile x an das Display ausgeben
Next
LcdPicture.ScaleMode = OldScaleMode 'Scalemode zurücksetzen
End Function
'Zeichnen der angegeben Zeile des Displays (nur intern)
Private Function WritePicLine(ByVal LcdPicture As PictureBox, ByVal Ystart As Long)
Dim Temp As Long
Dim Xval, Yval
For Xval = 0 To 127
Temp = 0
For Yval = Ystart To (Ystart + 7)
If GetPixel(LcdPicture.hDC, Xval, Yval) < 8388607 Then
Temp = Temp + 2 ^ (Yval - Ystart)
End If
Next Yval
If Xval < 64 Then
OutLcd Temp, False, cLeft
Else
OutLcd Temp, False, cRight
End If
Next Xval
End Function
'Initialisiert die benutzerdefinierte Schriftart. Momentan beträgt die maximale Zeichenhöhe 8 Pixel. Die Breite ist beliebig.
'Das Dateiformat ist sehr Simpel aufgebaut:
'Die ersten drei Zahlen stehen für den ASCII-Code des Zeichens. Für A wäre dies 065.
'Die nachfolgenden Nummern beschreiben das Symbol. Jede dreier-Gruppe beschreibt eine des Symbols. Vorhergehende Nullstellen müssen als 0 geschrieben werden (32 -> 032)
'Die erste Spalte für A wäre 01111110 -> 126, die zweite 10010000 -> 144, ...
'Der gesamte Buchstabe "A" sieht wie folgt aus:
'065126144144144126
' A 0 1 1 1 0
' 1 0 0 0 1
' 1 0 0 0 1
' 1 1 1 1 1
' 1 0 0 0 1
' 1 0 0 0 1
' 1 0 0 0 1
' 0 0 0 0 0
Function InitFont(ByVal FontFile As String) As Boolean
On Error GoTo Fehler
Dim FHandle As Long
Dim Temp As String
FHandle = FreeFile
Open FontFile For Input As FHandle
Do While EOF(FHandle) = False
Input #FHandle, Temp
If Len(Trim(Temp)) <> 0 And Left(Trim(Temp), 1) <> "#" Then
Charset(Mid(Temp, 1, 3)) = Right(Temp, Len(Temp) - 3)
End If
Loop
Close FHandle
InitFont = True
Exit Function
Fehler:
InitFont = False
End Function
'Den angegebenen Text der initialisierten Schriftart auf eine PictureBox mit hDc schreiben.
Function WriteText(ByVal DstPicture As PictureBox, ByVal LcdText As String, ByVal StartX As Long, ByVal StartY As Long, Optional ByVal FontTransparent As Boolean = False, Optional ByVal FontNegative As Boolean = False) As Boolean
On Error GoTo Fehler
Dim x, Y, TxtPos, ChrPos, clrActive, clrInactive As Long
Dim CurrChar, CurrCharCol As String
Dim OldScaleMode As ScaleModeConstants
OldScaleMode = DstPicture.ScaleMode 'Scalemode merken
DstPicture.ScaleMode = 3 'Scalemode auf Pixel stellen
x = StartX
If FontNegative = False Then 'Wenn die Schrift nicht negativ sein soll, ...
clrActive = 0 '... soll die dunkle Farbe Schwarz sein...
clrInactive = 16777215 '... und die helle Farbe weiß
Else 'andernfalls
clrActive = 16777215 'umgekehrt!
clrInactive = 0
End If
For TxtPos = 1 To Len(LcdText) 'den ganzen Text "abklappern"
CurrChar = Charset(Asc(Mid(LcdText, TxtPos, 1))) 'aktuelles Zeichen in die Variable setzen (Übersichtlicher/spart Zeit)
For ChrPos = 1 To Fix(Len(CurrChar) / 3) 'jede Spalte ist 3 Zeichen lang
CurrCharCol = DeztoBin(Mid(CurrChar, ChrPos * 3 - 2, 3)) 'die noch als dezimale Spalte in Binärwert umwandeln
For Y = 1 To 8 'die einzelnen Bits abfragen
If Mid(CurrCharCol, Y, 1) = "1" Then 'wenn das Bit 1 ist, soll...
SetPixel DstPicture.hDC, x, StartY - 1 + Y, clrActive 'die das Bit aktiv dargestellt werden
Else 'andernfalls...
If FontTransparent = False Then '... und wenn die Schrift _nicht_ transpartent dargestellt werden soll, ...
SetPixel DstPicture.hDC, x, StartY - 1 + Y, clrInactive '...soll das Bit inaktiv dargestellt werden
End If
End If
Next Y
x = x + 1 'die X-Koordinate 1 weiter nach rechts setzen
If FontTransparent = False And TxtPos < Len(LcdText) Then 'Wenn die Schrift _nicht_ transpartent dargestellt werden soll, ...
For Y = 1 To 8
SetPixel DstPicture.hDC, x, StartY - 1 + Y, clrInactive 'soll eine ganze Spalte mit der inaktiven Farbe eingefügt werden
Next Y
End If
Next ChrPos
x = x + 1 'die X-Koordinate 1 weiter nach rechts setzen (Platz zwischen den Buchstaben)
Next TxtPos
DstPicture.ScaleMode = OldScaleMode 'Scalemode zurücksetzen
DstPicture.Refresh
WriteText = True
Exit Function
Fehler:
DstPicture.ScaleMode = OldScaleMode 'Scalemode zurücksetzen
WriteText = False
End Function
'Berechnen der Textlänge in Pixeln anhand der geladenen Schriftart und dem gewünschten Text
Function TextLength(ByVal LcdText As String) As Long
Dim x, TxtLen
TxtLen = 0
For x = 1 To Len(LcdText)
TxtLen = TxtLen + Fix(Len(Charset(Asc(Mid(LcdText, x, 1)))) / 3) ' + 1
Next
TxtLen = TxtLen + Len(LcdText) - 1
If TxtLen = -1 Then TxtLen = 0
TextLength = TxtLen
End Function
Function ClearDisplay()
Dim x, Y As Long
For Y = 0 To 7
OutLcd 184 + Y, True, cBoth
For x = 0 To 64
OutLcd 0, False, cBoth
Next x
Next Y
End Function
Function ShowNoise()
Dim x, Y As Long
Randomize
For Y = 0 To 7
OutLcd 184 + Y, True, cBoth
For x = 0 To 63
OutLcd Rnd * 255, False, cLeft
OutLcd Rnd * 255, False, cRight
Next x
Next Y
End Function
'Testet die Übertragungsgeschwindigkeit eines kompletten Displayinhaltes an das Display
'Die Ausgabe erfolgt in ms
Function TestLcdSpeed() As Currency
Dim PerfFrequ As Currency
Dim PerfCntA As Currency
Dim PerfCntB As Currency
Dim PerfCntC As Currency
Dim PerfCntAvl As Long
PerfCntAvl = QueryPerformanceFrequency(PerfFrequ) '"Kalibriert" die Uhr
QueryPerformanceCounter PerfCntA 'den Startwert für den Counter in PerfCntA schreiben
ShowNoise 'Zum Testen der Geschwindigkeit wird das Display "verrauscht"
QueryPerformanceCounter PerfCntB 'den Endwert für den Counter in PerfCntB schreiben
TestLcdSpeed = CDbl((PerfCntB - PerfCntA) / PerfFrequ) * 1000
End Function