Advertisement
modestoo

lcdcontroller

Apr 13th, 2021
301
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. LIBRARY ieee;
  2. USE ieee.std_logic_1164.ALL;
  3.  
  4. ENTITY lcd_controller_v2_1 IS
  5.   PORT(
  6.     clk        : IN    STD_LOGIC;  --system clock
  7.     reset_n    : IN    STD_LOGIC;  --active low reinitializes lcd
  8.     lcd_enable : IN    STD_LOGIC;  --latches data into lcd controller
  9.     lcd_bus    : IN    STD_LOGIC_VECTOR(9 DOWNTO 0);  --data and control signals
  10.     busy       : OUT   STD_LOGIC := '1';  --lcd controller busy/idle feedback
  11.     rw, rs, e  : OUT   STD_LOGIC;  --read/write, setup/data, and enable for lcd
  12.     lcd_data   : OUT   STD_LOGIC_VECTOR(7 DOWNTO 0)); --data signals for lcd
  13. END lcd_controller_v2_1;
  14.  
  15. ARCHITECTURE controller OF lcd_controller_v2_1 IS
  16.   TYPE CONTROL IS(power_up, initialize, ready, send);
  17.   SIGNAL    state      : CONTROL;
  18.   CONSTANT  freq       : INTEGER := 50; --system clock frequency in MHz
  19. BEGIN
  20.   PROCESS(clk)
  21.     VARIABLE clk_count : INTEGER := 0; --event counter for timing
  22.   BEGIN
  23.   IF(clk'EVENT and clk = '1') THEN
  24.    
  25.       CASE state IS
  26.        
  27.         --wait 50 ms to ensure Vdd has risen and required LCD wait is met
  28.         WHEN power_up =>
  29.           busy <= '1';
  30.           IF(clk_count < (50000 * freq)) THEN    --wait 50 ms
  31.             clk_count := clk_count + 1;
  32.             state <= power_up;
  33.           ELSE                                   --power-up complete
  34.             clk_count := 0;
  35.             rs <= '0';
  36.             rw <= '0';
  37.             lcd_data <= "00110000";
  38.             state <= initialize;
  39.           END IF;
  40.          
  41.         --cycle through initialization sequence  
  42.         WHEN initialize =>
  43.           busy <= '1';
  44.           clk_count := clk_count + 1;
  45.           IF(clk_count < (10 * freq)) THEN       --function set
  46.             lcd_data <= "00111100";      --2-line mode, display on
  47.             --lcd_data <= "00110100";    --1-line mode, display on
  48.             --lcd_data <= "00110000";    --1-line mdoe, display off
  49.             --lcd_data <= "00111000";    --2-line mode, display off
  50.             e <= '1';
  51.             state <= initialize;
  52.           ELSIF(clk_count < (60 * freq)) THEN    --wait 50 us
  53.             lcd_data <= "00000000";
  54.             e <= '0';
  55.             state <= initialize;
  56.           ELSIF(clk_count < (70 * freq)) THEN    --display on/off control
  57.             lcd_data <= "00001100";      --display on, cursor off, blink off
  58.             --lcd_data <= "00001101";    --display on, cursor off, blink on
  59.             --lcd_data <= "00001110";    --display on, cursor on, blink off
  60.             --lcd_data <= "00001111";    --display on, cursor on, blink on
  61.             --lcd_data <= "00001000";    --display off, cursor off, blink off
  62.             --lcd_data <= "00001001";    --display off, cursor off, blink on
  63.             --lcd_data <= "00001010";    --display off, cursor on, blink off
  64.             --lcd_data <= "00001011";    --display off, cursor on, blink on            
  65.             e <= '1';
  66.             state <= initialize;
  67.           ELSIF(clk_count < (120 * freq)) THEN   --wait 50 us
  68.             lcd_data <= "00000000";
  69.             e <= '0';
  70.             state <= initialize;
  71.           ELSIF(clk_count < (130 * freq)) THEN   --display clear
  72.             lcd_data <= "00000001";
  73.             e <= '1';
  74.             state <= initialize;
  75.           ELSIF(clk_count < (2130 * freq)) THEN  --wait 2 ms
  76.             lcd_data <= "00000000";
  77.             e <= '0';
  78.             state <= initialize;
  79.           ELSIF(clk_count < (2140 * freq)) THEN  --entry mode set
  80.             lcd_data <= "00000110";      --increment mode, entire shift off
  81.             --lcd_data <= "00000111";    --increment mode, entire shift on
  82.             --lcd_data <= "00000100";    --decrement mode, entire shift off
  83.             --lcd_data <= "00000101";    --decrement mode, entire shift on
  84.             e <= '1';
  85.             state <= initialize;
  86.           ELSIF(clk_count < (2200 * freq)) THEN  --wait 60 us
  87.             lcd_data <= "00000000";
  88.             e <= '0';
  89.             state <= initialize;
  90.           ELSE                                   --initialization complete
  91.             clk_count := 0;
  92.             busy <= '0';
  93.             state <= ready;
  94.           END IF;    
  95.        
  96.         --wait for the enable signal and then latch in the instruction
  97.         WHEN ready =>
  98.           IF(lcd_enable = '1') THEN
  99.             busy <= '1';
  100.             rs <= lcd_bus(9);
  101.             rw <= lcd_bus(8);
  102.             lcd_data <= lcd_bus(7 DOWNTO 0);
  103.             clk_count := 0;            
  104.             state <= send;
  105.           ELSE
  106.             busy <= '0';
  107.             rs <= '0';
  108.             rw <= '0';
  109.             lcd_data <= "00000000";
  110.             clk_count := 0;
  111.             state <= ready;
  112.           END IF;
  113.        
  114.         --send instruction to lcd        
  115.         WHEN send =>
  116.         busy <= '1';
  117.         IF(clk_count < (50 * freq)) THEN  --do not exit for 50us
  118.            busy <= '1';
  119.            IF(clk_count < freq) THEN      --negative enable
  120.              e <= '0';
  121.            ELSIF(clk_count < (14 * freq)) THEN  --positive enable half-cycle
  122.              e <= '1';
  123.            ELSIF(clk_count < (27 * freq)) THEN  --negative enable half-cycle
  124.              e <= '0';
  125.            END IF;
  126.            clk_count := clk_count + 1;
  127.            state <= send;
  128.         ELSE
  129.           clk_count := 0;
  130.           state <= ready;
  131.         END IF;
  132.  
  133.       END CASE;    
  134.    
  135.       --reset
  136.       IF(reset_n = '0') THEN
  137.         state <= power_up;
  138.       END IF;
  139.    
  140.     END IF;
  141.   END PROCESS;
  142. END controller;
Advertisement
RAW Paste Data Copied
Advertisement