cursofpgavhdl

LCD_controller

Dec 17th, 2019
558
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.01 KB | None | 0 0
  1. --------------------------------------------------------------------------------
  2. --
  3. -- FileName: lcd_controller.vhd
  4. -- Dependencies: none
  5. -- Design Software: Quartus II 32-bit Version 11.1 Build 173 SJ Full Version
  6. --
  7. -- HDL CODE IS PROVIDED "AS IS." DIGI-KEY EXPRESSLY DISCLAIMS ANY
  8. -- WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED, INCLUDING BUT NOT
  9. -- LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  10. -- PARTICULAR PURPOSE, OR NON-INFRINGEMENT. IN NO EVENT SHALL DIGI-KEY
  11. -- BE LIABLE FOR ANY INCIDENTAL, SPECIAL, INDIRECT OR CONSEQUENTIAL
  12. -- DAMAGES, LOST PROFITS OR LOST DATA, HARM TO YOUR EQUIPMENT, COST OF
  13. -- PROCUREMENT OF SUBSTITUTE GOODS, TECHNOLOGY OR SERVICES, ANY CLAIMS
  14. -- BY THIRD PARTIES (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF),
  15. -- ANY CLAIMS FOR INDEMNITY OR CONTRIBUTION, OR OTHER SIMILAR COSTS.
  16. --
  17. -- Version History
  18. -- Version 1.0 6/2/2006 Scott Larson
  19. -- Initial Public Release
  20. -- Version 2.0 6/13/2012 Scott Larson
  21. --
  22. -- CLOCK FREQUENCY: to change system clock frequency, change Line 65
  23. --
  24. -- LCD INITIALIZATION SETTINGS: to change, comment/uncomment lines:
  25. --
  26. -- Function Set
  27. -- 2-line mode, display on Line 93 lcd_data <= "00111100";
  28. -- 1-line mode, display on Line 94 lcd_data <= "00110100";
  29. -- 1-line mode, display off Line 95 lcd_data <= "00110000";
  30. -- 2-line mode, display off Line 96 lcd_data <= "00111000";
  31. -- Display ON/OFF
  32. -- display on, cursor off, blink off Line 104 lcd_data <= "00001100";
  33. -- display on, cursor off, blink on Line 105 lcd_data <= "00001101";
  34. -- display on, cursor on, blink off Line 106 lcd_data <= "00001110";
  35. -- display on, cursor on, blink on Line 107 lcd_data <= "00001111";
  36. -- display off, cursor off, blink off Line 108 lcd_data <= "00001000";
  37. -- display off, cursor off, blink on Line 109 lcd_data <= "00001001";
  38. -- display off, cursor on, blink off Line 110 lcd_data <= "00001010";
  39. -- display off, cursor on, blink on Line 111 lcd_data <= "00001011";
  40. -- Entry Mode Set
  41. -- increment mode, entire shift off Line 127 lcd_data <= "00000110";
  42. -- increment mode, entire shift on Line 128 lcd_data <= "00000111";
  43. -- decrement mode, entire shift off Line 129 lcd_data <= "00000100";
  44. -- decrement mode, entire shift on Line 130 lcd_data <= "00000101";
  45. --
  46. --------------------------------------------------------------------------------
  47.  
  48. LIBRARY ieee;
  49. USE ieee.std_logic_1164.ALL;
  50.  
  51. ENTITY lcd_controller IS
  52. PORT(
  53. clk : IN STD_LOGIC; --system clock
  54. reset_n : IN STD_LOGIC; --active low reinitializes lcd
  55. lcd_enable : IN STD_LOGIC; --latches data into lcd controller
  56. lcd_bus : IN STD_LOGIC_VECTOR(9 DOWNTO 0); --data and control signals
  57. busy : OUT STD_LOGIC := '1'; --lcd controller busy/idle feedback
  58. rw, rs, e : OUT STD_LOGIC; --read/write, setup/data, and enable for lcd
  59. lcd_data : OUT STD_LOGIC_VECTOR(7 DOWNTO 0)); --data signals for lcd
  60. END lcd_controller;
  61.  
  62. ARCHITECTURE controller OF lcd_controller IS
  63. TYPE CONTROL IS(power_up, initialize, ready, send);
  64. SIGNAL state : CONTROL;
  65. CONSTANT freq : INTEGER := 50; --system clock frequency in MHz
  66. BEGIN
  67. PROCESS(clk)
  68. VARIABLE clk_count : INTEGER := 0; --event counter for timing
  69. BEGIN
  70. IF(clk'EVENT and clk = '1') THEN
  71.  
  72. CASE state IS
  73.  
  74. --wait 50 ms to ensure Vdd has risen and required LCD wait is met
  75. WHEN power_up =>
  76. busy <= '1';
  77. IF(clk_count < (50000 * freq)) THEN --wait 50 ms
  78. clk_count := clk_count + 1;
  79. state <= power_up;
  80. ELSE --power-up complete
  81. clk_count := 0;
  82. rs <= '0';
  83. rw <= '0';
  84. lcd_data <= "00110000";
  85. state <= initialize;
  86. END IF;
  87.  
  88. --cycle through initialization sequence
  89. WHEN initialize =>
  90. busy <= '1';
  91. clk_count := clk_count + 1;
  92. IF(clk_count < (10 * freq)) THEN --function set
  93. lcd_data <= "00111100"; --2-line mode, display on
  94. --lcd_data <= "00110100"; --1-line mode, display on
  95. --lcd_data <= "00110000"; --1-line mdoe, display off
  96. --lcd_data <= "00111000"; --2-line mode, display off
  97. e <= '1';
  98. state <= initialize;
  99. ELSIF(clk_count < (60 * freq)) THEN --wait 50 us
  100. lcd_data <= "00000000";
  101. e <= '0';
  102. state <= initialize;
  103. ELSIF(clk_count < (70 * freq)) THEN --display on/off control
  104. lcd_data <= "00001100"; --display on, cursor off, blink off
  105. --lcd_data <= "00001101"; --display on, cursor off, blink on
  106. --lcd_data <= "00001110"; --display on, cursor on, blink off
  107. --lcd_data <= "00001111"; --display on, cursor on, blink on
  108. --lcd_data <= "00001000"; --display off, cursor off, blink off
  109. --lcd_data <= "00001001"; --display off, cursor off, blink on
  110. --lcd_data <= "00001010"; --display off, cursor on, blink off
  111. --lcd_data <= "00001011"; --display off, cursor on, blink on
  112. e <= '1';
  113. state <= initialize;
  114. ELSIF(clk_count < (120 * freq)) THEN --wait 50 us
  115. lcd_data <= "00000000";
  116. e <= '0';
  117. state <= initialize;
  118. ELSIF(clk_count < (130 * freq)) THEN --display clear
  119. lcd_data <= "00000001";
  120. e <= '1';
  121. state <= initialize;
  122. ELSIF(clk_count < (2130 * freq)) THEN --wait 2 ms
  123. lcd_data <= "00000000";
  124. e <= '0';
  125. state <= initialize;
  126. ELSIF(clk_count < (2140 * freq)) THEN --entry mode set
  127. lcd_data <= "00000110"; --increment mode, entire shift off
  128. --lcd_data <= "00000111"; --increment mode, entire shift on
  129. --lcd_data <= "00000100"; --decrement mode, entire shift off
  130. --lcd_data <= "00000101"; --decrement mode, entire shift on
  131. e <= '1';
  132. state <= initialize;
  133. ELSIF(clk_count < (2200 * freq)) THEN --wait 60 us
  134. lcd_data <= "00000000";
  135. e <= '0';
  136. state <= initialize;
  137. ELSE --initialization complete
  138. clk_count := 0;
  139. busy <= '0';
  140. state <= ready;
  141. END IF;
  142.  
  143. --wait for the enable signal and then latch in the instruction
  144. WHEN ready =>
  145. IF(lcd_enable = '1') THEN
  146. busy <= '1';
  147. rs <= lcd_bus(9);
  148. rw <= lcd_bus(8);
  149. lcd_data <= lcd_bus(7 DOWNTO 0);
  150. clk_count := 0;
  151. state <= send;
  152. ELSE
  153. busy <= '0';
  154. rs <= '0';
  155. rw <= '0';
  156. lcd_data <= "00000000";
  157. clk_count := 0;
  158. state <= ready;
  159. END IF;
  160.  
  161. --send instruction to lcd
  162. WHEN send =>
  163. busy <= '1';
  164. IF(clk_count < (50 * freq)) THEN --do not exit for 50us
  165. busy <= '1';
  166. IF(clk_count < freq) THEN --negative enable
  167. e <= '0';
  168. ELSIF(clk_count < (14 * freq)) THEN --positive enable half-cycle
  169. e <= '1';
  170. ELSIF(clk_count < (27 * freq)) THEN --negative enable half-cycle
  171. e <= '0';
  172. END IF;
  173. clk_count := clk_count + 1;
  174. state <= send;
  175. ELSE
  176. clk_count := 0;
  177. state <= ready;
  178. END IF;
  179.  
  180. END CASE;
  181.  
  182. --reset
  183. IF(reset_n = '0') THEN
  184. state <= power_up;
  185. END IF;
  186.  
  187. END IF;
  188. END PROCESS;
  189. END controller;
Advertisement
Add Comment
Please, Sign In to add comment