Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
122 changes: 122 additions & 0 deletions LCD_controle2.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
module LCD_INIT(
input clk, // Clock do sistema
input rst, // Reset assíncrono ativo em nível baixo
input init_start, // Sinal para iniciar a sequência de inicialização
output reg [7:0] lcd_data, // Barramento de dados para o LCD
output reg lcd_rs, // Registro/Comando
output reg lcd_rw, // Read/Write
output reg lcd_e, // Enable
output reg done // Indica que a sequência terminou
);

// Inicialização das saídas do LCD
initial begin
lcd_data = 0;
lcd_e = 0; // Enable
lcd_rw = 0; // Read/Write
lcd_rs = 0; // Registro/Comando
done = 0; // Sequência ainda não concluída
end

// Contador para criar delays entre instruções
reg [31:0] counter = 0;
parameter MS = 50_000; // Delay de exemplo (aprox 1ms com clock 50MHz)
parameter WRITE = 0, WAIT = 1; // Estados da FSM
reg [1:0] state = WRITE;

// Contador de instruções
reg [7:0] instructions = 0;

// Sequência de escrita no LCD
always @(posedge clk or negedge rst) begin
if (~rst) begin
counter <= 0;
state <= WRITE;
instructions <= 0;
done <= 0;
end else if (init_start) begin // Só funciona se init_start estiver ativo
case (state)
WRITE: begin
if(counter == MS) begin
state <= WAIT;
counter <= 0;
end else begin
counter <= counter + 1;
end
end
WAIT: begin
if(counter == MS - 1) begin
state <= WRITE;
counter <= 0;
if(instructions < 37) instructions <= instructions + 1;
else done <= 1; // Sequência concluída
end else begin
counter <= counter + 1;
end
end
default: begin end
endcase
end else begin
// Se init_start não estiver ativo, mantém tudo zerado
lcd_data <= 0;
lcd_rs <= 0;
lcd_rw <= 0;
lcd_e <= 0;
done <= 0;
counter <= 0;
state <= WRITE;
instructions <= 0;
end
end

// Lógica combinacional para definir dados e sinais do LCD
always @(*) begin
// RW sempre em 0 para escrita
lcd_rw = 0;

// Pulso de enable: WRITE = 1, WAIT = 0
case (state)
WRITE: lcd_e = 1;
WAIT: lcd_e = 0;
default: lcd_e = lcd_e;
endcase

// Sequência de instruções e dados
case (instructions)
0: lcd_data = 8'h38; lcd_rs = 0; // Habilita o modo de 8 bits, 2 linhas
1: lcd_data = 8'h0E; lcd_rs = 0; // Display ON, Cursor ON, Blink OFF
2: lcd_data = 8'h01; lcd_rs = 0; // Clear display
3: lcd_data = 8'h02; lcd_rs = 0; // Cursor Home
4: lcd_data = 8'h06; lcd_rs = 0; // Incrementa cursor

// Primeira linha: "---- [- - - -]"
5: lcd_data = 8'h2D; lcd_rs = 1; // -
6: lcd_data = 8'h2D; lcd_rs = 1; // -
7: lcd_data = 8'h2D; lcd_rs = 1; // -
8: lcd_data = 8'h2D; lcd_rs = 1; // -
9: lcd_data = 8'h20; lcd_rs = 1; // Espaço
10: lcd_data = 8'h20; lcd_rs = 1; // Espaço
11: lcd_data = 8'h20; lcd_rs = 1; // Espaço
12: lcd_data = 8'h20; lcd_rs = 1; // Espaço
13: lcd_data = 8'h5B; lcd_rs = 1; // [
14: lcd_data = 8'h2D; lcd_rs = 1; // -
15: lcd_data = 8'h2D; lcd_rs = 1; // -
16: lcd_data = 8'h2D; lcd_rs = 1; // -
17: lcd_data = 8'h2D; lcd_rs = 1; // -
18: lcd_data = 8'h5D; lcd_rs = 1; // ]

// Segunda linha: move cursor e escreve "+00000"
19: lcd_data = 8'hC0; lcd_rs = 0; // Cursor para segunda linha
20: lcd_data = 8'h2B; lcd_rs = 1; // +
21: lcd_data = 8'h30; lcd_rs = 1; // 0
22: lcd_data = 8'h30; lcd_rs = 1; // 0
23: lcd_data = 8'h30; lcd_rs = 1; // 0
24: lcd_data = 8'h30; lcd_rs = 1; // 0
25: lcd_data = 8'h30; lcd_rs = 1; // 0

