1 | ----------------------------------------------------------------------------------
|
2 | -- Company:
|
3 | -- Engineer:
|
4 | --
|
5 | -- Create Date: 10:01:07 02/27/2012
|
6 | -- Design Name:
|
7 | -- Module Name: fsm - Behavioral
|
8 | -- Project Name:
|
9 | -- Target Devices:
|
10 | -- Tool versions:
|
11 | -- Description:
|
12 | --
|
13 | -- Dependencies:
|
14 | --
|
15 | -- Revision:
|
16 | -- Revision 0.01 - File Created
|
17 | -- Additional Comments:
|
18 | --
|
19 | ----------------------------------------------------------------------------------
|
20 | library IEEE;
|
21 | use IEEE.STD_LOGIC_1164.ALL;
|
22 |
|
23 | -- Uncomment the following library declaration if using
|
24 | -- arithmetic functions with Signed or Unsigned values
|
25 | use IEEE.NUMERIC_STD.ALL;
|
26 |
|
27 | -- Uncomment the following library declaration if instantiating
|
28 | -- any Xilinx primitives in this code.
|
29 | --library UNISIM;
|
30 | --use UNISIM.VComponents.all;
|
31 |
|
32 | entity fsm is
|
33 | Port ( clk : in STD_LOGIC;
|
34 | res_n : in STD_LOGIC;
|
35 | lcd_e : out STD_LOGIC;
|
36 | lcd_rs : out STD_LOGIC;
|
37 | lcd_rw : out STD_LOGIC;
|
38 | lcd_db : inout STD_LOGIC_VECTOR (7 downto 0));
|
39 | end fsm;
|
40 |
|
41 | architecture Behavioral of fsm is
|
42 |
|
43 | constant delay_40ms : integer := 4000000;
|
44 | constant delay_37us : integer := 3700;
|
45 | constant delay_1520us : integer := 152000;
|
46 | constant tsu : integer := 0; -- RS, R/W setup time (min. 0ns)
|
47 | constant twdsu : integer := 8; -- Write data setup time (min. 80ns)
|
48 | constant tdh : integer := 30; -- Data hold time (min. 300ns)
|
49 | constant th : integer := 1; -- RS, R/W hold time (min. 10ns)
|
50 |
|
51 | type state_type is (boot, function_set1, function_set2, disp_ctr, disp_clr, disp_entr, write_letter,idle);
|
52 | signal current_state : state_type := boot;
|
53 | signal cnt : integer range 0 to delay_40ms := 0;
|
54 |
|
55 | begin
|
56 |
|
57 | process(clk) is
|
58 |
|
59 | begin
|
60 | if rising_edge(clk) then
|
61 | case current_state is
|
62 | when boot => if cnt < delay_40ms then
|
63 | cnt <= cnt + 1;
|
64 | current_state <= boot;
|
65 | else
|
66 | cnt <= 0;
|
67 | current_state <= function_set1;
|
68 | end if;
|
69 |
|
70 | when function_set1 => lcd_rs <= '0';
|
71 | lcd_rw <= '0';
|
72 | lcd_db <= "001110" & '-' & '-';
|
73 |
|
74 | if cnt < delay_37us then
|
75 | cnt <= cnt + 1;
|
76 | current_state <= function_set1;
|
77 | else
|
78 | cnt <= 0;
|
79 | current_state <= function_set2;
|
80 | end if;
|
81 |
|
82 | when function_set2 => lcd_rs <= '0';
|
83 | lcd_rw <= '0';
|
84 | lcd_db <= "001110" & '-' & '-';
|
85 | if cnt < delay_37us then
|
86 | cnt <= cnt + 1;
|
87 | current_state <= function_set2;
|
88 | else
|
89 | cnt <= 0;
|
90 | current_state <= disp_ctr;
|
91 | end if;
|
92 |
|
93 | when disp_ctr => lcd_rs <= '0';
|
94 | lcd_rw <= '0';
|
95 | lcd_db <= "00001111";
|
96 | if cnt < delay_37us then
|
97 | cnt <= cnt + 1;
|
98 | current_state <= disp_ctr;
|
99 | else
|
100 | cnt <= 0;
|
101 | current_state <= disp_clr;
|
102 | end if;
|
103 |
|
104 | when disp_clr => lcd_rs <= '0';
|
105 | lcd_rw <= '0';
|
106 | lcd_db <= "00000001";
|
107 | if cnt < delay_1520us then
|
108 | cnt <= cnt + 1;
|
109 | current_state <= disp_clr;
|
110 | else
|
111 | cnt <= 0;
|
112 | current_state <= disp_entr;
|
113 | end if;
|
114 | when disp_entr => lcd_rs <= '0';
|
115 | lcd_rw <= '0';
|
116 | lcd_db <= "00000110";
|
117 | if cnt < delay_37us then
|
118 | cnt <= cnt + 1;
|
119 | current_state <= disp_entr;
|
120 | else
|
121 | cnt <= 0;
|
122 | current_state <= write_letter;
|
123 | end if;
|
124 | when write_letter => if cnt < twdsu then
|
125 | lcd_e <= '1';
|
126 | lcd_rw <= '0';
|
127 | lcd_rs <= '1';
|
128 | lcd_db <= "01000001";
|
129 | cnt <= cnt + 1;
|
130 | elsif cnt = twdsu then
|
131 | lcd_e <= '0';
|
132 | cnt <= cnt + 1;
|
133 | elsif cnt < twdsu + th then
|
134 | cnt <= cnt + 1;
|
135 | elsif cnt = twdsu + th then
|
136 | lcd_rw <= '1';
|
137 | cnt <= cnt + 1;
|
138 | else
|
139 | cnt <= 0;
|
140 | current_state <= idle;
|
141 | end if;
|
142 |
|
143 | when idle => lcd_e <= '1';
|
144 | lcd_rw <= '1';
|
145 | lcd_db <= "--------";
|
146 |
|
147 | current_state <= idle;
|
148 | end case;
|
149 | end if;
|
150 | end process f;
|
151 |
|
152 | end Behavioral;
|