{Considere um display LCD de 16 caracteres por 2 linhas previamente conectado a porta paralela.
Obs.: Obviamente não se trata de como construir tal periférico, pois se trata de técnicas de eletrônica e podem ser pesquisados em outros fóruns da categoria.
Primeiro crie a seguinte unit que será a classe de controle do LCD:
}
LCD_HOME = $02; // Coloca o cursor na 1. linha e 1. coluna 00000010
LCD_MODO = $04; // Modo de Funcionamento: 00000100
LCD_MODO_M = $01; // Mensagem desloca con cursor ou não 00000001
LCD_MODO_C = $02; // Cursor desloca para direita, ou esquerda 00000010
LCD_ONOFF = $08; // Controle do display: 00001000
LCD_ONOFF_CP = $01; // Cursor piscante ou não 00000001
LCD_ONOFF_C = $02; // Liga cursor ou não 00000010
LCD_ONOFF_D = $04; // Liga Display ou não 00000100
LCD_DESLOCA = $10; // Deslocamento do Cursor: 00010000
LCD_DESLOCA_D = $04; // Desloca pra direita ou esquerda 00000100
LCD_DESLOCA_M = $08; // Deslocar Mensagem ou o cursor 00001000
LCD_FUNCAO = $20; // Modo de Utilização: 00100000
LCD_FUNCAO_M = $04; // Usar Matriz 5x10 ou 5x7 00000100
LCD_FUNCAO_L = $08; // 2 ou mais linhas ou 1 linha 00001000
LCD_FUNCAO_B = $10; // 8 bits ou 4 bits 00010000
LCD_CGRAM = $40; // Estabelece endereço da CGRAM 01000000
LCD_DDRAM = $80; // Estabelece endereço da DDRAM 10000000
// Temporizações:
TEMPO_CURTO = 40; // 40us
TEMPO_LONGO = 1600; // 1600 us
TEMPO_INICIO = 15000; // 15 ms
type
THD44780 =class private
ValorPorta2 : Byte; public
PortaLCD : Integer;
lcd_id : integer;
Colunas : Integer;
Linhas : Integer;
destructor THD44780.Destroy; begin inherited Destroy; end;
procedure THD44780.MandaComando(Comando : Byte); begin
ValorPorta2 := (ValorPorta2) and LCD_RS_I;
WritePort(PortaLCD + 2, ValorPorta2 and LCD_EN_I);
WritePort(PortaLCD + 2, ValorPorta2 and LCD_EN_I);
usleep(1);
WritePort(PortaLCD, Comando);
usleep(1);
WritePort(PortaLCD + 2, ValorPorta2 or LCD_EN); if (Comando = LCD_LIMPA) or (Comando = LCD_HOME) then
usleep(TEMPO_LONGO) else
usleep(TEMPO_CURTO); end;
procedure THD44780.MandaDados(Dado : Byte); begin
ValorPorta2 := (ValorPorta2) or LCD_RS;
WritePort(PortaLCD + 2, ValorPorta2 and LCD_EN_I);
usleep(1);
WritePort(PortaLCD, Dado);
usleep(1);
WritePort(PortaLCD + 2, ValorPorta2 or LCD_EN);
usleep(TEMPO_CURTO); end;
procedure THD44780.IniciaLCD; begin
MandaComando(LCD_FUNCAO or LCD_FUNCAO_B or LCD_FUNCAO_L);
usleep(TEMPO_INICIO);
MandaComando(LCD_FUNCAO or LCD_FUNCAO_B or LCD_FUNCAO_L);
usleep(TEMPO_INICIO); end;
procedure THD44780.LimpaDisplay; begin
MandaComando(LCD_LIMPA); end;
procedure THD44780.Backlight(Ligar : Boolean); begin // end;
procedure THD44780.PosCursor(X, Y : Integer); var
offset : Integer; begin
offset := 0; if X > Colunas thenbegin
X := 1;
inc(Y); end; if Y > Linhas then
Y := 1; case Y of
1: offset := 0;
2: offset := 64;
3: offset := 20;
4: offset := 84; end;
MandaComando(LCD_DDRAM + offset + (X-1)); end;
procedure THD44780.Cursor_Display(CursorLigado, DisplayLigado, CursorPiscante : Boolean); var
Temp : Byte; begin
Temp := LCD_ONOFF; if CursorLigado then Temp := Temp or LCD_ONOFF_C; if DisplayLigado then Temp := Temp or LCD_ONOFF_D; if CursorPiscante then Temp := Temp or LCD_ONOFF_CP;
MandaComando(Temp); end;
procedure THD44780.DirecaoCursor(PraDireita, MensagemDesloca : Boolean); var
Temp : Byte; begin
Temp := LCD_MODO; if PraDireita then Temp := Temp or LCD_MODO_C; if MensagemDesloca then Temp := Temp or LCD_MODO_M;
MandaComando(Temp); end;
procedure THD44780.DeslocaCursor_Mensagem(Cursor, PraDireita : Boolean); var
Temp : Byte; begin
Temp := LCD_DESLOCA; ifnot Cursor then Temp := Temp or LCD_DESLOCA_M; if PraDireita then Temp := Temp or LCD_DESLOCA_D;
MandaComando(Temp); end;
procedure THD44780.EscreveTexto(Mensagem : String); var
I : Byte; begin for I := 1 to Length(Mensagem) do
MandaDados(Ord(Mensagem[I])); end;
procedure TForm1.DefineNewChar(CharNum: Byte; ACharSet: TNewCharSet); var
CGRam: Integer;
i: Integer; begin if CharNum > 7 then
Exit; // can only define from char 0 to 7
CGRam := $40 + (CharNum shl 3);
lcd.MandaComando(cgram); //WriteControl(CGRam); //comentar novamente for i := 0 to 7 do
lcd.MandaDados(ACharSet[i]); //WriteData(ACharSet[i]); //comentar novamente end;
function TForm1.BinToInt(Value: String): Integer; var
i,Size: Integer; begin
Result:=0;
Size:=Length(Value); for i:=Size downto 1 do if Value[i]='1' then Result:=Result+(1 shl (Size-i)); end;
procedure TForm1.FormCreate(Sender: TObject); begin
lcd := THD44780.Create;
InicializaLCD; end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction); begin
lcd.Free; end;