default: begin
lcd_data = 8'h02; lcd_rs = 0; // Default: cursor home
end
endcase
end
endmodule
111 changes: 111 additions & 0 deletions LCD_controller.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
module LCD_INIT(
output reg[7:0] data,
output reg EN, RW, RS,

input clk
);

initial begin
data = 0;
EN = 0; //Enable
RW = 0; //Read/Write
RS = 0;//
end

reg [31:0] counter = 0;
parameter MS = 50_000;
parameter WRITE = 0, WAIT = 1; // estados da fsm
reg [3:0] state = WRITE;

reg [7:0] instructions = 0;

always @(posedge clk, negedge rst) begin
case (state)
WRITE: begin
if(counter == MS) begin
state = WAIT;
counter = 0;
end else begin
counter = counter + 1;
end
end
WAIT: begin
if(counter == MS - 1) begin
state = WRITE;
counter = 0;
if(instructions < 38) instructions = instructions + 1;
end else begin
counter = counter + 1;
end

end
default : begin end
endcase
end



always @(*) begin
case (state)
WRITE: EN <= 1;
WAIT: EN <= 0;
default: EN <= EN;
endcase

case (instructions)
0: begin data <= 8'h38; RS <= 0; end //Habilita o modo de 8 bits, adiciona a segunda linha
1: begin data <= 8'h0E; RS <= 0; end //Display ON, Cursos ON, Blink OFF
2: begin data <= 8'h01; RS <= 0; end //Clear
3: begin data <= 8'h02; RS <= 0; end //Cursor Home // POSSO MUDAR A ODEM DO 06 E 02
4: begin data <= 8'h06; RS <= 0; end // Pula o cursor quando printa

5: begin data <= 8'h2D; RS <= 1; end //-
6: begin data <= 8'h2D; RS <= 1; end //-
7: begin data <= 8'h2D; RS <= 1; end //-
8: begin data <= 8'h2D; RS <= 1; end //-



9: begin data <= 8'h20; RS <= 1; end //ESPAÇO
10:begin data <= 8'h20; RS <= 1; end //ESPAÇO
11: begin data <= 8'h20; RS <= 1; end //ESPAÇO
12: begin data <= 8'h20; RS <= 1; end //ESPAÇO
13: begin data <= 8'h20; RS <= 1; end //ESPAÇO
14: begin data <= 8'h20; RS <= 1; end //ESPAÇO

15: begin data <= 8'h5B; RS <= 1; end // [
16: begin data <= 8'h2D; RS <= 1; end //-
17: begin data <= 8'h2D; RS <= 1; end //-
18: begin data <= 8'h2D; RS <= 1; end //-
19: begin data <= 8'h2D; RS <= 1; end //-
20 : begin data <= 8'h5D; RS <= 1; end //]

21: begin data <= 8'hC0; RS <= 0; end // PASSA O CURSOR PARA SEGUNDA LINHA
22: begin data <= 8'h20; RS <= 1; end //ESPAÇO
23: begin data <= 8'h20; RS <= 1; end //ESPAÇO
24: begin data <= 8'h20; RS <= 1; end //ESPAÇO
25: begin data <= 8'h20; RS <= 1; end //ESPAÇO
26: begin data <= 8'h20; RS <= 1; end //ESPAÇO
27: begin data <= 8'h20; RS <= 1; end //ESPAÇO
28: begin data <= 8'h20; RS <= 1; end //ESPAÇO
29: begin data <= 8'h20; RS <= 1; end //ESPAÇO
30: begin data <= 8'h20; RS <= 1; end //ESPAÇO
31: begin data <= 8'h20; RS <= 1; end //ESPAÇO

32: begin data <= 8'h2B; RS <= 1; end //+
33 : begin data <= 8'h30; RS <= 1; end //0
34: begin data <= 8'h30; RS <= 1; end //0
35: begin data <= 8'h30; RS <= 1; end //0
36: begin data <= 8'h30; RS <= 1; end //0
37: begin data <= 8'h30; RS <= 1; end //0






default: begin data <= 8'h02; RS <= 0; end
endcase
end

endmodule
Loading